-
Notifications
You must be signed in to change notification settings - Fork 1
/
SearchUnit2.pas
339 lines (320 loc) · 13.3 KB
/
SearchUnit2.pas
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
unit SearchUnit2;
interface
uses Windows, MSHTML, activex, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Vcl.ComCtrls,
Dialogs, strutils, DBGrids, DB, MainUnit, idglobal, IdMessage, IdSMTP, Grids,
settings, ExtCtrls, IdCookieManager, IdBaseComponent, IniFiles,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, Vcl.StdCtrls, Vcl.OleCtrls,
idAntiFreeze, IdHeaderList,
SHDocVw, Vcl.Menus, JvAppStorage,
JvAppIniStorage, JvComponentBase, JvFormPlacement, JvDebugHandler,
IdUserPassProvider, IdExplicitTLSClientServerBase,
IdMessageClient, IdSASLPlain, IdSASL, IdSASLUserPass,
IdSASLLogin, IdLogBase,
IdLogDebug, IdCoder, IdCoderQuotedPrintable, IdIntercept,
UnitVars, UnitProgress, IdAntiFreezeBase, UnitSend;
type
TSearch2 = class(TOBJECT)
procedure Search2;
private
procedure DeleteImgs(listFiles: Tstrings);
public
{ Public declarations }
ST: SearchT;
StringGrd: TStringGrid;
Constructor Create;
Destructor Destroy; override;
end;
var
// FormSearch: TFormSearch;
iRow: integer;
implementation
uses parse, DBs, UnitDB;
constructor TSearch2.Create;
begin
StringGrd := TStringGrid.Create(nil);
end;
destructor TSearch2.Destroy;
begin
StringGrd.Free;
inherited;
end;
procedure TSearch2.Search2;
var
http: TIdHTTP;
CM: TIdCookieManager;
Data, Imgs, Parts: Tstrings;
StrPage, sPost, JsScript, urlStr, sAD3000, sPart, sdt: String;
r, n, p, l: integer;
iRepeat, iNum, iPageNum, iPage: integer;
sItem: TsItem2;
// iURI: tidURI;
pHTML: TParseHTML;
Headers: TIdHeaderList;
IdConnectionIntercept1: TIdConnectionIntercept;
begin
// =========================
iRepeat := 5; // êîë-âî ïîâòîðîâ îòïðàâêè ìûëà
iNum := 0; // êîë-âî çàï÷àñòåé
ST.Name := 'Ïîèñê ïî PC-NET';
FormMain.Log('Çàãðóçêà ïàðàìåòðîâ');
try
// ----- Start init vars--------
http := TIdHTTP.Create();
http.Intercept := TIdConnectionIntercept.Create();
http.ConnectTimeout := 30000;
http.ReadTimeout := 60000;
http.HTTPOptions := [hoKeepOrigProtocol, hoForceEncodeParams];
http.AllowCookies := true; // ðàçðåøèòü êóêè
http.HandleRedirects := false; // ïåðåàäðåñàöèÿ
Data := TStringList.Create;
CM := TIdCookieManager.Create(http);
http.AllowCookies := true;
http.CookieManager := CM;
http.HandleRedirects := true;
http.Request.Host := 'www.bl-recycle.jp';
{ TODO : äîáàâèòü ïàðñåð õîñòà èç url }
http.Request.UserAgent :=
'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10';
http.Request.Accept :=
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
http.Request.AcceptLanguage := 'ru,en-us;q=0.7,en;q=0.3';
http.Request.AcceptCharSet := 'windows-1251,utf-8;q=0.7,*;q=0.7';
http.Request.Referer := Sets.Opt2.SiteURL;
http.CookieManager.CookieCollection.AddClientCookies('AGREE01=1');
// http.CookieManager.GenerateClientCookies(Sets.Opt2.SiteLoginURL,false,Headers);
// ïðèíÿòèå ïðàâèë ñàéòà
// ïî÷åìó-òî íå ðàáîòàåò???
// ----- end init vars--------
FormMain.Log('÷èòàåì ñïèñîê ïàðàìåòðîâ çàïðîñà ê ñàéòó èç áàçû...');
Sets.LsURLs.Clear;
// ÷èòàåì ñïèñîê ïàðàìåòðîâ èç áàçû
if DM.Get2URLs(Sets.LsURLs) < 1 then
exit; // íåò çàïèñåé - âûõîäèì íà finally
FormMain.Log('Íàéäåíî: ' + inttostr(Sets.LsURLs.Count));
// Data.Add('AD1210=8297400');
Data.Add('AD1210=' + Sets.Opt2.Login1);
Data.Add('AD1510=' + Sets.Opt2.Login2);
Data.Add('AD1520=' + Sets.Opt2.Login3);
Data.Add('E_SID=PARTS_SB');
Data.Add('E_WORKID=ID0110');
Data.Add('AD9820=101010');
// AD1210=8297400&AD1510=ps84124q&AD1520=ab340u79&E_SID=PARTS_SB&E_WORKID=ID0110&AD9820=101010
FormMain.Log('Ëîãèíèìñÿ íà ñåðâåð'); { DONE 4 : Äîáàâèòü ÷òåíèå èç áàçû }
{ DONE : Ñäåëàòü îáåðòêó äëÿ îáðàáîòêè èñêëþ÷åíèé }
StrPage := PostHTTP('POST', http, Sets.Opt2.SiteLoginURL, Data);
if (StrPage = '') or (AnsiPos('Ã×̿Ū¥¨¥é¡¼', StrPage) > 0) then
exit;
// òóò äîëæíà áûòü ñòðàíèöà ñ òåêñòîì ïðàâèë è äâóìÿ êíîïêàìè
FormMain.Log('íàæèìàåì íà êíîïêó...');
Data.Clear;
Data.Add('E_WORKID=ID0120');
Data.Add('turl=sbe');
FormMain.Log('óñïåøíî ïðîøëè àâòîðèçàöèþ.');
sleep(2000);
sItem := TsItem2.Create;
FormMain.Log('âûáèðàåì îáëàñòü...');
{ DONE : Ñäåëàòü îáåòðêó äëÿ îáðàáîòêè èñêëþ÷åíèé }
StrPage := PostHTTP('POST', http, Sets.Opt2.SiteLoginURL, Data);
if StrPage = '' then
exit;
l := 0;
// http://www.bl-recycle.jp/servlet/EW3SAS_WEB_PS
// AD3000M=-1&AD3000=1000&AD3500M=-1&AD3010M=0&AD3010=1010&AD3530M=-1&AD1030I=2&AD5400M=-1&AD4020M=0&AD4100M=0&E_WORKID=ID1410&ORDER01=AD4610B&ORDER01D=1&ORDER02=AD4060B&ORDER02D=1&ORDER03=AD4020B&ORDER03D=1&CHARGE=0
while (l < Sets.LsURLs.Count) do // öèêë çàïðîñîâ ïî ìàøèíàì
begin
Data.Clear;
sPost := AnsiDequotedStr(Sets.LsURLs.Strings[l], '"');
if sPost = '' then
begin
FormMain.Log('Îøèáêà!!! Çàïðîñ ìàøèíû - ïóñòîé!', 3);
Break;
end;
sItem.part1 := ReplaceText(sPost, '&', EOL);
Data.Text := sItem.part1;
sleep(2000);
{ DONE : Ñäåëàòü îáåòðêó äëÿ îáðàáîòêè èñêëþ÷åíèé }
FormMain.Log('èäåì ïî ññûëêå ê çàï÷àñòÿì...');
StrPage := PostHTTP('POST', http, Sets.Opt2.SiteLoginURL, Data);
// StrPage := '';
// ListPArts.Items.Clear;
// for i := 0 to ListView1.Items.Count - 1 do
// öèêë çàï÷àñòåé â ìàøèíå
if StrPage <> '' then
begin
Data.Clear;
// 'AD3000M=-1&AD3000=3000&AD3500M=-1&AD3010M=0&AD3010=3020&AD3530M=-1&AD1030I=2&AD5400M=-1&AD4020M=0&AD5510M=0&E_WORKID=ID1410&ORDER01=AD4610B&ORDER01D=1&ORDER02=AD4060B&ORDER02D=1&ORDER03=AD4020B&ORDER03D=1&CHARGE=0';
//
sPost := AnsiDequotedStr(Sets.LsURLs.Strings[l + 1], '"');
if sPost = '' then
begin
FormMain.Log('Îøèáêà!!! Çàïðîñ äåòàëè - ïóñòîé!', 3);
Break;
end;
FormMain.Log('÷èòàåì ñòðîêó äåòàëè: ' + inttostr(l));
FormMain.Log(sPost);
sItem.part2 := ReplaceText(sPost, '&', EOL);
sleep(2000);
iPage := 99999; // ïðèçíàê íàëè÷èÿ ñòðàíèö â ñïèñêå çàï÷àñòåé
iPageNum := 1; // íîìåð òåêóùåé ñòðàíèöû
FormMain.Log('Ãðóçèì ñïèñîê çàï÷àñòåé ïî ïàðàìåòðàì...');
{ DONE : Ñäåëàòü îáåðòêó äëÿ îáðàáîòêè èñêëþ÷åíèé }
while (iPage > 0) and (StrPage <> '') do
begin
Data.Text := sItem.part2;
sAD3000 := Data.Values['AD3000'];
if iPageNum > 1 then
begin // ïåðâóþ ïðîøëè - ïàðàìåòðû äëÿ ïîñëåäóþùèõ ñòðàíèö
Data.Values['E_WORKID'] := 'ID1411';
Data.Values['PAGENO'] := inttostr(iPageNum);
end;
StrPage := PostHTTP('POST', http, Sets.Opt2.SiteLoginURL, Data);
{ TODO 3 : Äîáàâèòü ïàðñåð ïðîäîëæåíèå ñòðàíèöû êîãäà çàï÷àñòåé > 20 }
if StrPage <> '' then
begin
FormMain.Log('Ïàðñèì òàáëèöó äëÿ StringGrid');
pHTML := TParseHTML.Create;
iNum := pHTML.ParseTableSR2(StrPage, StringGrd);
// iNum := ParseTable1(StrPage, StringGrd); { TODO : Ïðîâåðèòü!"""" }
inc(iPageNum); // óâåëè÷èâàåì íîìåð ñòðàíöû òàê êàê îíà óæå ñêà÷àíà
if iPage > iNum then
iPage := iNum;
// åñëè ïðîõîä ïåðâûé ðàç - òî îáíîâëåíèå êîë-âà çàï÷àñòåé äëÿ ïîäñ÷åòà ñòðàíèö
FormMain.Log('Êîë-âî çàï÷àñòåé: ' + inttostr(iNum) + ' ñòðàíèöà: ' +
inttostr(iPageNum));
if iNum = 0 then
Break; // ïóñòàÿ òàáëèöà - íå÷åãî ïàðñèòü
{ DONE : Çà÷åì òóò ïàðñèòü òàáëèöó??? }
FormMain.Log('Ïàðñèì áëîê ñêðèïòà...');
JsScript := GetScript(StrPage);
FormMain.Log('ïîèñê ññûëêè íà ÿâó-ñêðèïò:' + JsScript);
if pHTML.ParserScript(Data, JsScript, http) then
begin
FormMain.Log('çàïîëíÿåì ïàðàìåòðû èç ñêðèïòà...');
Parts := TStringList.Create;
if GetPartsNum(StrPage, Parts) then
// áåðåì íîìåðà çàï÷àñòåé èç ñòðàíèöû
begin
FormMain.Log('Íàéäåíî çàï÷àñòåé: ' + inttostr(Parts.Count));
ST.Status := 'Íàéäåíî çàï÷àñòåé: ' + inttostr(Parts.Count);
iPage := iPage - Parts.Count;
for r := 0 to Parts.Count - 1 do
begin
sItem.partnumber := Parts.Strings[r]; // äåòàëüêà íàéäåíà
FormMain.Log('ïðîâåðÿåì - åñòü ëè çàï÷àñòü â áàçå...' +
inttostr(r + 1));
if DM.GetItem2ID(sItem) < 0 then
// ïðîâåðÿåì åñòü ëè äåòàëü â áàçå, èíà÷å äîáàâëÿåì åå â áàçó
begin
urlStr := 'http://' + http.Request.Host;
FormMain.Log('ñîáèðàåì URL íîâîé çàï÷àñòè ïî ÷àñòÿì...');
for n := 1 to Data.Count - 1 do
begin
sdt := AnsiDequotedStr(Data.Strings[n], '''');
if AnsiContainsStr(sdt, 'target') then
sdt := sItem.partnumber;
if AnsiContainsStr(sdt, 'document') then
sdt := sAD3000;
urlStr := urlStr + sdt;
// Data.Strings[Data.IndexOf('target')] := Parts.Strings[r];
// çàïîëíÿåì POST çàïðîñ íà êàæäûé íîìåð çàï÷àñòè
{ DONE 3 : Çàìåíèòü íà ðåàëüíûé íîìåð! }
end;
try
FormMain.Log('Ãðóçèì çàï÷àñòü: ' + urlStr);
{ DONE : Ñäåëàòü îáåðòêó äëÿ îáðàáîòêè èñêëþ÷åíèé }
StrPage := getHTTP(http, urlStr);
{ TODO : Ìîæíî ëè cäåëàòü ïðîâåðêó çàï÷àñòè â áàçå ïî ññûëêå? }
http.Request.Referer := urlStr;
http.Request.Accept :=
'image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5';
FormMain.Log('Ãðóçèì êàðòèíêè äëÿ ¹' + sItem.partnumber);
Imgs := TStringList.Create;
GetImagesFromHTML(http, StrPage, Imgs,
'http://' + http.Request.Host, sItem.partnumber);
{ ListPArts.Items.Add(StrPage);
ListPArts.Items.SaveToFile(ExtractFilePath(Application.ExeName)
+ 'part' + Parts.Strings[r] + '.htm'); }
FormMain.Log('Îòñûëàåì ïèñüìî.');
while not SendMail2(FormSettings.EdSender.Text,
FormSettings.EdRecipients.Text,
FormSettings.EdSMTPHost.Text,
FormSettings.edSMTPport.Text,
'PS-NET - Ïîèñê àâòîçàï÷àñòåé ïî ïàðàìåòðàì. ID' +
sItem.partnumber, FormSettings.EdMailLogin.Text,
FormSettings.EdMailPass.Text,
// '[email protected]', '[email protected]',
// 'smtp.yandex.ru', '25', 'New parts.', 'a.cia', 'Ferdy4to)',
StrPage, Imgs) and (iRepeat > 0) do
begin
FormMain.Log
('Îøèáêà îòïðàâêè ïèñüìà! Æäåì è ïîâòîðÿåì...');
Application.ProcessMessages;
sleep(4000);
dec(iRepeat);
end;
FormMain.Log('Ïèñüìî îòïðàâëåíî.');
DeleteImgs(Imgs);
FormMain.Log('Äîáàâëÿåì çàï÷àñòü ¹' + sItem.partnumber +
' â áàçó.');
DM.Add2Item(sItem);
sleep(3000);
{ DONE : Ïðîâåðèòü ïî÷òó }
{ TODO : Äîáàâèòü ðàáîòó ñ áàçîé çàï÷àñòåé äëÿ êîíòðîëÿ íîâûõ }
finally
{ TODO :
Íåïðàâèëüíàÿ îáðàáîòêà îøèáêè.
Ñðàáîòêà îøèáêè â çàãðóçêå êàðòèíîê ïðåðûâàåò âåñü öèêë. }
FormMain.Log('ãîòîâî çàï÷àñòåé: ' + inttostr(r + 1) +
' èç ' + inttostr(Parts.Count));
end;
end;
end;
FormMain.Log('Çàêîí÷èëè.');
end;
Parts.Free;
end;
pHTML.Free;
end;
if StrPage = '' then
FormMain.Log('Îøèáêà: StrPage = ïóñòîé !', 2);
if iPage < 1 then
FormMain.Log('Îøèáêà: iPage < 1 !', 2);
end;
end
else
FormMain.Log('ïóñòîé îòâåò ñåðâåðà!', 2);
l := l + 2; //
end;
// WB_LoadHTML(WebBrowser1,StrPage);
finally
FormMain.Log('Ïðèáèðàåìñÿ...');
sItem.Free;
Data.Free;
CM.Free;
http.Free;
ST.FlgCancel := true;
end;
FormMain.Log('Ãîòîâî.');
{ if Pos('<input class="logoutlj_hidden" id="user" name="user" type="hidden" value="'+Edit1.Text,StrPage) <> 0 then
FormMain.Log('Àâòîðèçàöèÿ ïðîøëà óñïåøíî',1)
else
FormMain.Log('Àâòîðèçàöèÿ ïðîâàëèëàñü',3); }
// Memo1.Lines.Text := StrPage;
end;
procedure TSearch2.DeleteImgs(listFiles: Tstrings);
var
i: integer;
begin
i := 0;
while i < listFiles.Count do
begin
try
if FileExists(listFiles.Strings[i]) then
DeleteFile(listFiles.Strings[i]); // óäàëèòü ôàéë êàðòèíêè
except
end;
inc(i);
end;
end;
end.