Created
April 18, 2019 14:38
-
-
Save gerardmarquina/0e23a2580e79d32f6d50f898545a7065 to your computer and use it in GitHub Desktop.
A pdx forum web scraper for new eu4 dev diary, posts it as discord message using webhooks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//THIS IS CODED LIKE SHIT, ONLY TO BE USED TEMPORALY TIL REFACTORED | |
program bot; | |
uses | |
Winapi.Windows, System.SysUtils, System.Variants, System.Classes, | |
System.StrUtils, System.Net.URLClient, System.Net.HttpClient, | |
System.Net.HttpClientComponent; | |
{$APPTYPE CONSOLE} | |
var | |
client: TNetHTTPClient; | |
lr, response: TStringStream; | |
responsereader: TStreamReader; | |
responsestrings, newr: TStringList; | |
textline, textindex: integer; | |
i,x: integer; | |
new: string; | |
finished: boolean; | |
lasthashcode: integer; | |
source: TStringStream; | |
procedure ClearScreen; | |
var | |
stdout: THandle; | |
csbi: TConsoleScreenBufferInfo; | |
ConsoleSize: DWORD; | |
NumWritten: DWORD; | |
Origin: TCoord; | |
begin | |
stdout := GetStdHandle(STD_OUTPUT_HANDLE); | |
Win32Check(stdout<>INVALID_HANDLE_VALUE); | |
Win32Check(GetConsoleScreenBufferInfo(stdout, csbi)); | |
ConsoleSize := csbi.dwSize.X * csbi.dwSize.Y; | |
Origin.X := 0; | |
Origin.Y := 0; | |
Win32Check(FillConsoleOutputCharacter(stdout, ' ', ConsoleSize, Origin, | |
NumWritten)); | |
Win32Check(FillConsoleOutputAttribute(stdout, csbi.wAttributes, ConsoleSize, Origin, | |
NumWritten)); | |
Win32Check(SetConsoleCursorPosition(stdout, Origin)); | |
end; | |
begin | |
client := TNetHTTPClient.Create(nil); | |
response := TStringStream.Create; | |
client.ContentType := 'application/json'; | |
if paramcount>0 then begin | |
writeln('Executing pdx eu4 dev diary forum scrapper every ' + paramstr(1) + ' seconds'); | |
sleep(3000); | |
end | |
else | |
begin | |
writeln('Please supply the refresh parameter'); | |
readln; | |
exit; | |
end; | |
while true do | |
begin | |
clearscreen; | |
writeln('Scraping pdx game forums for new dev diaries...'); | |
client.Get('https://forum.paradoxplaza.com/forum/index.php?forums/europa-universalis-iv.731/', response); | |
writeln('Finished get request, analyzing...'); | |
responsereader := TStreamReader.Create(response); | |
//writeln(responsereader.ReadToEnd); | |
response.Position := 0; | |
responsestrings := TStringList.Create; | |
responsestrings.LoadFromStream(response); | |
for textline := 0 to responsestrings.Count do begin | |
textindex := ansipos('Development Diary', responsestrings[textline]); | |
if textindex<>0 then break; | |
end; | |
writeln('Found dev diary at line '+textline.ToString + ' at pos '+textindex.ToString); | |
writeln('Extracting entry URI...'); | |
i := ansipos('index.php?threads/', responsestrings[textline])-1; | |
i := responsestrings[textline].Length - i; | |
new := AnsiRightStr(responsestrings[textline], i); | |
i := ansipos('/preview', new); | |
new := AnsiLeftStr(new, i); | |
writeln('URI: ' + new); | |
writeln('Scraping forum entry... ' + 'https://forum.paradoxplaza.com/forum/'+new); | |
client.Get('https://forum.paradoxplaza.com/forum/'+new, response); | |
writeln('Finished get request, analyzing...'); | |
responsestrings := TStringList.Create; | |
responsestrings.LoadFromStream(response); | |
for textline := 0 to responsestrings.Count-1 do begin | |
textindex := ansipos('<div class="messageContent">', responsestrings[textline]); | |
if textindex<>0 then break; | |
end; | |
writeln('Found dev diary main comment at line '+textline.ToString + ' at pos '+textindex.ToString); | |
write('Printing dev diary message:'); | |
sleep(1000); | |
write('.'); | |
sleep(1000); | |
write('.'); | |
sleep(1000); | |
write('.'); | |
sleep(1000); | |
clearscreen(); | |
newr:= TStringList.Create; | |
x := 0; | |
i := 0; | |
finished := false; | |
if lr = response then writeln('Last dev diary already notified') else | |
begin | |
client.Post('webhook_goes_here', TStringStream.Create('{"content": "'+'New Dev Diary! : https://forum.paradoxplaza.com/forum/'+new+'"}'), response, [TNameValuePair.Create('Content-Type','application/json')]); | |
writeln(response.ReadString(response.Size)); | |
while finished=false do | |
begin | |
if ansipos('<div class="messageTextEndMarker">',responsestrings[textline+x]) <> 0 then break; | |
newr.Add(responsestrings[textline+x]); | |
writeln(newr[x]); | |
inc(x); | |
end; | |
lr := response; | |
end; | |
sleep(1000*strtoint(paramstr(1))); | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment