-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkd.cpp
1829 lines (1549 loc) · 43 KB
/
linkd.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
// CONSOLE
#define TXT_MULTI_THREAD_MODE
#include <stdp>
#include <stdr>
#include <txta>
#include <wtxta>
#include <ui64a>
#include <filer>
#include <filep>
#include <curl.h>
txta linkf = 28859;
void loadLinksWithExt()
{
linkf.Clear();
filer fr = "D:\\vbox\\all_links.txt";
txt lnk;
fr > lnk > FC;
ui64 file_s = NFND; // HTML file path start position
ui64 l_beg = NFND; // Line beginning
txt flinks = 4096;
for(ui64 i = 0; i < ~lnk; ++i)
{
if(i + 2 < ~lnk && lnk[i] == 'D' && lnk[i+1] == ':' && lnk[i+2] == '\\')
{
file_s = i;
}
if(lnk[i] == '\n')
{
if(file_s != NFND)
{
linkf << flinks;
if(flinks != empty)
{
txtclr(flinks);
}
flinks += txtsp(lnk, file_s, i);
file_s = NFND;
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
continue;
}
txt link = txtsp(lnk, l_beg, i);
ui64 last_sl = txtfe(link, TEND, '/'); // Last slash
ui64 dot = txtfr(link, last_sl, TEND, '.');
if(dot != NFND)
{
flinks += link;
}
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
}
}
}
bool64 isLetter(char c)
{
if((c < 'A' || c > 'Z') && (c < 'a' || c > 'z'))
{
return false;
}
return true;
}
const txt e_jpg = L(".jpg");
const txt e_png = L(".png");
const txt e_gif = L(".gif");
const txt e_jpeg = L(".jpeg");
const txt e_bmp = L(".bmp");
bool64 isImage(const txt &ext)
{
if(
ext == e_jpg ||
ext == e_png ||
ext == e_gif ||
ext == e_jpeg ||
ext == e_bmp)
{
return true;
}
return false;
}
void loadImageLinks()
{
linkf.Clear();
filer fr = "D:\\vbox\\all_links.txt";
txt lnk;
fr > lnk > FC;
ui64 file_s = NFND; // HTML file path start position
ui64 l_beg = NFND; // Line beginning
txt flinks = 4096;
for(ui64 i = 0; i < ~lnk; ++i)
{
if(i + 2 < ~lnk && lnk[i] == 'D' && lnk[i+1] == ':' && lnk[i+2] == '\\')
{
file_s = i;
}
if(lnk[i] == '\n')
{
if(file_s != NFND)
{
linkf << flinks;
if(flinks != empty)
{
txtclr(flinks);
}
flinks += txtsp(lnk, file_s, i);
file_s = NFND;
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
continue;
}
ui64 dot = txtfe(lnk, i, '.');
ui64 ext_e = dot + 1;
while(ext_e < ~lnk && isLetter(lnk[ext_e]))
{
++ext_e;
}
txt ext = txtsp(lnk, dot, ext_e-1);
t2low(ext);
txt link = txtsp(lnk, l_beg, i);
if(isImage(ext))
{
flinks += link;
}
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
}
}
filep fp = "D:\\vbox\\all_img_links.txt";
fp|TRN;
for(ui64 i = 0; i < ~linkf; ++i)
{
p|L(" ")|CR|i|CR;
fp|linkf[i];
}
}
void analyseLinkTypes()
{
txta exts = 1024;
ui64a exts_n = 1024;
filer fr = "D:\\vbox\\all_links_exts.txt";
txt lnk;
fr > lnk > FC;
ui64 file_s = NFND; // HTML file path start position
ui64 l_beg = NFND; // Line beginning
for(ui64 i = 0; i < ~lnk; ++i)
{
if(i + 2 < ~lnk && lnk[i] == 'D' && lnk[i+1] == ':' && lnk[i+2] == '\\')
{
file_s = i;
}
if(lnk[i] == '\n')
{
if(file_s != NFND) // Just skip files
{
file_s = NFND;
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
continue;
}
ui64 dot = txtfe(lnk, i, '.');
txt ext = txtsp(lnk, dot, i-1);
bool64 ext_fnd = false;
for(ui64 j = 0; j < ~exts; ++j)
{
if(exts[j] == ext)
{
++exts_n[j];
ext_fnd = true;
break;
}
}
if(!ext_fnd)
{
exts << ext;
exts_n << 1;
}
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
}
}
ui64 tot_num = 0;
for(ui64 i = 0; i < ~exts_n; ++i)
{
tot_num += exts_n[i];
}
for(ui64 i = 0; i < ~exts; ++i) // Insertion sort
{
for(ui64 j = i+1; j < ~exts; ++j)
{
if(exts_n[j] > exts_n[i])
{
ui64 tmp = exts_n[i];
exts_n[i] = exts_n[j];
exts_n[j] = tmp;
txt tmpt = exts[i];
exts[i] = exts[j];
exts[j] = tmpt;
}
}
}
for(ui64 i = 0; i < ~exts; ++i)
{
txt perc = i2t((ui64)(((double)exts_n[i]/(double)tot_num) * 1000.0));
txti(perc, ~perc-1, '.');
p|SPN(15)|exts[i]|S|exts_n[i]|S|perc|'%'|N;
}
p|L("TOTAL LINKS: ")|tot_num|N;
}
bool64 fileExists(const char *fn)
{
DWORD attr = GetFileAttributesA(fn);
if(attr == INVALID_FILE_ATTRIBUTES)
{
return false;
}
return true;
}
const txt base_p = L("D:\\vbox\\holywars.ru.local\\holywars.ru\\");
const txt dsl = L("//");
void loadLinksNotDown() // Load only links not present in local mirror
{
linkf.Clear();
filer fr = "D:\\vbox\\all_img_links.txt";
txt lnk;
fr > lnk > FC;
ui64 file_s = NFND; // HTML file path start position
ui64 l_beg = NFND; // Line beginning
txt flinks = 4096;
txt hex = 16;
for(ui64 i = 0; i < ~lnk; ++i)
{
if(i % 1024 == 0)
{
p|L("SCAN: ")|i|CR;
}
if(i + 2 < ~lnk && lnk[i] == 'D' && lnk[i+1] == ':' && lnk[i+2] == '\\')
{
file_s = i;
}
if(lnk[i] == '\n')
{
if(file_s != NFND)
{
linkf << flinks;
if(flinks != empty)
{
txtclr(flinks);
}
flinks += txtsp(lnk, file_s, i);
file_s = NFND;
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
continue;
}
txt link_p = txtsp(lnk, l_beg, i-1); // Link converted to local path
ui64 d_sl = txtf(link_p, 0, dsl); // Double forward slash (http: --> [//] <--)
link_p = txtd(link_p, 0, d_sl+2);
for(ui64 j = 0; j < ~link_p; ++j)
{
if(link_p[j] == '/')
{
link_p[j] = '\\';
}
if(link_p[j] == '%') // Uniform Resource Identifier (URI) to Internationalized Resource Identifier (IRI)
{
txtclr(hex);
txtssz(hex, 2);
hex[0] = link_p[j+1];
hex[1] = link_p[j+2];
txtr(link_p, j, 3, (char)h2i(hex));
}
}
link_p = base_p + link_p;
//http:// holywars.ru/public/upload/war/1304347755.3294.jpg
//https://fc08.deviantart.net/fs71/f/2011/034/d/2/forever_a_pone_by_glamourkat-d38ooot.jpg
//D:\vbox\holywars.ru.local\holywars.ru\ www.razruha.org\data\media\4789 \1333919273f99.jpg
if(!fileExists(link_p)) // Load only links missing from local mirror
{
txt link = txtsp(lnk, l_beg, i);
flinks += link;
}
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
}
}
filep fp = "D:\\vbox\\all_miss_img_links.txt";
fp|TRN;
for(ui64 i = 0; i < ~linkf; ++i)
{
p|L(" ")|CR|i|CR;
fp|linkf[i];
}
}
ui64 loadLinks()
{
//linkf.Clear();
//filer fr = "FAILED_ZEROSIZE.TXT";
////filer fr = L("D:\\vbox\\all_failed_links.txt");
////filer fr = L("D:\\vbox\\all_miss_img_links.txt");
////filer fr = L("D:\\P\\MT\\FAILED_REDIRECTS.TXT");
//if(!fr)
//{
// //fr < FO(L("all_failed_links.txt"));
// //fr < FO(L("all_miss_img_links.txt"));
// //fr < FO(L("FAILED_REDIRECTS.TXT"));
//}
//
//txt lnk;
//fr > lnk > FC;
//
//ui64 pure_links = 0;
//ui64 file_s = NFND; // HTML file path start position
//ui64 l_beg = NFND; // Line beginning
//txt file_link = 4096; // File or link
//for(ui64 i = 0; i < ~lnk; ++i)
//{
// if(i + 2 < ~lnk && lnk[i] == 'D' && lnk[i+1] == ':' && lnk[i+2] == '\\')
// {
// file_s = i;
// }
//
// if(lnk[i] == '\n')
// {
// if(file_s != NFND)
// {
// file_link = txtsp(lnk, file_s, i-1); // Do not include newline
// linkf << file_link;
// file_s = NFND;
//
// if(i + 1 < ~lnk)
// {
// l_beg = i + 1;
// }
// continue;
// }
//
// ++pure_links;
//
// file_link = txtsp(lnk, l_beg, i-1); // Do not include newline
// linkf << file_link;
//
// if(i + 1 < ~lnk)
// {
// l_beg = i + 1;
// }
// }
//}
//
//return pure_links;
linkf.Clear();
filer fr = "all_failed_links.txt";
txt lnk;
fr > lnk > FC;
ui64 l_beg = 0; // Line beginning
txt file_link = 4096; // File or link
for(ui64 i = 0; i < ~lnk; ++i)
{
if(lnk[i] == '\n')
{
txtsp(file_link, lnk, l_beg, i-1); // Do not include newline
linkf << file_link;
if(i + 1 < ~lnk)
{
l_beg = i + 1;
}
}
}
return ~linkf;
}
#define MAX_THREADS 24
#define MAX_TRYS 4 // How many times downloader thread will attempt to download link
#define TOUT_DT 400 // How much ms to add after each retry
bool64 all_threads_stop = false;
ui64 starting_link_idx;
ui64a lstatus; // Status of the link
txta failed_links = 28859; // YOU HAVE FAILED ME THE LAST TIME!
HANDLE mutex_main, mutex_status, mutex_print, mutex_fails, mutex_thread_num;
ui64 threads_active;
void incSafe(ui64 *var)
{
WaitForSingleObject(mutex_status, INFINITE); // CRITICAL SECTION START ==================
++*var;
ReleaseMutex(mutex_status); // CRITICAL SECTION END =====================================
}
void incThreads()
{
WaitForSingleObject(mutex_thread_num, INFINITE); // CRITICAL SECTION START ==================
++threads_active;
ReleaseMutex(mutex_thread_num); // CRITICAL SECTION END =====================================
}
ui64 curl_error;
ui64 redirect_fails;
ui64 status_ok; // 200
ui64 status_ok_other; // 201 - 226
ui64 status_redirect; // 300 - 308
ui64 status_bad_req; // 400
ui64 status_unauth; // 401
ui64 status_need_pay; // 402
ui64 status_forbid; // 403
ui64 status_not_fnd; // 404
ui64 status_fail_other; // 405 - 451
ui64 status_server_err; // 500 - 511
ui64 status_unrecogn; // 600 - INFINITY
void decThreads()
{
WaitForSingleObject(mutex_thread_num, INFINITE); // CRITICAL SECTION START ==================
--threads_active;
if(threads_active == 0)
{
p|SCP(0, MAX_THREADS+3);
p|N|L("CURL ERRORS: ")|curl_error|N;
p |L("REDIRECT FAILS: ")|redirect_fails|N;
p |L("STATUS OK: ")|status_ok|N;
p |L("STATUS OK OTHER: ")|status_ok_other|N;
p |L("STATUS REDIRECT: ")|status_redirect|N;
p |L("STATUS BAD REQ: ")|status_bad_req|N;
p |L("STATUS UNAUTH: ")|status_unauth|N;
p |L("STATUS NEED PAY: ")|status_need_pay|N;
p |L("STATUS FORBID: ")|status_forbid|N;
p |L("STATUS NOT FND: ")|status_not_fnd|N;
p |L("STATUS FAIL OTHER: ")|status_fail_other|N;
p |L("STATUS SERVER ERR: ")|status_server_err|N;
p |L("STATUS UNRECOGN: ")|status_unrecogn|N;
filep fp = "FAILED_LINKS.TXT";
for(ui64 i = 0; i < ~failed_links; ++i)
{
fp|failed_links[i]|N;
}
}
ReleaseMutex(mutex_thread_num); // CRITICAL SECTION END =====================================
}
void printSafe(ui64 y_pos, ui64 idx, const txt &t)
{
// |Thread xx: | -> 11 symbols
WaitForSingleObject(mutex_print, INFINITE); // CRITICAL SECTION START ==================
p|SCP(11, y_pos)|S(109);
p|SCP(11, y_pos)|SPN(5)|idx|S|txts(t, 0, 100)|L("...");
ReleaseMutex(mutex_print); // CRITICAL SECTION END =====================================
}
ui64 NOTHROW PARAMNOUSE headFunc(char *buffer, ui64 size, ui64 nitems, void *userdata)
{
txtadd(*((txt *)userdata), buffer, nitems);
return nitems; // [nitems] - size of data / [size] always 1 / No \0
}
ui64 NOTHROW PARAMNOUSE writeFunc(char *ptr, ui64 size, ui64 nmemb, void *userdata)
{
((binf *)userdata)->Add(ptr, nmemb);
return nmemb; // [nmemb] - size of data / [size] always 1 / No \0
}
const txt https_txt = L("https");
const txt e_jpg_caps = L(".JPG");
const txt e_png_caps = L(".PNG");
const txt e_txt = L(".txt");
const txt head_location = L("location:");
const txt head_content = L("content-type:");
const txt head_cont_img = L("image/");
const txt not_an_image = L(".not_an_image");
const txt cont_unknown = L("UNKNOWN CONTENT TYPE");
const txt output_dir_name = L("IMAGES\\");
const txt url_dsl = L("//");
void incResponseCode(i32 code)
{
switch(code)
{
case 200:
incSafe(&status_ok);
break;
case 400:
incSafe(&status_bad_req);
break;
case 401:
incSafe(&status_unauth);
break;
case 402:
incSafe(&status_need_pay);
break;
case 403:
incSafe(&status_forbid);
break;
case 404:
incSafe(&status_not_fnd);
break;
default:
break;
}
if(code >= 201 && code < 300)
{
incSafe(&status_ok_other);
}
else if(code >= 300 && code < 400)
{
incSafe(&status_redirect);
}
else if(code >= 405 && code < 500)
{
incSafe(&status_fail_other);
}
else if(code >= 500 && code < 600)
{
incSafe(&status_server_err);
}
else
{
incSafe(&status_unrecogn);
}
}
THREAD downloader(void *param)
{
incThreads();
CURL *curl = NULL;
txt last_file = MAX_PATH;
txt out_fname = MAX_PATH;
txt link_orig = 512;
txt link = 512;
txt link_redir = 512;
txt ext = 32;
txt cont_type = 128;
binf image = 0x100000; // 1'048'576 B
txt head = 0x1000; // 4'096 B
ui64 dot, ext_e;
//ui64 i = 0;
ui64 i = starting_link_idx;
get_next_link:
bool64 link_grabbed = false;
WaitForSingleObject(mutex_main, INFINITE); // CRITICAL SECTION START ==================
for(; i < ~linkf; ++i)
{
if(linkf[i][0] == 'D') // Source file found
{
last_file = linkf[i];
continue;
}
if(!lstatus[i]) // Link is availabe for download attempts
{
link = linkf[i];
lstatus[i] = true; // Now this link is owned by this thread
link_grabbed = true;
break;
}
}
ReleaseMutex(mutex_main); // CRITICAL SECTION END =====================================
if(!link_grabbed || all_threads_stop) // No more free links available, abort!
{
printSafe((ui64)param, i, L("No more links found, aborting..."));
curl_easy_cleanup(curl);
decThreads();
return 0;
}
link_orig = link;
cont_type = cont_unknown;
printSafe((ui64)param, i, link);
curl = curl_easy_init();
if(!curl)
{
p|"Thread #"|GetCurrentThreadId()|" failed to initialise CURL! Exiting...";
return 1;
}
bool64 https_change = false; // Change from http to https was tried
bool64 png_2_jpg = false; // Change from png to jpg/jpeg was tried
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headFunc);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &image);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &head);
retry_download:
image.Clear();
txtclr(head);
curl_easy_setopt(curl, CURLOPT_URL, (const char *)link);
CURLcode res;
ui64 trys = MAX_TRYS;
ui64 to_sleep = 0;
bool64 curl_err = false;
i32 http_code = 0;
while(--trys != UI64_MAX)
{
res = curl_easy_perform(curl);
if(res != CURLE_OK)
{
curl_err = true;
if(res == CURLE_OPERATION_TIMEDOUT)
{
goto total_fail;
}
goto end_loop_sleep;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if(http_code < 400)
{
break;
}
end_loop_sleep:
to_sleep += TOUT_DT;
Sleep((DWORD)to_sleep);
}
if(http_code >= 300 && http_code < 400) // Handle redirect
{
ui64 loc = txtfci(head, 0, head_location);
if(loc != NFND)
{
ui64 url_beg = loc + ~head_location;
while(head[url_beg] == ' ')
{
++url_beg;
}
ui64 url_end = url_beg;
while(head[url_end] != '\r' && head[url_end] != '\n')
{
++url_end;
}
link_redir = txtsp(head, url_beg, url_end-1);
if(link_redir[0] == '/') // Relative link
{
ui64 urlb = txtf(link, 0, url_dsl) + 2;
ui64 urle = txtf(link, urlb, '/') - 1;
link_redir = txtsp(link, 0, urle) + link_redir;
}
if(link_redir == link) // Site redirects to the same adress infinitely
{
incSafe(&redirect_fails);
goto total_fail;
}
link = link_redir;
goto retry_download;
}
}
// Extract image extention
dot = txtfe(link, TEND, '.');
if(dot == NFND) // This link or it's redirect is broken, skip it
{
goto total_fail;
}
ext_e = dot + 1;
while(ext_e < ~link && isLetter(link[ext_e]))
{
++ext_e;
}
--ext_e;
ext = txtsp(link, dot, ext_e);
if(curl_err || http_code >= 400) // Attempt failed!
{
if(link[4] != 's' && !https_change)
{
https_change = true;
txti(link, 4, 's');
goto retry_download;
}
if(!png_2_jpg)
{
png_2_jpg = true;
if(https_change)
{
https_change = false;
txtd(link, 4, 1); // Revert to http
}
if(ext == e_png) // Only png files will be attempted as jpg-s
{
txtr(link, dot, ext_e, e_jpg);
ext = e_jpg;
goto retry_download;
}
else if(ext == e_png_caps)
{
txtr(link, dot, ext_e, e_jpg_caps);
ext = e_jpg_caps;
goto retry_download;
}
}
total_fail: // Total failure
if(curl_err)
{
incSafe(&curl_error);
}
else
{
incResponseCode(http_code);
}
WaitForSingleObject(mutex_fails, INFINITE); // CRITICAL SECTION START ==================
failed_links << link_orig;
ReleaseMutex(mutex_fails); // CRITICAL SECTION END =====================================
goto get_next_link;
}
// Confirm that what was downloaded is actually an image
ui64 cont = txtfci(head, 0, head_content);
ui64 type_img = NFND;
if(cont != NFND)
{
ui64 hend = txtf(head, cont, '\n');
type_img = txtfr(head, cont, hend, head_cont_img);
cont_type = txtsp(head, cont, hend-2); // -2 for \r\n
}
if(type_img == NFND)
{
ext += not_an_image;
}
// Image was successfully acquired
CreateDirectoryA(output_dir_name, NULL);
out_fname = i2t(i);
filep fp = output_dir_name + out_fname + ext;
fp|image|FC;
fp|FO(output_dir_name + out_fname + e_txt);
fp|last_file|N|link_orig|N|link|N|"TRIES: "|trys|N|"HTTPC: "|http_code|N|cont_type|FC;
incResponseCode(http_code);
goto get_next_link;
return 0;
}
void downloadAll()
{
mutex_main = CreateMutex(NULL, FALSE, NULL);
mutex_status = CreateMutex(NULL, FALSE, NULL);
mutex_print = CreateMutex(NULL, FALSE, NULL);
mutex_fails = CreateMutex(NULL, FALSE, NULL);
mutex_thread_num = CreateMutex(NULL, FALSE, NULL);
lstatus.Reserve(~linkf);
lstatus.Fill(0);
// Initialise https library
curl_global_init(CURL_GLOBAL_ALL);
p|SCP(0, 0);
for(ui64 i = 0; i < MAX_THREADS; ++i)
{
p|L("Thread ")|SPCN(0)|SPN(2)|i|':'|N;
}
for(ui64 i = 0; i < MAX_THREADS; ++i)
{
CreateThread(NULL, 0, downloader, (void *)i, 0, NULL);
}
}
HANDLE swtm;
LARGE_INTEGER swtout;
bool64 sw_stop;
THREAD stopwatch(void *param)
{
swtout.QuadPart = -10000000; // 1 second
SYSTEMTIME lt, slt;
GetLocalTime(<);
slt = lt;
ui64 ssec = (ui64)(slt.wSecond + slt.wMinute * 60 + slt.wHour * 3600 + slt.wDay * 86400);
ui64 pre_ms = lt.wMilliseconds;
if(pre_ms > 20) // Wait for system clock to catch up and make delay ~ 20 ms
{
Sleep(1000 - (DWORD)pre_ms + 20);
}
else if(pre_ms < 15)
{
Sleep(15 - (DWORD)pre_ms);
}
while(1)
{
SetWaitableTimer(swtm, &swtout, 0, NULL, NULL, FALSE);
GetLocalTime(<);
ui64 sec = (ui64)(lt.wSecond + lt.wMinute * 60 + lt.wHour * 3600 + lt.wDay * 86400);
ui64 spas = sec - ssec;
ui64 d = spas/86400, h = spas%86400/3600, m = spas%86400%3600/60, s = spas%86400%3600%60;
WaitForSingleObject(mutex_print, INFINITE); // CRITICAL SECTION START ==================
p|SCP(0, MAX_THREADS+2)|SPC('0')|d|L(" d ")|SPN(2)|h|':'|SPN(2)|m|':'|SPN(2)|s|DP;
ReleaseMutex(mutex_print); // CRITICAL SECTION END =====================================
if(lt.wMilliseconds < pre_ms)
{
swtout.QuadPart -= 10000; // 1 ms
if(lt.wMilliseconds < 15)
{
swtout.QuadPart -= 10000;
}
}
else if(lt.wMilliseconds > pre_ms)
{
swtout.QuadPart += 10000; // 1 ms
if(lt.wMilliseconds > 25)
{
swtout.QuadPart += 10000;
}
}
pre_ms = lt.wMilliseconds;
WaitForSingleObject(swtm, INFINITE);
if(threads_active == 0)
{
return 0;
}
}
return 0;
}
void initiateDownload()
{
p|DC|L("LNKSID: ");
r > starting_link_idx;
p|SCP(0, 0)|S(40);
p|SCP(0, MAX_THREADS+1)|loadLinks()|L(" links loaded!")|N;
downloadAll();
swtm = CreateWaitableTimer(NULL, FALSE, NULL);
CreateThread(NULL, 0, stopwatch, NULL, 0, NULL);
p|P|P|P;
all_threads_stop = true;
p|DC|SCP(0, MAX_THREADS+3)|L("Aborting all threads!")|N;
p|P|P|P|P|P;
curl_global_cleanup();
}
void getFileExt(const wtxt &fn, wtxt &ext)
{
ui64 dot = txtfe(fn, TEND, '.');
if(dot == NFND)
{
return;
}
ui64 sz = (~fn - dot);
mcpy(ext, (const wchar_t *)fn + dot, sz * sizeof(wchar_t));
txtssz(ext, sz);
t2low(ext);
}
wtxt star = WL("\\*");
wtxt fnd_all = MAX_PATH;
wtxt full_fn = MAX_PATH;
wtxt fext = MAX_PATH;
wtxt fn;
binf fhead = 32;
void restoreImgExt(const wtxt &dir)
{
HANDLE ffind;
WIN32_FIND_DATAW fdata;
ui64 idx = 0;
fnd_all += dir, fnd_all += star;
// Start files enumeration =================================================================
ffind = FindFirstFileExW(
fnd_all, // [I] LPCSTR Full dir + path
FindExInfoBasic, // [I] FINDEX_INFO_LEVELS Search accuracy
&fdata, // [O] LPVOID Ouput file data
FindExSearchNameMatch, // [I] FINDEX_SEARCH_OPS Non wildcard filters
NULL, // [I] LPVOID Must be NULL (no support)
FIND_FIRST_EX_LARGE_FETCH); // [I] DWORD Additional search flags
// =========================================================================================
if(ffind == INVALID_HANDLE_VALUE)
{
p|PE|L(" In FindFirstFileExA!")|N;
return;
}
do
{
txtsdt(fn, MAX_PATH, 0, fdata.cFileName); // Always set direct manipulated text back to 0!
txtszu(fn);
if((~fn == 1 && fn[0] == '.') || (~fn == 2 && fn[0] == '.' && fn[1] == '.')) // Maybe not skip?