Retrieve and search chat history: Difference between revisions

Added FreeBASIC
m (syntax highlighting fixup automation)
(Added FreeBASIC)
 
(2 intermediate revisions by 2 users not shown)
Line 173:
end
end</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">#include "curl.bi"
#include "vbcompat.bi"
#define NULL 0
 
Dim Shared As CURL Ptr curl = NULL
 
Function WriteMemoryCallback(Byval contents As Any Ptr, Byval size As Uinteger, Byval nmemb As Uinteger, Byval userp As String Ptr) As Uinteger
Dim As Uinteger realsize = size * nmemb
*userp &= *Cptr(ZString Ptr, contents)
Return realsize
End Function
 
Function download(url As String) As String
If curl = NULL Then
curl_global_init(CURL_GLOBAL_DEFAULT)
curl = curl_easy_init()
End If
Dim As String readBuffer = ""
curl_easy_setopt(curl, CURLOPT_URL, url)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @WriteMemoryCallback)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, @readBuffer)
Dim As CURLcode res = curl_easy_perform(curl)
If res <> 0 Then
Print "libcurl error "; res; " ("; *curl_easy_strerror(res); ")"
Return ""
End If
Return readBuffer
End Function
 
Function grep(needle As String, haystack As String) As String
Dim As Integer i, j
Dim As String res = ""
j = 1
For i = 1 To Len(haystack)
If Mid(haystack, i, 1) = Chr(10) Then
Dim As String linea = Mid(haystack, j, i - j)
If Instr(linea, needle) Then
res &= linea & Chr(10)
End If
j = i + 1
End If
Next i
If Len(res) = 0 Then res = "no occurrences"
Return res
End Function
 
Dim As String needle = "github"
Dim As Integer days = 10
Dim As Integer i
For i = -days To 0
Dim As String dateStr = Format(Dateadd("d", i, Cdbl(Date)), "yyyy-mm-dd")
Dim As String url = "http://tclers.tk/conferences/tcl/" & dateStr & ".tcl"
Dim As String contents = download(url)
If Len(contents) = 0 Then Exit For
Print url
Print grep(needle, contents)
Next i
 
Sleep</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 277 ⟶ 338:
}
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
 
public final class RetrieveAndSearchChatHistory {
 
public static void main(String[] aArgs) throws URISyntaxException, IOException {
String subjectOfSearch = aArgs[0]; // The string 'available' was used to produce the displayed output.
 
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
LocalDate tomorrow = now.toLocalDate().plusDays(1);
LocalDate testDate = tomorrow.minusDays(10);
 
while ( ! testDate.equals(tomorrow ) ) {
URL address = new URI("http://tclers.tk/conferences/tcl/" + testDate + ".tcl").toURL();
HttpURLConnection connection = (HttpURLConnection) address.openConnection();
BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream()) );
System.out.println("Searching chat logs for " + testDate);
String line = "";
while ( ( line = reader.readLine() ).contains(subjectOfSearch) ) {
System.out.println(line);
}
reader.close();
connection.disconnect();
testDate = testDate.plusDays(1);
}
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Searching chat logs for 2023-08-28
m 2023-08-27T22:01:36Z {} {kurisu2 has become available}
Searching chat logs for 2023-08-29
Searching chat logs for 2023-08-30
m 2023-08-29T22:04:31Z {} {kevin_walzer has become available}
Searching chat logs for 2023-08-31
Searching chat logs for 2023-09-01
Searching chat logs for 2023-09-02
Searching chat logs for 2023-09-03
Searching chat logs for 2023-09-04
Searching chat logs for 2023-09-05
Searching chat logs for 2023-09-06
</pre>
 
=={{header|Julia}}==
Line 934 ⟶ 1,053:
 
However, it is designed to be used primarily as an embedded scripting language and we can therefore ask the host (in this case a C program) to call the necessary functions from its own libraries and pass the results back to Wren. We can then manipulate the date using the above module, search the downloaded files for the string in question and print the results.
<syntaxhighlight lang="ecmascriptwren">/* retrieve_and_search_chat_historyRetrieve_and_search_chat_history.wren */
 
import "./date" for Date
Line 1,002 ⟶ 1,121:
<br>
We now embed this in the following C program, build and run. Note that, whilst it would be possible to embed the Wren script ''directly'' into the C program (so there's only a single file), we have refrained from doing so in the interests of clarity.
<syntaxhighlight lang="c">/* gcc retrieve_and_search_chat_historyRetrieve_and_search_chat_history.c -o retrieve_and_search_chat_historyRetrieve_and_search_chat_history -lcurl -lwren -lm */
 
#include <stdio.h>
Line 1,199 ⟶ 1,318:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "retrieve_and_search_chat_historyRetrieve_and_search_chat_history.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 1,220 ⟶ 1,339:
Sample input/output:
<pre>
$ ./retrieve_and_search_chat_historyRetrieve_and_search_chat_history "now known as"
It's January 9 2022 4:07pm CET just now in Germany.
Searching for 'now known as' in the TCL Chatroom logs for the last 10 days:
2,130

edits