-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadSyncFavorites.pas
513 lines (400 loc) · 12.7 KB
/
ThreadSyncFavorites.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
unit ThreadSyncFavorites;
interface
uses
ActiveX, Classes, SysUtils, ShlObj, Windows, XMLDom, XMLIntf, MSXMLDom, XMLDoc,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, Forms,
IdAntiFreezeBase, IdAntiFreeze, StrUtils, HTTPApp, Variants;
type
ThreadFavoritesSync = class(TThread)
private
procedure InitIndyObject;
procedure FreeIndyObject;
{ Private declarations }
protected
procedure Execute; override;
procedure SyncFormCaptions();//完成后名字的调整
procedure SwitchSyncTab();
procedure InitSystemVar();//初始化系统变量
procedure GetUserIEFavorites();//获取IE收藏夹
procedure FreeSystemVar();
procedure FreePidl(pidl: PItemIDList);
procedure ProcessFavorData();//分析处理收藏夹数据
procedure SyncProcess();//线程同步实时处理数据
procedure APIGetUserTocken();//获取用户TOKEN
procedure GetAPIFavorResult();//获取同步结果
var
IdHttpMain:TIdHTTP; //HTTP RFC
XmlDocumentMain:TXMLDocument; //XML对象
IdAntiFreezeMain: TIdAntiFreeze;
public
constructor Create(Suspended: Boolean; IsShowForm: Boolean);
destructor Destroy; override;
end;
var iImport, iCurrent, iSum, iFinish, iTotal: Integer;
TASK_ID: string = '';
implementation
uses MD5, FormImportSync, FrameImportSyncStepTwo, FunctionCommon, SystemConfig;
var slFavor: TStrings;
NeedShowForm: Boolean;
{
Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure ThreadSyncFavorites.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
or
Synchronize(
procedure
begin
Form1.Caption := 'Updated in thread via an anonymous method'
end
)
);
where an anonymous method is passed.
Similarly, the developer can call the Queue method with similar parameters as
above, instead passing another TThread class as the first parameter, putting
the calling thread in a queue with the other thread.
}
{ ThreadSyncFavorites }
{*******************************************************************************}
constructor ThreadFavoritesSync.Create(Suspended, IsShowForm: Boolean);
begin
inherited Create(Suspended);
NeedShowForm:=IsShowForm;
end;
{*******************************************************************************}
destructor ThreadFavoritesSync.Destroy;
begin
inherited Destroy;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.Execute;
begin
{ Place thread code here }
FreeOnTerminate:=True;
InitSystemVar;
GetUserIEFavorites;
APIGetUserTocken;
ProcessFavorData; //分析数据
GetAPIFavorResult; //获取结果
FreeSystemVar;
if NeedShowForm then begin
Synchronize(SyncFormCaptions);
Sleep(300);
Synchronize(SwitchSyncTab);//最终跳转
end;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.FreePidl(pidl: PItemIDList);
var
allocator: IMalloc;
begin
if Succeeded(SHGetMalloc(allocator)) then
begin
allocator.Free(pidl);
{$IFDEF VER100}
allocator.Release;
{$ENDIF}
end;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.FreeSystemVar;
begin
FreeAndNil(slFavor);
FreeAndNil(IdAntiFreezeMain);
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.GetAPIFavorResult;
var
Param:TStringList;
RStream:TStringStream;
XmlResultInfo, TASK_SIGN: string;
fNode, tNode:IXMLNode;
begin
if TASK_ID <> '' then begin
TASK_SIGN:= StrToMD5('app_id' + IntToStr(API_ID) + 'queue_id' + TASK_ID
+ 'uid' + IntToStr(USER_ID) + API_KEY + API_USER_TOCKEN, 9);
InitIndyObject;//初始化HTTP对象
//XML
XMLDocumentMain:=TXMLDocument.Create(Application);
XMLDocumentMain.Active:=False;
XmlResultInfo:='';
//URL参数
Param:=TStringList.Create;
RStream:=TStringStream.Create('',TEncoding.UTF8); //UTF-8
//提交字段
Param.Add('app_id=' + IntToStr(API_ID));
Param.Add('uid='+ IntToStr(USER_ID));
Param.Add('queue_id=' + TASK_ID);
Param.Add('sign=' + TASK_SIGN);
try
try
IdHttpMain.Post(API_URL_SYNC_PROGRESS, Param, RStream);
except on E: Exception do begin
Log(E.Message);
end;
end;
finally
XmlResultInfo:= RStream.DataString;
end;
FreeIndyObject; //释放HTTP对象
FreeAndNil(Param);
FreeAndNil(RStream);
//获取用户Token
if (XmlResultInfo <> '') then begin
DebugTrace('获取收藏夹同步结果接口', XmlResultInfo);
XmlDocumentMain.XML.Text:= XmlResultInfo;
try
try
XmlDocumentMain.Active:= True;
except on E: Exception do begin
Log(E.Message);
end;
end;
finally
try
tNode:=XmlDocumentMain.DocumentElement.ChildNodes.Get(4);
fNode:=tNode;
tNode:=tNode.ChildNodes.Get(1); //Total
fNode:=fNode.ChildNodes.Get(3); //Finish Done
except on E: Exception do begin
Log(E.Message);
end;
end;
iTotal:= StrToInt(tNode.NodeValue);
iFinish:= StrToInt(tNode.NodeValue);
//DebugTrace('总数='+tNode.NodeValue+'|完成='+fNode.NodeValue);
end;
end;
//释放XML对象
FreeAndNil(XmlDocumentMain);
end;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.GetUserIEFavorites;
var
pidl: PItemIDList;
FavPath: array[0..MAX_PATH] of Char;
begin
if Succeeded(ShGetSpecialFolderLocation(Handle, CSIDL_FAVORITES, pidl)) then
begin
if ShGetPathfromIDList(pidl, FavPath) then
slFavor:=GetIEFavourites(StrPas(FavPath));
iSum:= slFavor.Count;
// The calling application is responsible for freeing the PItemIDList-pointer
// with the Shell's IMalloc interface
FreePIDL(pidl);
end;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.APIGetUserTocken;
var
Param:TStringList;
RStream:TStringStream;
XmlUserInfo: string;
Node:IXMLNode;
UserSign: RawByteString;
begin
InitIndyObject;//初始化HTTP对象
//XML
XMLDocumentMain:=TXMLDocument.Create(Application);
XMLDocumentMain.Active:=False;
XmlUserInfo:='';
//URL参数
Param:=TStringList.Create;
RStream:=TStringStream.Create('',TEncoding.UTF8); //UTF-8
//中文处理
DebugTrace('签名:=', API_USER_SIGN);
UserSign:=UTF8Encode(API_USER_SIGN);
SetCodePage(UserSign, 0, False);//重要注册处理
UserSign:=StrToMD5(UserSign, 9);
DebugTrace('签名MD5:=', UserSign);
//提交字段
Param.Add('app_id=' + IntToStr(API_ID));
Param.Add('uid='+ IntToStr(USER_ID));
Param.Add('username=' + USER_NAME);
Param.Add('login_key=' + USER_LOGIN_KEY);
Param.Add('sign=' + UserSign);
try
try
IdHttpMain.Post(API_URL_USERINFO, Param, RStream);
except on E: Exception do begin
Log(E.Message);
end;
end;
finally
XmlUserInfo:= RStream.DataString;
end;
FreeIndyObject; //释放HTTP对象
FreeAndNil(Param);
FreeAndNil(RStream);
//获取用户Token
if (XmlUserInfo <> '') then begin
DebugTrace('获取用户TOKEN接口', XmlUserInfo);
XmlDocumentMain.XML.Text:= XmlUserInfo;
try
try
XmlDocumentMain.Active:= True;
except on E: Exception do begin
Log(E.Message);
end;
end;
finally
try
Node:=XmlDocumentMain.DocumentElement.ChildNodes.Get(4);
Node:=Node.ChildNodes.Get(0);
except on E: Exception do begin
Log(E.Message);
end;
end;
API_USER_TOCKEN:= Node.NodeValue;
end;
end;
//释放XML对象
FreeAndNil(XmlDocumentMain);
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.FreeIndyObject;
begin
//释放HTTP访问对象
FreeAndNil(IdHttpMain);
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.InitIndyObject;
begin
//INDY
IdHttpMain := TIdHTTP.Create(nil);
IdHttpMain.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4 AlexaToolbar/alxg-3.1';
IdHttpMain.HandleRedirects := True;
IdHttpMain.ReadTimeout := 5000;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.InitSystemVar;
begin
iImport:=0;
iSum:=0;
iCurrent:=0;
iFinish:=0;
iTotal:=0;
slFavor:=TStringList.Create;
if NeedShowForm then frmImportSync.FrameSyncStep2.ProgressBarSync.Percent:=0;
//动态生成SIGN签名
API_USER_SIGN:='app_id' + IntToStr(API_ID) + 'login_key' + USER_LOGIN_KEY
+ 'uid' + IntToStr(USER_ID) + 'username' + USER_NAME
+ API_KEY;
IdAntiFreezeMain:=TIdAntiFreeze.Create(nil);
IdAntiFreezeMain.OnlyWhenIdle:= False;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.ProcessFavorData;
var I:integer;
tempUrl, XmlFavorInfo, FAV_DATA, FAV_SIGN: string;
Param:TStringList;
RStream:TStringStream;
Node:IXMLNode;
begin
FAV_DATA:='';
XmlFavorInfo:='';
for I := 0 to iSum - 1 do begin
iCurrent:=I;
//判断只有TAOBAO|TMALL的站点启用分析
tempUrl:= HTTPEncode(slFavor.Strings[I]);//URL
if ((Pos('taobao.com', LowerCase(tempUrl))<>0) or (Pos('tmall.com', LowerCase(tempUrl))<>0)) then begin
Inc(iImport); //处理多少个收藏商品
FAV_DATA:= FAV_DATA + tempUrl + '{|}' + '{||}';
end;
Synchronize(SyncProcess);
end;
if FAV_DATA<>'' then
FAV_DATA:= LeftStr(FAV_DATA, Length(FAV_DATA)-4);
//DebugTrace('收藏夹数据', FAV_DATA);
//动态签名
FAV_SIGN:= StrToMD5('app_id' + IntToStr(API_ID) + 'favor_data' + FAV_DATA
+ 'uid' + IntToStr(USER_ID) + API_KEY + API_USER_TOCKEN, 9);
if ((iImport <> 0) and (FAV_DATA <> '')) then begin
//批量传送收藏夹数据
InitIndyObject;
//URL参数
Param:=TStringList.Create;
RStream:=TStringStream.Create('',TEncoding.UTF8); //UTF-8
//提交字段
Param.Add('app_id=' + IntToStr(API_ID));
Param.Add('uid='+ IntToStr(USER_ID));
Param.Add('favor_data=' + FAV_DATA);
Param.Add('sign=' + FAV_SIGN);
try
try
IdHttpMain.Post(API_URL_SYNC_FAVOR, Param, RStream);
except on E: Exception do begin
Log(E.Message);
end;
end;
finally
XmlFavorInfo:= RStream.DataString;
end;
FreeIndyObject; //释放HTTP对象
FreeAndNil(Param);
FreeAndNil(RStream);
//获取任务ID
if (XmlFavorInfo <> '') then begin
//XML
XMLDocumentMain:=TXMLDocument.Create(Application);
XMLDocumentMain.Active:=False;
XmlDocumentMain.XML.Text:= XmlFavorInfo;
DebugTrace('获取任务ID接口', XmlFavorInfo);
try
try
XmlDocumentMain.Active:= True;
except on E: Exception do begin
Log(E.Message);
end;
end;
finally
try
Node:=XmlDocumentMain.DocumentElement.ChildNodes.Get(4);
Node:=Node.ChildNodes.Get(1);
except on E: Exception do begin
Log(E.Message);
end;
end;
TASK_ID:= VarToStr(Node.NodeValue); //获取任务ID
end;
end;
FreeAndNil(XmlDocumentMain);
end;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.SwitchSyncTab;
begin
if iImport = 0 then begin
frmImportSync.FrameSyncStep2.PageControlStepSync.ActivePageIndex:=2;
end else begin
frmImportSync.FrameSyncStep2.PageControlStepSync.ActivePageIndex:=1;
end;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.SyncFormCaptions;
begin
frmImportSync.FrameSyncStep2.ProgressBarSync.Percent:=100;
frmImportSync.FrameSyncStep2.lblProcess.Caption:= IntToStr(iImport);
frmImportSync.FrameSyncStep2.lblRet1.Caption:=IntToStr(iImport);
//frmImportSync.btnOk.Visible:=False;
frmImportSync.btnSmall.Caption:='完成';
frmImportSync.Caption:= '导入成功';
frmImportSync.lblTitleBase.Caption:='导入成功';
frmImportSync.FrameSyncStep2.SetLastLabelPostion;
end;
{*******************************************************************************}
procedure ThreadFavoritesSync.SyncProcess;
begin
if iSum <> 0 then begin
if NeedShowForm then begin
frmImportSync.FrameSyncStep2.ProgressBarSync.Percent:= iCurrent*100 div iSum;
end;
end;
end;
{*******************************************************************************}
end.