forked from cool2528/baiduCDP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PanParse.cpp
1579 lines (1526 loc) · 53.7 KB
/
PanParse.cpp
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "PanParse.h"
#include <sys/timeb.h>
#include <dbghelp.h>
#include <shlwapi.h>
#include <boost/regex.hpp>
#include <boost/format.hpp>
#include "base64.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/pointer.h"
#include "resource.h"
#pragma comment(lib,"dbghelp.lib")
#pragma comment(lib,"shlwapi.lib")
std::string CBaiduParse::m_vcCodeUrl;
std::string CBaiduParse::m_VerCode;
std::string CBaiduParse::m_SharePass;
DWORD CBaiduParse::WriteFileBuffer(std::string szFileNmae, PVOID pFileBuffer, DWORD dwFileSize)
{
DWORD dwStates = -1;
std::string szFilePath, strName;
size_t npos = -1;
if (szFileNmae.empty())
return dwStates;
npos = szFileNmae.rfind("\\");
if (npos != std::string::npos)
{
szFilePath = szFileNmae.substr(0, npos + 1);
if (!szFilePath.empty())
{
if (MakeSureDirectoryPathExists(szFilePath.c_str()))
{
HANDLE hFile = INVALID_HANDLE_VALUE;
DWORD dwError = NULL;
hFile = CreateFileA(szFileNmae.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
dwError = ::GetLastError();
if (hFile != INVALID_HANDLE_VALUE)
{
::WriteFile(hFile, pFileBuffer, dwFileSize, &dwStates, NULL);
::CloseHandle(hFile);
}
}
}
}
return dwStates;
}
REQUESTINFO CBaiduParse::ParseBaiduAddr(const std::string strUrl, std::string& strCookies)
{
std::string strWebUrl = strUrl;
int nResult = IsPassWordShareUrl(strWebUrl, strCookies);
HttpRequest BaiduHttp;
BAIDUREQUESTINFO BaiduInfo;
REQUESTINFO strRealUrl;
std::string strDownloadUrl;
std::string strResponseText;
BaiduHttp.SetRequestCookies(strCookies);
BaiduHttp.SetHttpRedirect(true);
BaiduHttp.Send(GET, strUrl);
strResponseText = BaiduHttp.GetResponseText();
strResponseText = Utf8_To_Gbk(strResponseText.c_str());
if (strResponseText.empty())
return strRealUrl;
std::string strJson = GetTextMid(strResponseText, "yunData.setData(", ");");
BaiduInfo = GetBaiduInfo(strJson);
strDownloadUrl = "https://pan.baidu.com/api/sharedownload?sign=" + BaiduInfo.sign + \
"×tamp=" + BaiduInfo.timestamp + "&channel=chunlei&web=1&app_id=" + BaiduInfo.app_id + \
"&bdstoken=" + BaiduInfo.bdstoken + "&logid=" + GetLogid() + "&clienttype=0";
strCookies = BaiduHttp.MergeCookie(strCookies, BaiduHttp.GetResponCookie());
BaiduInfo.BDCLND = GetTextMid(strCookies, "BDCLND=", ";");
if (BaiduInfo.BDCLND.empty())
{
strJson = "encrypt=0&product=share&uk=%1%&primaryid=%2%&fid_list=%%5B%3%%%5D&path_list=";
strJson = str(boost::format(strJson) % BaiduInfo.uk % BaiduInfo.shareid % BaiduInfo.fs_id);
}
else
{
strJson = "encrypt=0&extra=%%7B%%22sekey%%22%%3A%%22%1%%%22%%7D&product=share&uk=%2%&primaryid=%3%&fid_list=%%5B%4%%%5D&path_list=";
strJson = str(boost::format(strJson) % BaiduInfo.BDCLND % BaiduInfo.uk % BaiduInfo.shareid % BaiduInfo.fs_id);
}
/*准备提交数据获取真实URL地址*/
BaiduHttp.SetRequestHeader("Content-Type", " application/x-www-form-urlencoded; charset=UTF-8");
BaiduHttp.SetRequestHeader("Accept", " application/json, text/javascript, */*; q=0.01");
BaiduHttp.SetRequestCookies(strCookies);
BaiduHttp.Send(POST, strDownloadUrl, strJson);
strJson = BaiduHttp.GetResponseText();
rapidjson::Document dc;
dc.Parse(strJson.c_str());
if (!dc.IsObject())
return strRealUrl;
int nError = -1;
if (dc.HasMember("errno") && dc["errno"].IsInt())
nError = dc["errno"].GetInt();
if (!nError)
{
if (dc.HasMember("list") && dc["list"].IsArray())
{
for (auto &v : dc["list"].GetArray())
{
if (v.HasMember("dlink") && v["dlink"].IsString())
strRealUrl.strDownloadUrl = v["dlink"].GetString();
}
}
}
else
{
//这里需要输入验证码了
int index = 0;
do
{
if (index>5)
break;
strRealUrl.strDownloadUrl = GetBaiduAddr(BaiduInfo, strCookies);
index++;
} while (strRealUrl.strDownloadUrl.empty());
}
// BaiduHttp.Send(HEAD, strRealUrl.strDownloadUrl);
// strRealUrl.strDownloadUrl = GetTextMid(BaiduHttp.GetallResponseHeaders(), "Location: ", "\r\n");
strRealUrl.strFileName = BaiduInfo.server_filename;
strRealUrl.strCookies = strCookies;
return strRealUrl;
}
REQUESTINFO CBaiduParse::ParseBaiduAddrEx(BAIDUREQUESTINFO& BaiduInfo, std::string& strCookies)
{
HttpRequest BaiduHttp;
REQUESTINFO strRealUrl;
std::string strDownloadUrl;
std::string strJson;
BaiduInfo.app_id = BaiduInfo.app_id.empty() ? "250528" : BaiduInfo.app_id;
strDownloadUrl = "https://pan.baidu.com/api/sharedownload?sign=" + BaiduInfo.sign + \
"×tamp=" + BaiduInfo.timestamp + "&channel=chunlei&web=1&app_id=" + BaiduInfo.app_id + \
"&bdstoken=" + BaiduInfo.bdstoken + "&logid=" + GetLogid() + "&clienttype=0";
BaiduInfo.BDCLND = GetTextMid(strCookies, "BDCLND=", ";");
if (BaiduInfo.BDCLND.empty())
{
strJson = "encrypt=0&product=share&uk=%1%&primaryid=%2%&fid_list=%%5B%3%%%5D&path_list=";
strJson = str(boost::format(strJson) % BaiduInfo.uk % BaiduInfo.shareid % BaiduInfo.fs_id);
}
else
{
strJson = "encrypt=0&extra=%%7B%%22sekey%%22%%3A%%22%1%%%22%%7D&product=share&uk=%2%&primaryid=%3%&fid_list=%%5B%4%%%5D&path_list=";
strJson = str(boost::format(strJson) % BaiduInfo.BDCLND % BaiduInfo.uk % BaiduInfo.shareid % BaiduInfo.fs_id);
}
/*准备提交数据获取真实URL地址*/
BaiduHttp.SetRequestHeader("Content-Type", " application/x-www-form-urlencoded; charset=UTF-8");
BaiduHttp.SetRequestHeader("Accept", " application/json, text/javascript, */*; q=0.01");
BaiduHttp.SetRequestCookies(strCookies);
BaiduHttp.Send(POST, strDownloadUrl, strJson);
strJson = BaiduHttp.GetResponseText();
rapidjson::Document dc;
dc.Parse(strJson.c_str());
if (!dc.IsObject())
return strRealUrl;
int nError = -1;
if (dc.HasMember("errno") && dc["errno"].IsInt())
nError = dc["errno"].GetInt();
if (!nError)
{
if (dc.HasMember("list") && dc["list"].IsArray())
{
for (auto &v : dc["list"].GetArray())
{
if (v.HasMember("dlink") && v["dlink"].IsString())
strRealUrl.strDownloadUrl = v["dlink"].GetString();
}
}
}
else
{
//这里需要输入验证码了
int index = 0;
do
{
if (index>5)
break;
strRealUrl.strDownloadUrl = GetBaiduAddr(BaiduInfo, strCookies);
index++;
} while (strRealUrl.strDownloadUrl.empty());
}
// BaiduHttp.Send(HEAD, strRealUrl.strDownloadUrl);
// strRealUrl.strDownloadUrl = GetTextMid(BaiduHttp.GetallResponseHeaders(), "Location: ", "\r\n");
strRealUrl.strFileName = BaiduInfo.server_filename;
strRealUrl.strCookies = strCookies;
return strRealUrl;
}
std::string CBaiduParse::AnalysisShareUrlInfo(const std::string strUrl, std::string& strCookie)
{
std::string strWebUrl = strUrl;
std::string strResultJson;
int nResult = IsPassWordShareUrl(strWebUrl, strCookie);
HttpRequest BaiduHttp;
BAIDUREQUESTINFO BaiduInfo;
ZeroMemory(&BaiduInfo, sizeof(BAIDUREQUESTINFO));
REQUESTINFO strRealUrl;
std::string strDownloadUrl;
std::string strResponseText;
BaiduHttp.SetRequestCookies(strCookie);
BaiduHttp.SetHttpRedirect(true);
BaiduHttp.Send(GET, strUrl);
strResponseText = BaiduHttp.GetResponseText();
strResponseText = Utf8_To_Gbk(strResponseText.c_str());
if (strResponseText.empty())
return strResultJson;
std::string strJson = GetTextMid(strResponseText, "yunData.setData(", ");");
BaiduInfo = GetBaiduInfo(strJson);
// if (BaiduInfo.n_isdir)
// {
// strResultJson = GetBaiduShareFileListInfo(Utf8_To_Gbk(BaiduInfo.strPath.c_str()), strCookie, BaiduInfo);
// }
// else
// {
rapidjson::Document docjson;
docjson.SetObject();
rapidjson::Value array_list(rapidjson::kArrayType);
rapidjson::Value itemObject(rapidjson::kObjectType);
rapidjson::Value isdir(rapidjson::kNumberType);
isdir.SetInt(BaiduInfo.n_isdir);
itemObject.AddMember(rapidjson::StringRef("isdir"), isdir, docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("cookie"), rapidjson::StringRef(strCookie.c_str()), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("name"), rapidjson::StringRef(BaiduInfo.server_filename.c_str()), docjson.GetAllocator());
std::string strFilesize = BaiduInfo.server_filename.empty() ? "--" : GetFileSizeType(BaiduInfo.nSize);
rapidjson::Value FileSize(rapidjson::kStringType);
FileSize.SetString(strFilesize.c_str(), strFilesize.length(), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("Size"), FileSize, docjson.GetAllocator());
rapidjson::Value nCategory(rapidjson::kNumberType);
nCategory.SetInt(BaiduInfo.ncategory);
itemObject.AddMember(rapidjson::StringRef("nCategory"), nCategory, docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("path"), rapidjson::StringRef(BaiduInfo.strPath.c_str()), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("FileType"), rapidjson::StringRef(::PathFindExtensionA(Utf8_To_Gbk(BaiduInfo.server_filename.c_str()).c_str())), docjson.GetAllocator());
std::string strTime = BaiduInfo.server_filename.empty() ? "-" :timestampToDate(BaiduInfo.server_time);
rapidjson::Value FileTime(rapidjson::kStringType);
FileTime.SetString(strTime.c_str(), strTime.length(), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("ChangeTime"), FileTime, docjson.GetAllocator());
rapidjson::Value fs_id(rapidjson::kStringType);
fs_id.SetString(BaiduInfo.fs_id.c_str(), BaiduInfo.fs_id.length(), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("fs_id"), fs_id, docjson.GetAllocator());
array_list.PushBack(itemObject, docjson.GetAllocator());
rapidjson::Value info(rapidjson::kObjectType);
info.AddMember(rapidjson::StringRef("uk"), rapidjson::StringRef(BaiduInfo.uk.c_str()), docjson.GetAllocator());
info.AddMember(rapidjson::StringRef("bdstoken"), rapidjson::StringRef(BaiduInfo.bdstoken.c_str()), docjson.GetAllocator());
info.AddMember(rapidjson::StringRef("sign"), rapidjson::StringRef(BaiduInfo.sign.c_str()), docjson.GetAllocator());
info.AddMember(rapidjson::StringRef("shareid"), rapidjson::StringRef(BaiduInfo.shareid.c_str()), docjson.GetAllocator());
info.AddMember(rapidjson::StringRef("timestamp"), rapidjson::StringRef(BaiduInfo.timestamp.c_str()), docjson.GetAllocator());
docjson.AddMember(rapidjson::StringRef("info"), info, docjson.GetAllocator());
docjson.AddMember(rapidjson::StringRef("data"), array_list, docjson.GetAllocator());
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
docjson.Accept(writer);
strResultJson = buffer.GetString();
//WriteFileBuffer(".\\1.txt", (PVOID)strResultJson.c_str(), strResultJson.length());
//}
return strResultJson;
}
int CBaiduParse::IsPassWordShareUrl(std::string& strUrl, std::string& strCookies)
{
int nResult = -1;
if (strUrl.empty() || strCookies.empty())
{
return nResult;
}
HttpRequest BaiduHttp;
int nPos = std::string::npos;
BaiduHttp.SetRequestCookies(strCookies);
BaiduHttp.Send(GET,strUrl);
strCookies = BaiduHttp.MergeCookie(strCookies, BaiduHttp.GetResponCookie());
std::string&& strRetHeader = BaiduHttp.GetallResponseHeaders();
nPos = strRetHeader.find("Location:");
if (nPos != std::string::npos)
{
strUrl = GetTextMid(strRetHeader, "Location:", "\r\n");
if (!strUrl.empty())
{
strUrl.erase(0, strUrl.find_first_not_of(" "));
nPos = strUrl.rfind("surl=");
if (nPos!=std::string::npos)
{
int index = 0;
int nError = 0;
do
{
ShowInputPassDlg();
std::string strKey = strUrl.substr(nPos + lstrlenA("surl="), strUrl.length() - nPos);
std::string strReferer = "https://pan.baidu.com/share/init?surl=" + strKey;
strUrl = str(boost::format("https://pan.baidu.com/share/verify?surl=%1%&t=%2%&channel=chunlei&web=1&app_id=250528&bdstoken=null&logid=%3%&clienttype=0") % strKey % getTimeStamp() % GetLogid());
std::string strPost = str(boost::format("pwd=%1%&vcode=&vcode_str=") % m_SharePass);
BaiduHttp.SetRequestHeader("Referer", strReferer);
BaiduHttp.Send(POST, strUrl, strPost);
std::string strResult = BaiduHttp.GetResponseText();
rapidjson::Document dc;
if (index>5)
break;
dc.Parse(strResult.c_str());
if (!dc.IsObject())
{
return nResult;
}
if (dc.HasMember("errno") && dc["errno"].IsInt())
{
nError = dc["errno"].GetInt();
}
index++;
} while (nError != 0);
strCookies = BaiduHttp.MergeCookie(strCookies, BaiduHttp.GetResponCookie());
nResult = 1;
}else
nResult = 0;
}
else
nResult = 0;
}
else
{
nResult = 0;
}
return nResult;
}
std::string CBaiduParse::AddOfflineDownload(const std::string& strUrl,const std::string& strSavePath, const std::string& strCookie)
{
std::string bResult;
if (strUrl.empty() || strCookie.empty() || strSavePath.empty())
return bResult;
BaiduUserInfo userinfo;
if (!GetloginBassInfo(userinfo, strCookie))
return bResult;
std::string strOffLineUrl = str(boost::format(OFF_LINE_DOWNLOAD_URL) % userinfo.bdstoken % URL_Coding(strSavePath.c_str()) % URL_Coding(strUrl.c_str()));
HttpRequest BaiduHttp;
BaiduHttp.SetRequestCookies(strCookie);
BaiduHttp.Send(GET, strOffLineUrl);
std::string strTextData = BaiduHttp.GetResponseText();
rapidjson::Document dc;
dc.Parse(strTextData.c_str());
if (!dc.IsObject())
return bResult;
if (dc.HasMember("task_id") && dc["task_id"].IsUint64())
{
ULONGLONG nUtask_id = dc["task_id"].GetUint64();
bResult = std::to_string(nUtask_id);
}
return bResult;
}
REGEXVALUE CBaiduParse::QueryOffLineList(const std::string& strCookie)
{
REGEXVALUE strResult;
ZeroMemory(&strResult, sizeof(REGEXVALUE));
if (strCookie.empty())
return strResult;
BaiduUserInfo userinfo;
if (!GetloginBassInfo(userinfo, strCookie))
return strResult;
std::string strQueryUrl = str(boost::format(OFF_LINE_QUERY_ALL_URL) % userinfo.bdstoken);
HttpRequest BaiduHttp;
BaiduHttp.SetRequestCookies(strCookie);
BaiduHttp.Send(GET, strQueryUrl);
std::string strTextData = BaiduHttp.GetResponseText();
rapidjson::Document dc;
dc.Parse(strTextData.c_str());
if (!dc.IsObject())
return strResult;
if (dc.HasMember("task_info") && dc["task_info"].IsArray())
{
for (auto& v: dc["task_info"].GetArray())
{
if (v.HasMember("task_id") && v["task_id"].IsString())
{
strResult.push_back(std::string(v["task_id"].GetString()));
}
}
}
return strResult;
}
std::string CBaiduParse::QueryTaskIdListStatus(const REGEXVALUE& TaskIdList, const std::string& strCookie)
{
std::string strResult,strTaskIdList;
if (TaskIdList.empty() || strCookie.empty())
return strResult;
BaiduUserInfo userinfo;
if (!GetloginBassInfo(userinfo, strCookie))
return strResult;
if (TaskIdList.size()==1)
{
strTaskIdList = TaskIdList.at(0);
}
else
{
for (size_t i =0;i<TaskIdList.size();i++)
{
if (i < TaskIdList.size()-1)
{
strTaskIdList += TaskIdList.at(i) + ",";
}
else
{
strTaskIdList += TaskIdList.at(i);
}
}
}
std::string strQueryUrl = str(boost::format(QUERY_LIST_TASKIDS_URL) % userinfo.bdstoken % URL_Coding(strTaskIdList.c_str()));
HttpRequest BaiduHttp;
BaiduHttp.SetRequestCookies(strCookie);
BaiduHttp.Send(GET, strQueryUrl);
std::string strTextData = BaiduHttp.GetResponseText();
rapidjson::Document dc,datajson;
dc.Parse(strTextData.c_str());
if (!dc.IsObject())
return strResult;
datajson.SetObject();
if (dc.HasMember("task_info") && dc["task_info"].IsObject())
{
rapidjson::Value task_info = dc["task_info"].GetObjectW();
rapidjson::Value arraylist(rapidjson::kArrayType);
for (size_t i =0;i < TaskIdList.size();i++)
{
rapidjson::Value addItem(rapidjson::kObjectType);
if (task_info.HasMember(TaskIdList[i].c_str()) && task_info[TaskIdList[i].c_str()].IsObject())
{
rapidjson::Value itemObj = task_info[TaskIdList[i].c_str()].GetObjectW();
int nStatus = 0;
std::string strFileSize;
std::string strtask_name, strProgress;
ULONGLONG uLfinished_size=0,uLFileSize=0;
if (itemObj.HasMember("status") && itemObj["status"].IsString())
nStatus = atoi(itemObj["status"].GetString());
if (itemObj.HasMember("file_size") && itemObj["file_size"].IsString())
{
uLFileSize = _atoi64(itemObj["file_size"].GetString());
strFileSize = GetFileSizeType(uLFileSize);
}
if (itemObj.HasMember("task_name") && itemObj["task_name"].IsString())
strtask_name = itemObj["task_name"].GetString();
if (itemObj.HasMember("finished_size") && itemObj["finished_size"].IsString())
{
uLfinished_size = _atoi64(itemObj["finished_size"].GetString());;
}
if (uLFileSize)
{
int && t_result = ((double)uLfinished_size / (double)uLFileSize) * 100;
strProgress = std::to_string(t_result) + "%";
}
rapidjson::Value status(rapidjson::kNumberType);
rapidjson::Value FileSize(rapidjson::kStringType);
rapidjson::Value FileName(rapidjson::kStringType);
rapidjson::Value TaskID(rapidjson::kStringType);
rapidjson::Value progress(rapidjson::kStringType);
status.SetInt(nStatus);
progress.SetString(strProgress.c_str(), strProgress.length(), datajson.GetAllocator());
FileSize.SetString(strFileSize.c_str(), strFileSize.length(), datajson.GetAllocator());
FileName.SetString(strtask_name.c_str(), strtask_name.length(), datajson.GetAllocator());
TaskID.SetString(TaskIdList[i].c_str(), TaskIdList[i].length(), datajson.GetAllocator());
addItem.AddMember(rapidjson::StringRef("progress"), progress, datajson.GetAllocator());
addItem.AddMember(rapidjson::StringRef("task_id"), TaskID, datajson.GetAllocator());
addItem.AddMember(rapidjson::StringRef("status"), status, datajson.GetAllocator());
addItem.AddMember(rapidjson::StringRef("Size"), FileSize, datajson.GetAllocator());
addItem.AddMember(rapidjson::StringRef("name"), FileName, datajson.GetAllocator());
arraylist.PushBack(addItem, datajson.GetAllocator());
}
}
datajson.AddMember(rapidjson::StringRef("data"), arraylist, datajson.GetAllocator());
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
datajson.Accept(writer);
strResult = buffer.GetString();
}
return strResult;
}
std::string CBaiduParse::DeleteOffLineTask(const REGEXVALUE& TaskIdList, const std::string& strCookie)
{
std::string strResult, strTaskIdList;
if (TaskIdList.empty() || strCookie.empty())
return strResult;
BaiduUserInfo userinfo;
if (!GetloginBassInfo(userinfo, strCookie))
return strResult;
if (TaskIdList.size() == 1)
{
strTaskIdList = TaskIdList.at(0);
}
else
{
for (size_t i = 0; i < TaskIdList.size(); i++)
{
if (i < TaskIdList.size() - 1)
{
strTaskIdList += TaskIdList.at(i) + ",";
}
else
{
strTaskIdList += TaskIdList.at(i);
}
}
}
std::string strQueryUrl = str(boost::format(OFF_LINE_DELETE_TASKID_URL) % userinfo.bdstoken % URL_Coding(strTaskIdList.c_str()));
HttpRequest BaiduHttp;
BaiduHttp.SetRequestCookies(strCookie);
BaiduHttp.Send(GET, strQueryUrl);
std::string strTextData = BaiduHttp.GetResponseText();
return strResult;
}
std::string CBaiduParse::GetBaiduAddr(BAIDUREQUESTINFO baiduinfo, const std::string strCookies)
{
std::string strRealUrl;
std::string strVcCode;
VERCODEINFO verCinfo = GetVerCodeinfo(baiduinfo, strCookies);
m_vcCodeUrl = verCinfo.image;
std::string strJson;
//显示验证码输入窗口
ShowInputVerCodeDlg();
HttpRequest BaiduHttp;
strVcCode = m_VerCode;
std::string strDownloadUrl = "https://pan.baidu.com/api/sharedownload?sign=" + baiduinfo.sign + \
"×tamp=" + baiduinfo.timestamp + "&channel=chunlei&web=1&app_id=" + baiduinfo.app_id + \
"&bdstoken=" + baiduinfo.bdstoken + "&logid=" + GetLogid() + "&clienttype=0";
baiduinfo.BDCLND = GetTextMid(strCookies, "BDCLND=", ";");
if (baiduinfo.BDCLND.empty())
{
strJson = "encrypt=0&product=share&vcode_input=%1%&vcode_str=%2%&uk=%3%&primaryid=%4%&fid_list=%%5B%5%%%5D";
strJson = str(boost::format(strJson) % strVcCode % verCinfo.verCode % baiduinfo.uk % baiduinfo.shareid % baiduinfo.fs_id);
}
else
{
strJson = "encrypt=0&extra=%%7B%%22sekey%%22%%3A%%22%1%%%22%%7D&product=share&vcode_input=%2%&vcode_str=%3%&uk=%4%&primaryid=%5%&fid_list=%%5B%6%%%5D&path_list=";
strJson = str(boost::format(strJson) % baiduinfo.BDCLND % strVcCode % verCinfo.verCode % baiduinfo.uk % baiduinfo.shareid % baiduinfo.fs_id);
}
/*准备提交数据获取真实URL地址*/
BaiduHttp.SetRequestHeader("Content-Type", " application/x-www-form-urlencoded; charset=UTF-8");
BaiduHttp.SetRequestHeader("Accept", " application/json, text/javascript, */*; q=0.01");
BaiduHttp.SetRequestCookies(strCookies);
BaiduHttp.Send(POST, strDownloadUrl, strJson);
strJson = BaiduHttp.GetResponseText();
rapidjson::Document dc;
dc.Parse(strJson.c_str());
if (!dc.IsObject())
return strRealUrl;
int nError = -1;
if (dc.HasMember("errno") && dc["errno"].IsInt())
nError = dc["errno"].GetInt();
if (!nError)
{
if (dc.HasMember("list") && dc["list"].IsArray())
{
for (auto &v : dc["list"].GetArray())
{
if (v.HasMember("dlink") && v["dlink"].IsString())
strRealUrl = v["dlink"].GetString();
}
}
}
return strRealUrl;
}
VERCODEINFO CBaiduParse::GetVerCodeinfo(BAIDUREQUESTINFO baiduinfo, const std::string strCookies)
{
VERCODEINFO result;
HttpRequest BaiduHttp;
std::string strJson;
std::string strVerCodeUrl = "https://pan.baidu.com/api/getvcode?prod=pan&t=0.48382518029895166&channel=chunlei&web=1&app_id=" + baiduinfo.app_id + "&bdstoken=" + baiduinfo.bdstoken + "&logid=" + GetLogid() + "&clienttype=0";
BaiduHttp.SetRequestHeader("Accept", " application/json, text/javascript, */*; q=0.01");
BaiduHttp.SetRequestHeader("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3");
BaiduHttp.SetRequestCookies(strCookies);
BaiduHttp.Send(GET, strVerCodeUrl);
strJson = BaiduHttp.GetResponseText();
rapidjson::Document dc;
dc.Parse(strJson.c_str());
if (!dc.IsObject())
return result;
if (dc.HasMember("img") && dc["img"].IsString())
result.image = dc["img"].GetString();
if (dc.HasMember("vcode") && dc["vcode"].IsString())
result.verCode = dc["vcode"].GetString();
return result;
}
long long CBaiduParse::getTimeStamp()
{
timeb t;
ftime(&t);
return t.time * 1000 + t.millitm;
}
BAIDUREQUESTINFO CBaiduParse::GetBaiduInfo(const std::string strJson)
{
BAIDUREQUESTINFO baiduInfo;
rapidjson::Document dc;
dc.Parse(strJson.c_str());
if (!dc.IsObject())
return baiduInfo;
if (dc.HasMember("bdstoken") && dc["bdstoken"].IsString())
baiduInfo.bdstoken = dc["bdstoken"].GetString();
if (dc.HasMember("shareid") && dc["shareid"].IsInt64())
baiduInfo.shareid = std::to_string(dc["shareid"].GetInt64());
if (dc.HasMember("uk") && dc["uk"].IsUint64())
baiduInfo.uk = std::to_string(dc["uk"].GetUint64());
if (dc.HasMember("sign") && dc["sign"].IsString())
baiduInfo.sign = dc["sign"].GetString();
if (dc.HasMember("timestamp") && dc["timestamp"].IsInt())
baiduInfo.timestamp = std::to_string(dc["timestamp"].GetInt());
if (dc.HasMember("file_list") && dc["file_list"].IsObject())
{
rapidjson::Value file_list = dc["file_list"].GetObject();
if (file_list.HasMember("list") && file_list["list"].IsArray())
{
for (auto& v : file_list["list"].GetArray())
{
if (v.HasMember("app_id") && v["app_id"].IsString())
{
baiduInfo.app_id = v["app_id"].GetString();
}
if (v.HasMember("server_filename") && v["server_filename"].IsString())
{
baiduInfo.server_filename = v["server_filename"].GetString();
}
if (v.HasMember("path") && v["path"].IsString())
{
baiduInfo.strPath = v["path"].GetString();
}
if (v.HasMember("isdir") && v["isdir"].IsUint())
{
baiduInfo.n_isdir = v["isdir"].GetUint();
}
if (v.HasMember("category") && v["category"].IsUint())
{
baiduInfo.ncategory = v["category"].GetUint();
}
if (v.HasMember("server_ctime") && v["server_ctime"].IsUint64())
{
baiduInfo.server_time = v["server_ctime"].GetUint64();
}
if (v.HasMember("size") && v["size"].IsUint64())
{
baiduInfo.nSize = v["size"].GetUint64();
}
if (v.HasMember("fs_id") && v["fs_id"].IsUint64())
{
baiduInfo.fs_id = std::to_string(v["fs_id"].GetUint64());
}
}
}
}
return baiduInfo;
}
std::string CBaiduParse::Unicode_To_Ansi(const wchar_t* szbuff)
{
std::string result;
CHAR* MultPtr = nullptr;
int MultLen = -1;
MultLen = ::WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, szbuff, -1, NULL, NULL, NULL, NULL);
MultPtr = new CHAR[MultLen + 1];
if (MultPtr)
{
ZeroMemory(MultPtr, MultLen + 1);
::WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, szbuff, -1, MultPtr, MultLen, NULL, NULL);
result = MultPtr;
delete[] MultPtr;
MultPtr = nullptr;
}
return result;
}
std::wstring CBaiduParse::Ansi_To_Unicode(const char* szbuff)
{
std::wstring result;
WCHAR* widePtr = nullptr;
int wideLen = -1;
wideLen = ::MultiByteToWideChar(CP_ACP, NULL, szbuff, -1, NULL, NULL);
widePtr = new WCHAR[wideLen + 1];
if (widePtr)
{
ZeroMemory(widePtr, (wideLen + 1) * sizeof(WCHAR));
::MultiByteToWideChar(CP_ACP, NULL, szbuff, -1, widePtr, wideLen);
result = widePtr;
delete[] widePtr;
widePtr = nullptr;
}
return result;
}
std::string CBaiduParse::Gbk_To_Utf8(const char* szBuff)
{
std::string resault;
int widLen = 0;
int MultiLen = 0;
WCHAR* widPtr = nullptr;
CHAR* MulitPtr = nullptr;
widLen = ::MultiByteToWideChar(CP_ACP, NULL, szBuff, -1, NULL, NULL);//获取转换后需要的空间
widPtr = new WCHAR[widLen + 1];
if (!widPtr)
return resault;
ZeroMemory(widPtr, (widLen + 1) * sizeof(WCHAR));
if (!::MultiByteToWideChar(CP_ACP, NULL, szBuff, -1, widPtr, widLen))
{
delete[] widPtr;
widPtr = nullptr;
return resault;
}
MultiLen = ::WideCharToMultiByte(CP_UTF8, NULL, widPtr, -1, NULL, NULL, NULL, NULL);
if (MultiLen)
{
MulitPtr = new CHAR[MultiLen + 1];
if (!MulitPtr)
{
delete[] widPtr;
widPtr = nullptr;
return resault;
}
ZeroMemory(MulitPtr, (MultiLen + 1) * sizeof(CHAR));
::WideCharToMultiByte(CP_UTF8, NULL, widPtr, -1, MulitPtr, MultiLen, NULL, NULL);
resault = MulitPtr;
delete[] MulitPtr;
MulitPtr = nullptr;
}
delete[] widPtr;
widPtr = nullptr;
return resault;
}
std::string CBaiduParse::Utf8_To_Gbk(const char* szBuff)
{
std::string resault;
int widLen = 0;
int MultiLen = 0;
WCHAR* widPtr = nullptr;
CHAR* MulitPtr = nullptr;
widLen = ::MultiByteToWideChar(CP_UTF8, NULL, szBuff, -1, NULL, NULL);//获取转换后需要的空间
widPtr = new WCHAR[widLen + 1];
if (!widPtr)
return resault;
ZeroMemory(widPtr, (widLen + 1) * sizeof(WCHAR));
if (!::MultiByteToWideChar(CP_UTF8, NULL, szBuff, -1, widPtr, widLen))
{
delete[] widPtr;
widPtr = nullptr;
return resault;
}
MultiLen = ::WideCharToMultiByte(CP_ACP, NULL, widPtr, -1, NULL, NULL, NULL, NULL);
if (MultiLen)
{
MulitPtr = new CHAR[MultiLen + 1];
if (!MulitPtr)
{
delete[] widPtr;
widPtr = nullptr;
return resault;
}
ZeroMemory(MulitPtr, (MultiLen + 1) * sizeof(CHAR));
::WideCharToMultiByte(CP_ACP, NULL, widPtr, -1, MulitPtr, MultiLen, NULL, NULL);
resault = MulitPtr;
delete[] MulitPtr;
MulitPtr = nullptr;
}
delete[] widPtr;
widPtr = nullptr;
return resault;
}
std::string CBaiduParse::GetTextMid(const std::string strSource, const std::string strLeft, const std::string strRight)
{
std::string strResult;
int nBeigin = 0, nEnd = 0, nLeft = 0;
if (strSource.empty())return strResult;
nBeigin = strSource.find(strLeft);
if (nBeigin == std::string::npos)return strResult;
nLeft = nBeigin + strLeft.length();
nEnd = strSource.find(strRight, nLeft);
if (nEnd == std::string::npos)
{
strResult = strSource.substr(nLeft, strSource.length());
return strResult;
}
strResult = strSource.substr(nLeft, nEnd - nLeft);
return strResult;
}
std::string CBaiduParse::GetLogid(bool isFlag /*= true*/)
{
std::string strResult;
srand(ULLONG_MAX);
CHAR szLogid[0x20];
CHAR szTime[0x20];
CHAR szResult[0x50];
ZeroMemory(szLogid, 0x20);
ZeroMemory(szTime, 0X20);
ZeroMemory(szResult, 0X50);
sprintf_s(szLogid, "%I64u", labs(rand()));
sprintf_s(szTime, "%I64d", getTimeStamp());
strcat_s(szResult, szTime);
strcat_s(szResult, "0.");
if (isFlag)
{
if (lstrlenA(szLogid) >= 16)
szLogid[16] = 0;
}
else
{
if (lstrlenA(szLogid) >= 16)
szLogid[17] = 0;
}
strcat_s(szResult, szLogid);
strResult = szResult;
strResult = aip::base64_encode(strResult.c_str(), strResult.length());
return strResult;
}
bool CBaiduParse::GetloginBassInfo(BaiduUserInfo& baseInfo, const std::string strCookie)
{
HttpRequest BaiduHttp;
BaiduHttp.SetRequestCookies(strCookie);
BaiduHttp.Send(GET, HOME_URL);
std::string strResult(BaiduHttp.GetResponseText());
// WriteFileBuffer(".\\1.txt", (char*)strResult.c_str(), strResult.length());
// strResult = GetTextMid(strResult, "context=", ";\n var yunData");
REGEXVALUE rv = GetRegexValue(strResult, "context=(.*?);\\s");
if (rv.size() < 1)
return false;
strResult = rv.at(0);
//WriteFileBuffer(".\\2.txt", (char*)strResult.c_str(), strResult.length());
rapidjson::Document dc;
dc.Parse(strResult.c_str());
if (!dc.IsObject())
return false;
if (dc.HasMember("bdstoken") && dc["bdstoken"].IsString())
baseInfo.bdstoken = dc["bdstoken"].GetString();
if (dc.HasMember("username") && dc["username"].IsString())
baseInfo.strUserName = dc["username"].GetString();
if (dc.HasMember("photo") && dc["photo"].IsString())
baseInfo.strHeadImageUrl = dc["photo"].GetString();
if (dc.HasMember("is_vip") && dc["is_vip"].IsInt())
baseInfo.is_vip = dc["is_vip"].GetInt();
BaiduHttp.Send(GET, str(boost::format(DISK_CAPACITY_QUERY) % baseInfo.bdstoken));
strResult = BaiduHttp.GetResponseText();
dc.Parse(strResult.c_str());
if (!dc.IsObject())
return false;
if (dc.HasMember("total") && dc["total"].IsUint64())
{
baseInfo.strDisktotal = GetFileSizeType(dc["total"].GetUint64());
}
if (dc.HasMember("used") && dc["used"].IsUint64())
{
baseInfo.strDiskUsed = GetFileSizeType(dc["used"].GetUint64());
}
return true;
}
REGEXVALUE CBaiduParse::GetRegexValue(const std::string strvalue, const std::string strRegx)
{
REGEXVALUE rvResult;
if (strvalue.empty() || strRegx.empty())
return rvResult;
boost::regex expr(strRegx);
boost::smatch what;
if (boost::regex_search(strvalue, what, expr))
{
size_t length = what.size();
for (size_t i = 1; i < length;++i)
{
if (what[i].matched)
{
/* std::vector<char>val;
val.resize(what[i].str().length());
memcpy(val.data(), &what[i].str()[1], what[i].str().length() - 2);*/
rvResult.push_back(what[i].str());
}
}
}
return rvResult;
}
void CBaiduParse::ShowInputVerCodeDlg()
{
::DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_VERCODE), GetDesktopWindow(), ImageProc);
}
INT_PTR CALLBACK CBaiduParse::ShareProc(_In_ HWND hwndDlg, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_BTN_OK:
{
char szCode[MAX_PATH];
ZeroMemory(szCode, MAX_PATH);
::GetDlgItemTextA(hwndDlg, IDC_EDIT_PASS, szCode, MAX_PATH);
m_SharePass = szCode;
::EndDialog(hwndDlg, 0);
}
break;
default:
break;
}
}
break;
default:
break;
}
return 0;
}
void CBaiduParse::ShowInputPassDlg()
{
::DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_SHAREPASS), GetDesktopWindow(), ShareProc);
}
std::string CBaiduParse::GetBaiduFileListInfo(const std::string& path, const std::string strCookie)
{
std::string strResultJson;
FileTypeArray fileInfoResult;
std::string strFileUrl,strResult;
ZeroMemory(&fileInfoResult, sizeof(fileInfoResult));
if (strCookie.empty() || path.empty())
return strResultJson;
BaiduUserInfo userinfo;
if (!GetloginBassInfo(userinfo, strCookie))
return strResultJson;
strFileUrl = str(boost::format(FILE_LIST_URL) % URL_Coding(path.c_str()).c_str() % userinfo.bdstoken % GetLogid() % std::to_string(getTimeStamp()));
HttpRequest BaiduHttp;
BaiduHttp.SetRequestCookies(strCookie);
BaiduHttp.Send(GET, strFileUrl);
strResult = BaiduHttp.GetResponseText();
rapidjson::Document dc;
dc.Parse(strResult.c_str());
if (!dc.IsObject())
return strResultJson;
if (dc.HasMember("list") && dc["list"].IsArray())
{
for (auto&v:dc["list"].GetArray())
{
if (v.IsObject())
{
BaiduFileInfo item;
if (v.HasMember("category") && v["category"].IsInt())
item.nCategory = v["category"].GetInt();
if (v.HasMember("isdir") && v["isdir"].IsInt())
item.nisdir = v["isdir"].GetInt();
if (v.HasMember("path") && v["path"].IsString())
item.strPath = v["path"].GetString();
if (v.HasMember("server_filename") && v["server_filename"].IsString())
item.server_filename = v["server_filename"].GetString();
if (v.HasMember("size") && v["size"].IsUint())
item.size = v["size"].GetUint();
if (v.HasMember("server_ctime") && v["server_ctime"].IsUint())
item.server_ctime = v["server_ctime"].GetUint();
if (v.HasMember("fs_id") && v["fs_id"].IsUint64())
item.fs_id = std::to_string(v["fs_id"].GetUint64());
if (!item.nisdir)
item.strFileType = ::PathFindExtensionA(Utf8_To_Gbk(item.server_filename.c_str()).c_str());
fileInfoResult.push_back(item);
}
}
}
rapidjson::Document docjson;
docjson.SetObject();
rapidjson::Value UsrName(rapidjson::kStringType);
UsrName.SetString(userinfo.strUserName.c_str(), userinfo.strUserName.length(), docjson.GetAllocator());
docjson.AddMember(rapidjson::StringRef("UserName"), UsrName, docjson.GetAllocator());
rapidjson::Value UsrHeaderImage(rapidjson::kStringType);
UsrHeaderImage.SetString(userinfo.strHeadImageUrl.c_str(), userinfo.strHeadImageUrl.length(), docjson.GetAllocator());
docjson.AddMember(rapidjson::StringRef("UserHeader"), UsrHeaderImage, docjson.GetAllocator());
rapidjson::Value array_list(rapidjson::kArrayType);
for (size_t i = 0; i < fileInfoResult.size();i++)
{
rapidjson::Value itemObject(rapidjson::kObjectType);
rapidjson::Value isdir(rapidjson::kNumberType);
isdir.SetInt(fileInfoResult[i].nisdir);
itemObject.AddMember(rapidjson::StringRef("isdir"), isdir, docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("name"), rapidjson::StringRef(fileInfoResult[i].server_filename.c_str()), docjson.GetAllocator());
std::string strFilesize = GetFileSizeType(fileInfoResult[i].size);
rapidjson::Value FileSize(rapidjson::kStringType);
FileSize.SetString(strFilesize.c_str(), strFilesize.length(), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("Size"), FileSize, docjson.GetAllocator());
rapidjson::Value nCategory(rapidjson::kNumberType);
nCategory.SetInt(fileInfoResult[i].nCategory);
itemObject.AddMember(rapidjson::StringRef("nCategory"), nCategory, docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("path"), rapidjson::StringRef(fileInfoResult[i].strPath.c_str()), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("FileType"), rapidjson::StringRef(fileInfoResult[i].strFileType.c_str()), docjson.GetAllocator());
std::string strTime = timestampToDate(fileInfoResult[i].server_ctime);
rapidjson::Value FileTime(rapidjson::kStringType);
FileTime.SetString(strTime.c_str(), strTime.length(), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("ChangeTime"), FileTime, docjson.GetAllocator());
rapidjson::Value fs_id(rapidjson::kStringType);
fs_id.SetString(fileInfoResult[i].fs_id.c_str(), fileInfoResult[i].fs_id.length(), docjson.GetAllocator());
itemObject.AddMember(rapidjson::StringRef("fs_id"), fs_id, docjson.GetAllocator());
array_list.PushBack(itemObject, docjson.GetAllocator());
}
docjson.AddMember(rapidjson::StringRef("data"), array_list, docjson.GetAllocator());