unit uParseServices;
interface
uses IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, StrUtils, Classes;
type
TServices = (svGoogle, svMilw0rm, svHeise, svGulli, svTecchannel,
svWikipedia);
type
TResult =
record
Title: TStrings;
Link: TStrings;
end;
type
TPattern =
record
FBegin:
string;
FEnd:
string;
end;
type
TGoogle =
class
private
FPatternTitle: TPattern;
FPatternLink: TPattern;
FQueryURL:
string;
function Parse: TResult;
end;
type
TMilw0rm =
class
private
FPatternTitle: TPattern;
FQueryURL:
string;
function Parse: TResult;
end;
type
THeise =
class
private
FQueryURL:
string;
function Parse: TResult;
end;
type
TGulli =
class
private
FQueryURL:
string;
function Parse: TResult;
end;
type
TTecChannel =
class
private
FQueryURL:
string;
function Parse: TResult;
end;
type
TWikipedia =
class
private
FPatternTitle: TPattern;
FPatternArticle: TPattern;
FQueryURL:
string;
function Parse:
string;
// Erster Artikel
end;
{(*}const
GoogleURL = '
http://google.de/?q=[<QUERY>]';
Milw0rmURL = '
http://milw0rm.com/?q=[<QUERY>]';
HeiseURL = '
http://heise.de/?q=[<QUERY>]';
GulliURL = '
http://gulli.com/?q=[<QUERY>]';
TecChannelURL = '
...[<QUERY>]';
WikipediaURL = '
http://wikipedia.de/go?q=[<QUERY>]&l=de&e=wikipedia&s=suchen&b=suchen';
{*)}
type
TParseServices =
class
private
FResponse:
string;
FQuery:
string;
FService: TServices;
FFilterdLinks: TStrings;
FFilterdTitles: TStrings;
Google: TGoogle;
Milw0rm: TMilw0rm;
Heise: THeise;
Gulli: TGulli;
TecChannel: TTecChannel;
Wikipedia: TWikipedia;
public
property Service: TServices
read FService
write FService;
property Query:
string read FQuery
write FQuery;
procedure Submit;
function Results: TResult;
property GetResult: TResult
read Results;
// Gibt den Titel und die Links wieder
property GetRawHTTPResponse:
string read FResponse;
// Gibt den kompletten HTTP Response zurück
constructor Create;
destructor Destroy;
override;
end;
implementation
constructor TParseServices.Create;
begin
inherited;
Google := TGoogle.Create;
Milw0rm := TMilw0rm.Create;
Heise := THeise.Create;
Gulli := TGulli.Create;
TecChannel := TTecChannel.Create;
Wikipedia := TWikipedia.Create;
FFilterdLinks.Create;
FFilterdTitles.Create;
// [<QUERY>] als Platzhalter wird mit StringReplace ersetzt
// Stimmen nicht alle, nur für Anschauung präsent
Google.FQueryURL := GoogleURL;
Milw0rm.FQueryURL := Milw0rmURL;
Heise.FQueryURL := HeiseURL;
Gulli.FQueryURL := GulliURL;
TecChannel.FQueryURL := TecChannelURL;
Wikipedia.FQueryURL := WikipediaURL;
// Parst den Content
with Google
do
begin
FPatternTitle.FBegin := '
';
// Pattern kommen noch :)
FPatternTitle.FEnd := '
';
FPatternLink.FBegin := '
';
FPatternLink.FEnd := '
';
end;
with Milw0rm
do
begin
FPatternTitle.FBegin := '
';
FPatternTitle.FEnd := '
';
end;
with Heise
do
begin
end;
with Gulli
do
begin
end;
with TecChannel
do
begin
end;
with Wikipedia
do
begin
FPatternTitle.FBegin := '
<h1 id="firstHeading" class="firstHeading">';
// Titel <h1 id="firstHeading" class="firstHeading">Titel</h1>
FPatternTitle.FEnd := '
</h1>';
FPatternArticle.FBegin := '
';
// Content
...</p><table id="toc" class="toc">
FPatternArticle.FEnd := '
</p><table id="toc" class="toc">';
end;
end;
destructor TParseServices.Destroy;
begin
Google.Free;
Milw0rm.Free;
Heise.Free;
Gulli.Free;
TecChannel.Free;
Wikipedia.Free;
FFilterdLinks.Free;
FFilterdTitles.Free;
inherited;
end;
procedure TParseServices.Submit;
begin
case Service
of
svGoogle:
begin
Google.Parse;
end;
svMilw0rm:
begin
end;
svHeise:
begin
end;
svGulli:
begin
end;
svTecchannel:
begin
end;
svWikipedia:
begin
end;
end;
end;
function TGoogle.Parse: TResult;
var
Titles: TStrings;
Links: TStrings;
begin
Titles := TStringList.Create;
Links := TStringList.Create;
// ToDo: FQueryURL wird aufgerufen und die Anwort in FResponse gespeichert
// Dann wird mitPos, PosEx, Copy geparst bis Titel und Links gefiltert sind
Titles.Add('
ATitle');
Links.Add('
ALink');
Parse.Title := Titles;
Parse.Link := Links;
Links.Free;
Titles.Free;
end;
function TParseServices.Results: TResult;
begin
Results.Link := FFilterdLinks;
Results.Title := FFilterdTitles;
end;
end.