Skip to content

Instantly share code, notes, and snippets.

@freeonterminate
Last active June 19, 2017 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freeonterminate/cab6db4be11b7114aa528b22f2dbbb7f to your computer and use it in GitHub Desktop.
Save freeonterminate/cab6db4be11b7114aa528b22f2dbbb7f to your computer and use it in GitHub Desktop.
Show MVP List from Embarcadero web page.
(*
* This is sample code (My memorandum)
* System.Net.HttpClient.THttpClient class
*
* Programmed by HOSOKAWA Jun / twitter @pik
*)
program MVPCounter;
{$APPTYPE CONSOLE}
uses
System.Classes,
System.SysUtils,
System.Net.HttpClient;
const
// MVP Directory URL
EMBARCADERO_MVP_DIRECTORY_URL =
'https://www.embarcadero.com/jp/mvp-directory';
// MVP Start Tag in Directory-HTML
MVP_START_TAG = '<div class="col-sm-6 info">'#13#10'<h4>';
MVP_END_CHAR = '</h4>';
MVP_START_TAG_LEN = Length(MVP_START_TAG);
// Http getting timeout
TIME_OUT_MSEC = 1000 * 30; // = 30 [sec]
type
TOpenThread = class(TThread);
var
Source: String;
Count: Integer;
HttpThread: TThread;
StartTime: TDateTime;
TimedOut: Boolean;
function CheckSource: Boolean;
var
SIndex, EIndex: Integer;
begin
SIndex := Source.IndexOf(MVP_START_TAG);
Result := SIndex > -1;
if (Result) then
begin
Source := Source.Substring(SIndex + MVP_START_TAG_LEN);
EIndex := Source.IndexOf(MVP_END_CHAR);
if (EIndex > -1) then
Writeln(Source.Substring(0, EIndex));
end;
end;
procedure HttpReceiveDataCallBack(
const Sender: TObject;
AContentLength: Int64;
AReadCount: Int64;
var Abort: Boolean);
begin
Write(#13);
Write('Read: ', AReadCount, ' bytes');
end;
begin
{$REGION 'if iOS / Android then Complile error.'}
// iOS / Android does not have a console.
{$IF defined(ANDROID) or defined(IOS)}
{$MESSAGE FATAL 'The platform must have Console.'};
{$ENDIF}
{$ENDREGION}
Writeln;
Writeln('embarcadero MVP Counter');
Writeln;
HttpThread := // Auto Release (FreeOnTerminate = True)
TThread.CreateAnonymousThread(
procedure
var
Http: THttpClient;
Stream: TStringStream;
begin
Stream := nil;
Http := nil;
try
Stream := TStringStream.Create('', TEncoding.UTF8);
Http := THttpClient.Create;
// If you use Class, you can use OnReceivData Event.
Http.ReceiveDataCallBack := HttpReceiveDataCallBack;
// HttpClient is BlockingIO
Http.Get(EMBARCADERO_MVP_DIRECTORY_URL, Stream);
Source := Stream.DataString;
finally
Http.DisposeOf;
Stream.DisposeOf;
end;
HttpThread.Terminate;
end
);
Writeln('Getting directory source');
TimedOut := False;
StartTime := Now;
HttpThread.Start;
while not TOpenThread(HttpThread).Terminated do
begin
TThread.Sleep(100);
if ((Now - StartTime) * MSecsPerDay >= TIME_OUT_MSEC) then
begin
TimedOut := True;
Break;
end;
end;
Writeln;
if (TimedOut) then
Writeln('timeout')
else
Writeln('done');
Writeln;
Writeln('MVP List');
Writeln;
Count := 0;
while CheckSource do
Inc(Count);
Writeln;
Writeln('MVP Count = ', Count);
// Executed by IDE and Debugging ?
{$WARN SYMBOL_PLATFORM OFF}
if (DebugHook <> 0) then
begin
Writeln;
Writeln('Press Enter key to exit.');
Readln;
end;
{$WARN SYMBOL_PLATFORM ON}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment