1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
uses ...IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP, ....;
procedure TForm18.BitBtn1Click(Sender: TObject);
var F : TextFile;
Buffer,Markup : String;
Http: TIdHttp;
URL, Filename: string;
FS: TFileStream;
// mise en page d'un cours
procedure Cours (B : String);
var i,j,l : integer;
Devise : String;
Valeur : String;
c : Double;
begin
i:=Pos('(',B)+1;
Devise:=Copy(B,i,3);
i:=Pos('=',B)+1;
j:=Pos('<',B);
Valeur:=Copy(B,i,j-i);
Valeur:=StringReplace(Valeur,'.',decimalseparator,[]);
c:=StrToFloatDef(Valeur,0);
Memo1.Lines.Add(Format('%s %3.4f',[devise,c]));
end;
// mise en page date cours
procedure DateCours(B : String);
var i,j : Integer;
Day : String;
DC : TDate;
F : TFormatSettings;
d,m,y : word;
const
months : array[1..12] of string = ('Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sept','Nov','Oct','Dec');
begin
i:=Pos(':',B)+2;
j:=Pos('</h1>',B);
Day:=Copy(B,i,j-i);
d:=StrToInt(Copy(day,1,2));
y:=StrToInt(RightStr(day,4));
m:=ANSIindexstr(Copy(Day,4,3),months)+1;
try
DC:=encodedate(y,m,d);
memo1.Lines.Add(FormatDateTime('dd/mm/yyyy',DC));
except
memo1.Lines.Add(Day);
end;
end;
begin
URL := 'http://www.ecb.europa.eu/euro.html';
memo1.Lines.Clear;
Filename := ExtractFilePath(Application.exename)+'euro.html'; // le fichier sera récupéré
if FileExists(FileName) then DeleteFile(FileName);
FS := TFileStream.Create(Filename, fmCreate); // dans un stream
try
try
Http := TIdHttp.Create(nil);
try
try
Http.Get(URL, FS);
except
// on E: EIdHTTPProtocolException do begin
// Showmessage(E.ErrorMessage);
// if E.ErrorCode <> 404 then raise;
// if URL = '' then raise;
// end;
end;
finally
Http.Free;
end;
finally
FS.Free;
end;
except
DeleteFile(Filename);
memo1.Lines.add('Impossible de télécharger le fichier.');
Exit;
end;
{ Création du fichier de sortie }
AssignFile(F,'euro.html');
Reset(F) ;
while not EOF(F) do
begin
ReadLn(F, buffer);
if ANSIEndsText('<br />',Buffer) then Cours(Buffer);
if ANSIStartsText('<h1>Date',Buffer) then DateCours(Buffer);
end;
CloseFile(F) ;
end; |
Partager