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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| unit robot;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer,
IdHTTPServer, StdCtrls, IdTCPConnection, IdTCPClient, IdHTTP,
IdCookieManager, decoupe;
type
TForm1 = class(TForm)
Start: TButton;
IdHTTP: TIdHTTP;
HTMLFORMULAIRE: TMemo;
HTMLBNF: TMemo;
HTMLPUBLIC: TMemo;
IdCookieManager1: TIdCookieManager;
Resultat: TMemo;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
procedure StartClick(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.StartClick(Sender: TObject);
var FORMULAIRE : TStringList ;
url : string ;
variable, valeur, deb, fin, presence, res : string ;
ltemp : string ;
tmp, debnom, finnom : string;
adresse, cp, ville, region : string;
i, p, nb, annonceparpage, mois, annee, page, j, nbpage : integer ;
decoupage : Tdecoupe;
datedeb, datefin : string;
begin
// on initialise les valeurs
HTMLFORMULAIRE.Clear ;
datedeb := '01/' + edit1.Text + '/' + edit2.Text;
mois := strtoint(edit1.Text);
annee := strtoint(edit2.Text);
if (mois<12) then mois := mois + 1
else
begin
mois := 1;
annee := annee + 1;
end;
datefin := '01/' + inttostr(mois) + '/' + inttostr(annee);
page := 1;
nbpage := 1;
// On telecharge la page d'accueil afin de se voir attribué un numero de session
url := 'http://www.journal-officiel.gouv.fr/jahia/Jahia/associations' ;
HTMLBNF.text := IdHTTP.Get(url) ;
//showmessage('fin dl page accueil');
FORMULAIRE := TStringList.create() ;
decoupage := Tdecoupe.Create(self);
refresh ;
// On analyse les variables de la page
for i := 0 to HTMLBNF.Lines.Count - 1 do
begin
if pos( '<!--' , uppercase(HTMLBNF.lines[i])) > 0 then HTMLBNF.lines[i] := '' ;
if pos( 'FORM NAME="' , uppercase(HTMLBNF.lines[i])) > 0 then
begin
ltemp := copy( HTMLBNF.lines[i],pos( 'FORM NAME="' , uppercase(HTMLBNF.lines[i]))+11 ,10000 );
if pos( 'ACTION="' , uppercase(HTMLBNF.lines[i])) > 0 then
begin
ltemp := copy( HTMLBNF.lines[i],pos( 'ACTION="' , uppercase(HTMLBNF.lines[i]))+8 ,10000 );
URL := copy( ltemp,1,pos( '"' ,ltemp )-1) ;
end ;
end else
begin
if (pos( 'NAME="' , uppercase(HTMLBNF.lines[i])) > 0) or (pos( 'NAME = "' , uppercase(HTMLBNF.lines[i])) > 0) then
begin
if (pos( 'NAME="' , uppercase(HTMLBNF.lines[i])) > 0) then ltemp := copy( HTMLBNF.lines[i],pos( 'NAME="' , uppercase(HTMLBNF.lines[i]))+6 ,10000 );
if (pos( 'NAME = "' , uppercase(HTMLBNF.lines[i])) > 0) then ltemp := copy( HTMLBNF.lines[i],pos( 'NAME = "' , uppercase(HTMLBNF.lines[i]))+8 ,10000 );
variable := copy( ltemp,1,pos( '"' ,ltemp )-1) ;
if pos( 'VALUE="' , uppercase(ltemp)) > 0 then
begin
ltemp := copy( ltemp,pos( 'VALUE="' , uppercase(ltemp))+7 ,10000 );
valeur := copy( ltemp,1,pos( '"' ,ltemp )-1) ;
end else
begin
if pos( 'VALUE=' , uppercase(ltemp)) > 0 then
begin
ltemp := copy( ltemp,pos( 'VALUE=' , uppercase(ltemp))+6 ,10000 );
valeur := copy( ltemp,1,pos( ' ' ,ltemp )-1) ;
end ;
end ;
// On analyse les variables de la page
if variable <> '' then
begin
if variable ='resultatsParPage' then valeur := '20' ;
if variable ='JPA_D_D' then valeur := datedeb ; //date debut
if variable ='JPA_D_F' then valeur := datefin ; //date fin
if variable ='JRE_ID' then valeur := region ; //departement
if variable ='original_method' then valeur := 'post' ;
if variable = 'JTY_ID' then valeur := inttostr(page); //numero de la page
HTMLFORMULAIRE.Lines.Add(variable+'='+valeur);
end ;
variable := '' ;
valeur := '' ;
end ;
end ;
end ;
//showmessage('fin analyse variables');
//on lance pour les 22 régions (region 1 non dispo sur jo)
for j:=2 to 2 do // A FAIRE METTRE j:2 to 22 do
begin
//showmessage('debut for region');
region := 'R' + inttostr(j);
repeat
HTMLPUBLIC.Clear;
FORMULAIRE.Clear;
resultat.Clear;
for i := 0 to htmlformulaire.Lines.Count - 1 do
begin
variable := copy(htmlformulaire.Lines[i],1,pos('=',htmlformulaire.Lines[i])-1);
valeur := '';
if variable = 'HI_PAGE' then valeur := '1' ;
if variable ='resultatsParPage' then valeur := '20' ;
if variable ='JPA_D_D' then valeur := datedeb ; //date debut
if variable ='JPA_D_F' then valeur := datefin ; //date fin
if variable ='JRE_ID' then valeur := region ; //departement
if variable ='original_method' then valeur := 'post' ;
if variable = 'JTY_ID' then valeur := inttostr(page); //numero de la page
htmlformulaire.Lines[i] := variable + '=' + valeur;
end ;
url := 'http://www.journal-officiel.gouv.fr/jahia/Jahia/associations/cache/bypass?' ;
url := url +'appid=1320_38&appparams=http%3A%2F%2Fwww.journal-officiel.gouv.fr%2F' ;
url := url +'webclipping_myjahiasite%2Fservlet%2Fwebclip%3Fjahia_url_web_clipping%3Dhttp%3A%2F%2Fassoc.journal-officiel.gouv.fr%2FListeResultJOA.asp&resetAppSession=true#field_1320' ;
FORMULAIRE.Text := HTMLFORMULAIRE.Text ;
HTMLPUBLIC.Text := IdHTTP.post(URL,FORMULAIRE) ;
HTMLPUBLIC.Lines.SaveToFile('robotasso.html');
showmessage('fin envoi post');
//decoupage
valeur := HTMLPUBLIC.Lines.Text;
//nb annonces
deb := '<p>Résultat de la recherche : <strong>';
fin := ' annonces</strong><br></p>';
presence := '';
tmp := decoupage.Decoupebalisebalise(valeur,deb,fin,presence);
if (tmp<>'') then
begin
nb := strtoint(tmp);
if nbpage=1 then
nbpage := (nb div 25) + 1;
//25 annonces par page
res := '';
if (nb > 24) then annonceparpage := 24 else annonceparpage := nb-1;
nb := nb - 25;
for i:=0 to annonceparpage do
begin
//decoupage d'une annonce
deb := '<div class="infos">';
fin := '</small></p>';
tmp := decoupage.Decoupebalisebalise(valeur,deb,fin,presence);
//decoupage nom asso
debnom := '<p>Association : <b>';
finnom := '.</b><br/>';
res := res + decoupage.Decoupebalisebalise(tmp,debnom,finnom,presence) + ' # ';
//decoupage activité asso
debnom := 'Activité(s) : <b>';
finnom := '</b><br/>';
res := res + decoupage.Decoupebalisebalise(tmp,debnom,finnom,presence) + ' # ';
//decoupage departement asso
debnom := 'Département (Région) : <b>';
finnom := '(';
res := res + decoupage.Decoupebalisebalise(tmp,debnom,finnom,presence) + ' # ';
//decoupage descriptif asso
debnom := 'Objet : </i>';
finnom := '<i>Siège social : </i>';
res := res + decoupage.Decoupebalisebalise(tmp,debnom,finnom,presence) + ' # ';
//decoupage adresse asso
debnom := '<i>Siège social : </i>';
finnom := '<i>';
adresse := decoupage.Decoupebalisebalise(tmp,debnom,finnom,presence);
//debnom := ' ';
//finnom := '.';
//ville := decoupage.Decoupebalisebalise(adresse,debnom,finnom,presence);;
//cp := copy(adresse,length(adresse)-length(ville)-13,5);
//adresse := copy(adresse,0,length(adresse)-length(ville)-19);
res := res + adresse + ' # ';// + cp + ' # ' + ville + ' # ';
//on enlève l'annonce découpé de la page
p := pos(deb,valeur)+ length(deb);
valeur := copy(valeur,p,(length(valeur)-p));
res := res + #13#10;
end;
Resultat.Text := res;
Resultat.Lines.SaveToFile(inttostr(mois)+inttostr(annee)+region+'.txt');
end;
page := page + 1;
//showmessage('fin decoupage');
until page > nbpage;
end; //fin for region
showmessage('fini');
end;
end. |
Partager