Created
July 28, 2015 14:31
-
-
Save freeonterminate/c55c265f0c6830bf7a3b to your computer and use it in GitHub Desktop.
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 sample code (My memorandum) | |
* System.Net.HttpClient.THttpClient class | |
* | |
* Programmed by HOSOKAWA Jun / twitter @pik | |
*) | |
program MVPCount; | |
{$APPTYPE CONSOLE} | |
uses | |
System.Classes, | |
System.SysUtils, | |
System.Net.HttpClient; | |
const | |
// MVP Directory URL | |
EMBARCADERO_MVP_DIRECTORY_URL = 'http://www.embarcadero.com/jp/mvp-directory'; | |
// MVP Start Tag in Directory-HTML | |
MVP_START_TAG = '<h4 class="col-sm-4">'; | |
MVP_END_CHAR = '<'; | |
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 not defined(MSWINDOWS) and not (defined(MACOS) and not 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