File size: 85,991 Bytes
7b773e7
4b9057c
 
b8e3ad7
 
4b9057c
b8e3ad7
 
 
4b9057c
 
 
fd2ce95
5641dea
b9e60db
b8e3ad7
 
 
 
5641dea
 
b9e60db
b8e3ad7
c5e6558
18121e0
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
b9e60db
b8e3ad7
 
 
0444076
ac436f8
c5e6558
 
 
 
6f39f32
 
 
 
 
 
 
 
 
 
 
 
c5e6558
52f13e6
 
 
 
 
 
 
c5e6558
52f13e6
 
c5e6558
 
 
 
 
b8e3ad7
 
 
 
 
52f13e6
 
c5e6558
fd2ce95
 
4b9057c
fd2ce95
 
 
 
 
 
4b9057c
fd2ce95
 
 
 
 
 
 
4b9057c
52f13e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5e6558
6f39f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5e6558
 
 
4b9057c
5641dea
6f39f32
 
 
 
c5e6558
 
6f39f32
 
 
c5e6558
 
 
 
 
 
5641dea
b8e3ad7
 
4b9057c
b8e3ad7
c5e6558
b8e3ad7
5641dea
b8e3ad7
ac436f8
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb399cc
 
c5e6558
fb399cc
 
 
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f13e6
b8e3ad7
52f13e6
 
 
 
 
b8e3ad7
52f13e6
 
c5e6558
52f13e6
 
b8e3ad7
52f13e6
 
b8e3ad7
fd2ce95
52f13e6
fd2ce95
52f13e6
fb399cc
b8e3ad7
52f13e6
b8e3ad7
52f13e6
b8e3ad7
52f13e6
b8e3ad7
 
52f13e6
 
 
 
b8e3ad7
 
c5e6558
 
52f13e6
b8e3ad7
 
 
52f13e6
b8e3ad7
 
52f13e6
 
fb399cc
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
 
 
 
c5e6558
b8e3ad7
c5e6558
 
f81cb89
 
c5e6558
 
 
 
 
 
 
 
b8e3ad7
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f13e6
f81cb89
 
c5e6558
f81cb89
fb399cc
 
 
b8e3ad7
 
 
 
 
 
 
 
fb399cc
 
 
 
b8e3ad7
 
 
 
 
 
 
 
 
 
 
 
 
fb399cc
 
5a2226b
 
b8e3ad7
 
 
5a2226b
 
 
 
3b03ee1
c5e6558
5a2226b
3b03ee1
 
907ffd6
b8e3ad7
 
baece32
 
907ffd6
baece32
c5e6558
baece32
 
 
 
 
 
 
 
 
c5e6558
52f13e6
 
c5e6558
52f13e6
 
 
c5e6558
 
 
52f13e6
 
 
 
c5e6558
 
 
52f13e6
 
c5e6558
b8e3ad7
 
 
52f13e6
baece32
 
907ffd6
c5e6558
b8e3ad7
 
 
c5e6558
 
b8e3ad7
c5e6558
 
b8e3ad7
 
 
 
c5e6558
b8e3ad7
 
 
 
 
c5e6558
b8e3ad7
 
 
c5e6558
 
 
b8e3ad7
 
 
 
c5e6558
 
 
b8e3ad7
 
c5e6558
b8e3ad7
 
 
 
3b03ee1
c5e6558
3b03ee1
 
b8e3ad7
52f13e6
3b03ee1
 
b8e3ad7
3b03ee1
 
 
c5e6558
 
3b03ee1
 
 
 
 
907ffd6
942484e
3b03ee1
942484e
 
 
 
 
3b03ee1
 
b8e3ad7
3b03ee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f13e6
 
 
 
 
 
3b03ee1
 
 
 
 
c5e6558
3b03ee1
 
 
 
 
 
 
 
 
b8e3ad7
c5e6558
3b03ee1
 
 
 
 
 
c5e6558
 
52f13e6
b8e3ad7
c5e6558
942484e
 
b8e3ad7
942484e
b8e3ad7
 
942484e
 
b8e3ad7
 
942484e
 
 
b8e3ad7
942484e
 
 
b8e3ad7
942484e
c5e6558
942484e
 
b8e3ad7
942484e
 
b8e3ad7
942484e
b8e3ad7
 
942484e
 
b8e3ad7
 
942484e
c5e6558
942484e
 
c5e6558
942484e
 
 
 
 
b8e3ad7
 
942484e
b8e3ad7
942484e
 
 
 
 
 
 
 
b8e3ad7
942484e
c5e6558
b8e3ad7
942484e
 
b8e3ad7
 
 
942484e
 
b8e3ad7
942484e
b8e3ad7
942484e
 
 
b8e3ad7
 
942484e
 
b8e3ad7
 
 
 
 
c5e6558
52f13e6
b8e3ad7
 
 
 
 
52f13e6
 
b8e3ad7
942484e
 
 
 
 
 
b8e3ad7
942484e
b8e3ad7
942484e
 
 
 
b8e3ad7
c5e6558
942484e
 
b8e3ad7
 
c5e6558
942484e
 
 
 
 
 
b8e3ad7
942484e
 
 
 
 
c5e6558
942484e
 
b8e3ad7
942484e
 
 
b8e3ad7
 
 
 
 
 
 
52f13e6
b8e3ad7
 
 
 
 
 
 
942484e
b8e3ad7
 
52f13e6
b8e3ad7
 
52f13e6
 
 
b8e3ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f13e6
b8e3ad7
942484e
 
 
 
b8e3ad7
c5e6558
942484e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
 
 
 
942484e
 
 
 
b8e3ad7
 
c5e6558
 
 
18121e0
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
942484e
c5e6558
942484e
c5e6558
 
 
 
942484e
c5e6558
 
baece32
c5e6558
 
baece32
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
c5e6558
 
 
 
 
 
b8e3ad7
c5e6558
 
 
b8e3ad7
c5e6558
 
 
 
 
 
 
 
 
 
b8e3ad7
c5e6558
 
b8e3ad7
c5e6558
 
b8e3ad7
 
 
c5e6558
 
 
 
 
b8e3ad7
 
942484e
b8e3ad7
 
942484e
 
c5e6558
942484e
 
b8e3ad7
942484e
b8e3ad7
3b03ee1
b8e3ad7
3b03ee1
 
 
 
 
 
 
 
c5e6558
 
52f13e6
b8e3ad7
c5e6558
b8e3ad7
942484e
c5e6558
942484e
 
 
c5e6558
b8e3ad7
 
 
 
 
 
 
 
 
 
52f13e6
b8e3ad7
52f13e6
b8e3ad7
 
 
 
 
 
942484e
b8e3ad7
 
 
 
 
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
942484e
c5e6558
942484e
 
b8e3ad7
c5e6558
942484e
 
 
 
 
 
 
b8e3ad7
942484e
 
 
 
b8e3ad7
942484e
 
 
 
 
c5e6558
b8e3ad7
942484e
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
942484e
 
 
 
 
 
 
 
907ffd6
b8e3ad7
942484e
b8e3ad7
942484e
b8e3ad7
942484e
b8e3ad7
942484e
 
 
 
 
 
 
 
 
3b03ee1
aaa2984
c5e6558
4b9057c
5641dea
 
b8e3ad7
5641dea
9fa91d7
 
 
c5e6558
7e696de
ac436f8
 
 
b8e3ad7
 
c5e6558
 
 
 
 
b8e3ad7
c5e6558
4ce7f57
c5e6558
b8e3ad7
c5e6558
 
 
b8e3ad7
 
 
 
c5e6558
 
b8e3ad7
4ce7f57
b8e3ad7
 
c5e6558
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
c5e6558
 
 
 
 
 
 
 
 
b8e3ad7
 
4b9057c
9fa91d7
b8e3ad7
 
c5e6558
 
 
 
 
 
18121e0
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
 
9ad3033
7e696de
b8e3ad7
 
 
7e696de
ac436f8
7e696de
b8e3ad7
ac436f8
7e696de
5641dea
b8e3ad7
5641dea
 
 
 
 
 
b8e3ad7
5641dea
 
 
 
b8e3ad7
5641dea
c5e6558
 
 
 
 
 
5641dea
b8e3ad7
5641dea
 
 
 
 
 
 
 
 
 
c5e6558
b8e3ad7
 
 
 
 
 
 
c5e6558
 
b8e3ad7
 
 
 
 
 
 
 
c5e6558
b8e3ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5641dea
b8e3ad7
 
c5e6558
6f39f32
 
 
 
 
b8e3ad7
c5e6558
 
5641dea
b8e3ad7
 
 
 
 
 
7e696de
 
 
5641dea
b8e3ad7
 
c5e6558
b8e3ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
5641dea
b8e3ad7
5641dea
b8e3ad7
 
 
 
 
 
6f39f32
b8e3ad7
 
 
5641dea
b8e3ad7
 
 
5641dea
 
b8e3ad7
 
 
 
 
5641dea
b8e3ad7
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
 
 
 
 
 
 
 
 
 
 
5641dea
b8e3ad7
 
 
 
 
9fa91d7
c5e6558
b8e3ad7
 
c5e6558
b8e3ad7
 
 
 
 
 
c5e6558
b8e3ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f13e6
c5e6558
b8e3ad7
 
52f13e6
 
b8e3ad7
5641dea
b8e3ad7
5641dea
 
 
 
 
 
b8e3ad7
 
c5e6558
 
baece32
b8e3ad7
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18121e0
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
c5e6558
 
 
 
 
b8e3ad7
c5e6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e3ad7
 
 
c5e6558
4b9057c
b8e3ad7
4b9057c
ac436f8
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
import streamlit as st
import os
import asyncio
import subprocess
import tempfile
import logging
import time
import json
import base64
import re
import random
import zipfile
import datetime
import traceback
import shutil
import mimetypes
from pathlib import Path
from urllib.parse import urlparse, urljoin, unquote
from io import BytesIO
from PIL import Image
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

# Advanced imports
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError
from bs4 import BeautifulSoup
from PyPDF2 import PdfReader
import google_auth_oauthlib.flow
import googleapiclient.discovery
import google.auth.transport.requests
import googleapiclient.http
import requests
import celery
from celery import Celery
import splash
import pyppeteer
import mitmproxy
from mitmproxy import http

# Configure page and logging
st.set_page_config(page_title="Advanced File Downloader", layout="wide")
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Initialize Celery for distributed task processing
celery_app = Celery('file_downloader', broker='redis://localhost:6379/0')

# Configure Google OAuth
GOOGLE_OAUTH_CONFIG = {
    "web": {
        "client_id": "90798824947-u25obg1q844qeikjoh4jdmi579kn9p1c.apps.googleusercontent.com",
        "project_id": "huggingface-449214",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_secret": "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",
        "redirect_uris": ["https://euler314-craw-web.hf.space/"]
    }
}

# -------------------- User Agent Settings --------------------
USER_AGENTS = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15',
    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.54',
    'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1',
    'Mozilla/5.0 (iPad; CPU OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1',
]

# -------------------- Proxy Management --------------------
PROXY_POOL = []
CURRENT_PROXY_INDEX = 0

# -------------------- Network Interception Configuration --------------------
NETWORK_INTERCEPTOR_CONFIG = {
    "enabled": False,
    "intercept_types": ["xhr", "fetch", "document", "media"],
    "save_intercepted": True,
    "intercept_folder": "./intercepted_data"
}

# -------------------- Utility Functions --------------------
def get_random_user_agent():
    return random.choice(USER_AGENTS)

def sizeof_fmt(num, suffix='B'):
    for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
        if abs(num) < 1024.0:
            return f"{num:3.1f}{unit}{suffix}"
        num /= 1024.0
    return f"{num:.1f}Y{suffix}"

def create_zip_file(file_paths, output_dir):
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    zip_path = os.path.join(output_dir, f"downloads_{timestamp}.zip")
    with zipfile.ZipFile(zip_path, 'w') as zipf:
        for file_path in file_paths:
            zipf.write(file_path, os.path.basename(file_path))
    return zip_path

def get_file_extension(url, default='.pdf'):
    """Extract file extension from URL or filename"""
    path = urlparse(url).path
    ext = os.path.splitext(path)[1].lower()
    if not ext:
        return default
    return ext

def get_domain(url):
    """Extract domain from URL"""
    parsed = urlparse(url)
    return parsed.netloc

def is_valid_file_url(url, extensions):
    """Check if URL is a valid file URL based on extension"""
    return any(url.lower().endswith(ext) for ext in extensions)

# -------------------- Google Drive Functions --------------------
def get_google_auth_url():
    client_config = GOOGLE_OAUTH_CONFIG["web"]
    flow = google_auth_oauthlib.flow.Flow.from_client_config(
        {"web": client_config},
        scopes=["https://www.googleapis.com/auth/drive.file"]
    )
    flow.redirect_uri = client_config["redirect_uris"][0]
    authorization_url, _ = flow.authorization_url(
        access_type="offline",
        include_granted_scopes="true",
        prompt="consent"
    )
    return authorization_url

def exchange_code_for_credentials(auth_code):
    if not auth_code.strip():
        return None, "No code provided."
    try:
        client_config = GOOGLE_OAUTH_CONFIG["web"]
        flow = google_auth_oauthlib.flow.Flow.from_client_config(
            {"web": client_config},
            scopes=["https://www.googleapis.com/auth/drive.file"]
        )
        flow.redirect_uri = client_config["redirect_uris"][0]
        flow.fetch_token(code=auth_code.strip())
        creds = flow.credentials
        if not creds or not creds.valid:
            return None, "Could not validate credentials. Check code and try again."
        return creds, "Google Sign-In successful!"
    except Exception as e:
        return None, f"Error during token exchange: {e}"

def google_drive_upload(file_path, credentials, folder_id=None):
    try:
        drive_service = googleapiclient.discovery.build("drive", "v3", credentials=credentials)
        file_metadata = {'name': os.path.basename(file_path)}
        if folder_id:
            file_metadata['parents'] = [folder_id]
        media = googleapiclient.http.MediaFileUpload(file_path, resumable=True)
        created = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
        return created.get("id", "")
    except Exception as e:
        return f"Error uploading to Drive: {str(e)}"

def create_drive_folder(drive_service, name):
    folder_metadata = {'name': name, 'mimeType': 'application/vnd.google-apps.folder'}
    folder = drive_service.files().create(body=folder_metadata, fields='id').execute()
    return folder.get('id')

# -------------------- Setup Functions --------------------
def setup_dependencies():
    """Install required system dependencies"""
    try:
        # Install system dependencies
        subprocess.run(['apt-get', 'update', '-y'], check=True)
        packages = [
            'libnss3', 'libnss3-tools', 'libnspr4', 'libatk1.0-0',
            'libatk-bridge2.0-0', 'libatspi2.0-0', 'libcups2', 'libxcomposite1',
            'libxdamage1', 'libdrm2', 'libgbm1', 'libpango-1.0-0',
            'redis-server', 'python3-dev', 'build-essential'
        ]
        subprocess.run(['apt-get', 'install', '-y', '--no-install-recommends'] + packages, check=True)
        
        # Install Python packages
        subprocess.run(['pip', 'install', 'playwright', 'pyppeteer', 'splash', 'celery[redis]', 'mitmproxy'], check=True)
        
        # Install browsers
        subprocess.run(['python3', '-m', 'playwright', 'install', 'chromium'], check=True)
        subprocess.run(['python3', '-m', 'pyppeteer', 'install'], check=True)
        
        st.success("Dependencies installed successfully!")
        return True
    except Exception as e:
        st.error(f"Error installing dependencies: {e}")
        st.info("You may need to manually install dependencies. Check console for details.")
        logger.error(f"Setup error: {e}")
        traceback.print_exc()
        return False

def check_services():
    """Check if required services are running"""
    try:
        # Check Redis for Celery
        redis_running = subprocess.run(['redis-cli', 'ping'], capture_output=True, text=True).stdout.strip() == 'PONG'
        if not redis_running:
            # Try to start Redis
            subprocess.run(['service', 'redis-server', 'start'], check=True)
        
        # Create directories for intercepted data
        os.makedirs(NETWORK_INTERCEPTOR_CONFIG['intercept_folder'], exist_ok=True)
        
        return True
    except Exception as e:
        logger.error(f"Service check error: {e}")
        return False

# -------------------- Network Interception Classes --------------------
class NetworkInterceptor:
    """Class to intercept network traffic using mitmproxy"""
    
    def __init__(self, intercept_types=None, save_path=None):
        self.intercept_types = intercept_types or ["xhr", "fetch", "document"]
        self.save_path = save_path or "./intercepted_data"
        os.makedirs(self.save_path, exist_ok=True)
        self.captured_data = []
    
    def intercept_request(self, flow):
        """Process intercepted requests"""
        try:
            url = flow.request.url
            method = flow.request.method
            content_type = flow.request.headers.get("Content-Type", "")
            
            # Log the request
            self.captured_data.append({
                "type": "request",
                "url": url,
                "method": method,
                "headers": dict(flow.request.headers),
                "timestamp": time.time()
            })
            
            logger.info(f"Intercepted {method} request to {url}")
        except Exception as e:
            logger.error(f"Error intercepting request: {e}")
    
    def intercept_response(self, flow):
        """Process intercepted responses"""
        try:
            url = flow.request.url
            status_code = flow.response.status_code
            content_type = flow.response.headers.get("Content-Type", "")
            
            # Only process responses of interest based on content type
            if any(t in content_type.lower() for t in ["application/pdf", "application/msword", 
                                                      "application/vnd.openxmlformats", 
                                                      "application/zip"]):
                # Save the file
                filename = os.path.basename(urlparse(url).path)
                if not filename or filename == '/':
                    filename = f"file_{int(time.time())}"
                    
                    # Try to add extension based on content type
                    if "pdf" in content_type:
                        filename += ".pdf"
                    elif "msword" in content_type:
                        filename += ".doc"
                    elif "openxmlformats" in content_type and "wordprocessingml" in content_type:
                        filename += ".docx"
                    elif "zip" in content_type:
                        filename += ".zip"
                
                file_path = os.path.join(self.save_path, filename)
                with open(file_path, "wb") as f:
                    f.write(flow.response.content)
                
                logger.info(f"Saved intercepted file: {file_path}")
                
                # Record metadata about the captured file
                self.captured_data.append({
                    "type": "file",
                    "url": url,
                    "content_type": content_type,
                    "size": len(flow.response.content),
                    "path": file_path,
                    "timestamp": time.time()
                })
        except Exception as e:
            logger.error(f"Error intercepting response: {e}")
    
    def get_captured_files(self):
        """Return list of captured files"""
        return [item for item in self.captured_data if item["type"] == "file"]

# -------------------- Browser Automation Classes --------------------
class MultiEngineBrowser:
    """Class that supports multiple browser engines (Playwright, Pyppeteer, Splash)"""
    
    def __init__(self, engine="playwright", use_proxy=False, proxy=None, stealth=True):
        self.engine = engine
        self.use_proxy = use_proxy
        self.proxy = proxy
        self.stealth = stealth
        self.browser = None
        self.context = None
        self.page = None
    
    async def setup(self):
        """Initialize browser based on selected engine"""
        if self.engine == "playwright":
            return await self.setup_playwright()
        elif self.engine == "pyppeteer":
            return await self.setup_pyppeteer()
        elif self.engine == "splash":
            return await self.setup_splash()
        else:
            raise ValueError(f"Unsupported browser engine: {self.engine}")
    
    async def setup_playwright(self):
        """Setup Playwright browser"""
        from playwright.async_api import async_playwright
        
        self.playwright = await async_playwright().start()
        browser_args = [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-dev-shm-usage',
            '--disable-web-security',
            '--disable-features=IsolateOrigins,site-per-process',
        ]
        
        if self.stealth:
            browser_args.extend([
                '--disable-blink-features=AutomationControlled',
                '--disable-features=IsolateOrigins'
            ])
        
        launch_options = {
            "headless": True,
            "args": browser_args
        }
        
        if self.use_proxy and self.proxy:
            launch_options["proxy"] = {"server": self.proxy}
        
        self.browser = await self.playwright.chromium.launch(**launch_options)
        
        context_options = {
            "viewport": {"width": 1920, "height": 1080},
            "user_agent": get_random_user_agent(),
            "bypass_csp": True,
            "ignore_https_errors": True,
            "accept_downloads": True
        }
        
        self.context = await self.browser.new_context(**context_options)
        
        # Apply stealth features
        if self.stealth:
            await self.context.add_init_script("""
                Object.defineProperty(navigator, 'webdriver', { get: () => false });
                Object.defineProperty(navigator, 'plugins', { 
                    get: () => [1, 2, 3, 4, 5].map(() => ({ length: 1 }))
                });
                Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
                window.chrome = { runtime: {} };
            """)
        
        self.page = await self.context.new_page()
        return self.page
    
    async def setup_pyppeteer(self):
        """Setup Pyppeteer browser"""
        from pyppeteer import launch
        
        browser_args = [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-dev-shm-usage',
            '--disable-web-security',
        ]
        
        if self.stealth:
            browser_args.extend([
                '--disable-blink-features=AutomationControlled',
                '--disable-features=IsolateOrigins'
            ])
        
        launch_options = {
            "headless": True,
            "args": browser_args,
            "ignoreHTTPSErrors": True,
            "userDataDir": tempfile.mkdtemp()
        }
        
        if self.use_proxy and self.proxy:
            browser_args.append(f'--proxy-server={self.proxy}')
        
        self.browser = await launch(launch_options)
        self.page = await self.browser.newPage()
        
        # Set user agent
        await self.page.setUserAgent(get_random_user_agent())
        
        # Set viewport
        await self.page.setViewport({"width": 1920, "height": 1080})
        
        # Apply stealth features
        if self.stealth:
            await self.page.evaluateOnNewDocument("""
                Object.defineProperty(navigator, 'webdriver', { get: () => false });
                Object.defineProperty(navigator, 'plugins', { 
                    get: () => [1, 2, 3, 4, 5].map(() => ({ length: 1 }))
                });
                Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
                window.chrome = { runtime: {} };
            """)
        
        return self.page
    
    async def setup_splash(self):
        """Setup Splash browser through API"""
        # Splash is typically used via HTTP API
        # We'll use requests for this
        self.splash_url = "http://localhost:8050/render.html"
        return None  # No actual page object for Splash
    
    async def goto(self, url, wait_until=None, timeout=30000):
        """Navigate to a URL"""
        if self.engine == "playwright":
            return await self.page.goto(url, wait_until=wait_until or 'networkidle', timeout=timeout)
        elif self.engine == "pyppeteer":
            return await self.page.goto(url, waitUntil=wait_until or 'networkidle0', timeout=timeout)
        elif self.engine == "splash":
            # Use Splash HTTP API
            params = {
                "url": url,
                "wait": min(timeout/1000, 30),  # Splash uses seconds
                "timeout": min(timeout/1000, 60),
                "resource_timeout": min(timeout/1000, 30),
                "html": 1,
                "png": 0,
                "render_all": 1
            }
            
            if self.use_proxy and self.proxy:
                params["proxy"] = self.proxy
            
            headers = {"User-Agent": get_random_user_agent()}
            response = requests.get(self.splash_url, params=params, headers=headers)
            self.last_html = response.text
            return response
    
    async def content(self):
        """Get page content"""
        if self.engine == "playwright":
            return await self.page.content()
        elif self.engine == "pyppeteer":
            return await self.page.content()
        elif self.engine == "splash":
            return self.last_html
    
    async def close(self):
        """Close browser"""
        if self.engine == "playwright":
            if self.browser:
                await self.browser.close()
            if self.playwright:
                await self.playwright.stop()
        elif self.engine == "pyppeteer":
            if self.browser:
                await self.browser.close()
        # No cleanup needed for Splash as it's stateless

# -------------------- Download Manager Class --------------------
class DownloadManager:
    def __init__(self, browser_engine="playwright", use_proxy=False, proxy=None, query=None, num_results=5, use_stealth=True):
        self.browser_engine = browser_engine
        self.use_proxy = use_proxy
        self.proxy = proxy
        self.query = query
        self.num_results = num_results
        self.use_stealth = use_stealth
        self.browser = None
        self.network_interceptor = None
        
        # Configure network interception if enabled
        if NETWORK_INTERCEPTOR_CONFIG["enabled"]:
            self.network_interceptor = NetworkInterceptor(
                intercept_types=NETWORK_INTERCEPTOR_CONFIG["intercept_types"],
                save_path=NETWORK_INTERCEPTOR_CONFIG["intercept_folder"]
            )

    async def __aenter__(self):
        # Initialize multi-engine browser
        self.browser = MultiEngineBrowser(
            engine=self.browser_engine,
            use_proxy=self.use_proxy,
            proxy=self.proxy,
            stealth=self.use_stealth
        )
        self.page = await self.browser.setup()
        
        # Set headers for better stealth
        if self.browser_engine == "playwright":
            await self.page.set_extra_http_headers({
                'Accept-Language': 'en-US,en;q=0.9',
                'Accept-Encoding': 'gzip, deflate, br',
                'DNT': '1',
                'Referer': 'https://www.google.com/',
                'Sec-Fetch-Dest': 'document',
                'Sec-Fetch-Mode': 'navigate',
                'Sec-Fetch-Site': 'cross-site',
                'Sec-Fetch-User': '?1',
                'Upgrade-Insecure-Requests': '1'
            })
        
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await self.browser.close()

    async def search_web(self, search_engine="bing"):
        """Search web using specified search engine"""
        urls = []
        try:
            if search_engine == "bing":
                search_url = f"https://www.bing.com/search?q={self.query}"
            elif search_engine == "google":
                search_url = f"https://www.google.com/search?q={self.query}"
            else:
                raise ValueError(f"Unsupported search engine: {search_engine}")
            
            await self.browser.goto(search_url, timeout=30000)
            
            if self.browser_engine == "playwright":
                if search_engine == "bing":
                    links = await self.page.query_selector_all("li.b_algo h2 a")
                    for link in links[:self.num_results]:
                        href = await link.get_attribute('href')
                        if href:
                            urls.append(href)
                elif search_engine == "google":
                    links = await self.page.query_selector_all("div.g a[href^='http']")
                    for link in links[:self.num_results]:
                        href = await link.get_attribute('href')
                        if href:
                            urls.append(href)
            elif self.browser_engine == "pyppeteer":
                if search_engine == "bing":
                    links = await self.page.querySelectorAll("li.b_algo h2 a")
                    for link in links[:self.num_results]:
                        href = await self.page.evaluate('el => el.getAttribute("href")', link)
                        if href:
                            urls.append(href)
                elif search_engine == "google":
                    links = await self.page.querySelectorAll("div.g a[href^='http']")
                    for link in links[:self.num_results]:
                        href = await self.page.evaluate('el => el.getAttribute("href")', link)
                        if href:
                            urls.append(href)
            elif self.browser_engine == "splash":
                # Parse the HTML with BeautifulSoup
                soup = BeautifulSoup(self.browser.last_html, 'html.parser')
                if search_engine == "bing":
                    links = soup.select("li.b_algo h2 a")
                    for link in links[:self.num_results]:
                        href = link.get("href")
                        if href:
                            urls.append(href)
                elif search_engine == "google":
                    links = soup.select("div.g a[href^='http']")
                    for link in links[:self.num_results]:
                        href = link.get("href")
                        if href:
                            urls.append(href)
            
            return urls
        except Exception as e:
            logger.error(f"Error searching web: {e}")
            return []

    async def get_file_size(self, url):
        try:
            headers = {'User-Agent': get_random_user_agent()}
            response = requests.head(url, headers=headers, timeout=15)
            length = response.headers.get('Content-Length', None)
            if length:
                return sizeof_fmt(int(length))
            else:
                return "Unknown Size"
        except Exception:
            return "Unknown Size"

    async def get_pdf_metadata(self, url):
        try:
            headers = {'User-Agent': get_random_user_agent()}
            response = requests.get(url, headers=headers, timeout=15, stream=True)
            if response.status_code == 200:
                content = BytesIO(response.content)
                reader = PdfReader(content)
                return {
                    'Title': reader.metadata.get('/Title', 'N/A') if reader.metadata else 'N/A',
                    'Author': reader.metadata.get('/Author', 'N/A') if reader.metadata else 'N/A',
                    'Pages': len(reader.pages),
                }
            else:
                return {}
        except Exception:
            return {}

    async def extract_real_download_url(self, url):
        try:
            headers = {'User-Agent': get_random_user_agent()}
            response = requests.head(url, headers=headers, timeout=15, allow_redirects=True)
            return response.url
        except Exception as e:
            logger.error(f"Error extracting real download URL: {e}")
            return url

    async def get_edu_exam_links(self, url):
        """Specialized method for educational exam websites that follows a common pattern."""
        try:
            logger.info(f"Fetching exam links from {url}")
            links = set()
            
            # First try with direct requests for speed
            headers = {"User-Agent": get_random_user_agent()}
            try:
                response = requests.get(url, headers=headers, timeout=30)
                
                if response.status_code == 200:
                    # Parse with BeautifulSoup for efficiency
                    soup = BeautifulSoup(response.text, "html.parser")
                    parsed_base = urlparse(url)
                    base_url = f"{parsed_base.scheme}://{parsed_base.netloc}"
                    
                    # Look for all links
                    for a in soup.find_all("a", href=True):
                        href = a["href"]
                        full_url = urljoin(url, href)
                        
                        # Look for text clues
                        link_text = a.get_text().lower()
                        
                        # Special patterns for exam sites (expanded list)
                        url_patterns = [
                            "/eduexp/docs/", "/exam/", "/pastexam/", "/papers/", 
                            "/test/", "/download/", "/files/", "/assignments/",
                            "paper_", "question_", "exam_", "test_", "past_",
                            "assignment_", "sample_", "study_material", "notes_",
                            "/resource/", "/subject/", "/course/", "/material/"
                        ]
                        
                        text_patterns = [
                            "exam", "paper", "test", "question", "past", "download",
                            "assignment", "sample", "study", "material", "notes",
                            "subject", "course", "resource", "pdf", "document",
                            "view", "open", "get", "solution", "answer"
                        ]
                        
                        # Check URL and text patterns
                        if any(pattern in full_url.lower() for pattern in url_patterns) or \
                           any(pattern in link_text for pattern in text_patterns) or \
                           any(full_url.lower().endswith(ext) for ext in ['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx', '.zip']):
                            links.add(full_url)
            except Exception as e:
                logger.warning(f"Request-based extraction failed: {e}")
            
            # Use browser-based approach if needed
            if len(links) < 5 or "phsms.cloud.ncnu.edu.tw" in url:
                logger.info("Using browser for enhanced link extraction")
                
                # Navigate to the page
                await self.browser.goto(url, timeout=45000)
                
                # Get page content and parse with BeautifulSoup
                content = await self.browser.content()
                soup = BeautifulSoup(content, "html.parser")
                parsed_base = urlparse(url)
                base_url = f"{parsed_base.scheme}://{parsed_base.netloc}"
                
                # Process all links on the page
                for a in soup.find_all("a", href=True):
                    href = a["href"]
                    full_url = urljoin(url, href)
                    link_text = a.get_text().lower()
                    
                    # Apply the same filtering criteria
                    url_patterns = [
                        "/eduexp/docs/", "/exam/", "/pastexam/", "/papers/", 
                        "/test/", "/download/", "/files/", "/assignments/",
                        "paper_", "question_", "exam_", "test_", "past_",
                        "assignment_", "sample_", "study_material", "notes_",
                        "/resource/", "/subject/", "/course/", "/material/"
                    ]
                    
                    text_patterns = [
                        "exam", "paper", "test", "question", "past", "download",
                        "assignment", "sample", "study", "material", "notes",
                        "subject", "course", "resource", "pdf", "document",
                        "view", "open", "get", "solution", "answer"
                    ]
                    
                    # Check URL and text patterns
                    if any(pattern in full_url.lower() for pattern in url_patterns) or \
                       any(pattern in link_text for pattern in text_patterns) or \
                       any(full_url.lower().endswith(ext) for ext in ['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx', '.zip']):
                        links.add(full_url)
            
            # Filter to likely exam documents
            filtered_links = []
            for link in links:
                # Common file extensions
                if any(ext in link.lower() for ext in ['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx', '.zip']):
                    filtered_links.append(link)
                    continue
                
                # Common paths for exam documents
                if any(pattern in link.lower() for pattern in [
                    "/eduexp/docs/pastexam", "/exam/", "/pastexam/", "/papers/", 
                    "/pastpapers/", "/questionpapers/", "/tests/", "/assignments/",
                    "/resource/", "/material/", "/notes/", "/subjectmaterial/"
                ]):
                    filtered_links.append(link)
            
            logger.info(f"Found {len(filtered_links)} potential exam document links")
            return filtered_links
            
        except Exception as e:
            logger.error(f"Error getting exam links: {e}")
            return []

    async def extract_downloadable_files(self, url, custom_ext_list):
        found_files = []
        try:
            # Special handling for educational exam sites
            if "phsms.cloud.ncnu.edu.tw" in url or any(keyword in url.lower() for keyword in 
                                                    ["exam", "test", "pastpaper", "eduexp"]):
                logger.info("Using specialized handler for educational exam site")
                
                # Get direct links to exam files
                exam_links = await self.get_edu_exam_links(url)
                
                for link in exam_links:
                    # Try to resolve any redirection
                    real_url = await self.extract_real_download_url(link)
                    filename = os.path.basename(urlparse(real_url).path)
                    
                    # If filename is URL encoded (common with Chinese/international sites)
                    if '%' in filename:
                        try:
                            filename = unquote(filename)
                        except Exception:
                            pass
                    
                    # If filename is empty or invalid, create a sensible one
                    if not filename or filename == '/':
                        domain = get_domain(real_url)
                        ext = get_file_extension(real_url, '.pdf')
                        filename = f"file_from_{domain}{ext}"
                    
                    # Get file size
                    size_str = await self.get_file_size(real_url)
                    
                    # Get metadata for PDFs
                    meta = {}
                    if real_url.lower().endswith('.pdf'):
                        try:
                            meta = await self.get_pdf_metadata(real_url)
                        except Exception:
                            pass
                    
                    found_files.append({
                        'url': real_url,
                        'filename': filename,
                        'size': size_str,
                        'metadata': meta,
                        'source_url': url  # Add source URL for better tracking
                    })
                
                # If we found exam files with the specialized method, return them
                if found_files:
                    return found_files
            
            # Standard extraction method for all pages
            await self.browser.goto(url, timeout=30000)
            
            # Get page content
            content = await self.browser.content()
            soup = BeautifulSoup(content, 'html.parser')
            
            # Define file extensions to look for
            default_exts = ['.pdf', '.docx', '.doc', '.zip', '.rar', '.mp3', '.mp4', 
                          '.avi', '.mkv', '.png', '.jpg', '.jpeg', '.gif', '.xlsx', 
                          '.pptx', '.odt', '.txt']
            all_exts = set(default_exts + [ext.strip().lower() for ext in custom_ext_list if ext.strip()])
            
            # Get base URL for resolving relative links
            parsed_base = urlparse(url)
            base_url = f"{parsed_base.scheme}://{parsed_base.netloc}"
            path_base = os.path.dirname(parsed_base.path)
            
            # Process all anchor tags for file links
            for a in soup.find_all('a', href=True):
                href = a['href'].strip()
                
                # Handle PHP and download links separately
                if '.php' in href.lower() or 'download' in href.lower():
                    full_url = href if href.startswith('http') else urljoin(base_url, href)
                    real_url = await self.extract_real_download_url(full_url)
                    if real_url and real_url != full_url:
                        filename = os.path.basename(urlparse(real_url).path) or 'downloaded_file'
                        found_files.append({
                            'url': real_url,
                            'filename': filename,
                            'size': await self.get_file_size(real_url),
                            'metadata': {},
                            'source_url': url
                        })
                        continue
                
                # Check for direct file extensions
                if any(href.lower().endswith(ext) for ext in all_exts):
                    file_url = href if href.startswith('http') else urljoin(base_url, href)
                    size_str = await self.get_file_size(file_url)
                    meta = {}
                    if file_url.lower().endswith('.pdf'):
                        meta = await self.get_pdf_metadata(file_url)
                    found_files.append({
                        'url': file_url,
                        'filename': os.path.basename(file_url.split('?')[0]),
                        'size': size_str,
                        'metadata': meta,
                        'source_url': url
                    })
                
                # Handle Google Drive links
                elif ("drive.google.com" in href) or ("docs.google.com" in href):
                    file_id = None
                    for pattern in [r'/file/d/([^/]+)', r'id=([^&]+)', r'open\?id=([^&]+)']:
                        match = re.search(pattern, href)
                        if match:
                            file_id = match.group(1)
                            break
                    
                    if file_id:
                        # Determine if it's a view-only file
                        is_view_only = "View-only" in (await self.get_file_size(f"https://drive.google.com/uc?export=download&id={file_id}"))
                        
                        filename = f"gdrive_{file_id}"
                        ext = get_file_extension(href, '.pdf')
                        if ext != '.':
                            filename += ext
                        
                        found_files.append({
                            'url': href,
                            'filename': filename,
                            'size': "View-only" if is_view_only else await self.get_file_size(f"https://drive.google.com/uc?export=download&id={file_id}"),
                            'metadata': {
                                'view_only': is_view_only,
                                'file_id': file_id
                            },
                            'source_url': url
                        })
            
            # Check for embedded content (iframe, embed, object)
            for elem_tag in ['iframe', 'embed', 'object', 'source']:
                for elem in soup.find_all(elem_tag):
                    src = elem.get('src') or elem.get('data')
                    if src and any(src.lower().endswith(ext) for ext in all_exts):
                        file_url = src if src.startswith('http') else urljoin(base_url, src)
                        found_files.append({
                            'url': file_url,
                            'filename': os.path.basename(file_url.split('?')[0]),
                            'size': await self.get_file_size(file_url),
                            'metadata': {},
                            'source_url': url
                        })
            
            # Deduplicate files
            seen_urls = set()
            unique_files = []
            for f in found_files:
                if f['url'] not in seen_urls:
                    seen_urls.add(f['url'])
                    unique_files.append(f)
                    
            return unique_files
            
        except Exception as e:
            logger.error(f"Error extracting files from {url}: {e}")
            return []

    async def download_file(self, file_info, save_dir, referer=None):
        """Download a file and provide a direct download link"""
        file_url = file_info['url']
        fname = file_info['filename']
        referer = referer or file_info.get('source_url', 'https://www.google.com')
        
        # Create unique filename to avoid overwriting
        path = os.path.join(save_dir, fname)
        base, ext = os.path.splitext(fname)
        counter = 1
        while os.path.exists(path):
            path = os.path.join(save_dir, f"{base}_{counter}{ext}")
            counter += 1
        
        os.makedirs(save_dir, exist_ok=True)
        
        try:
            # Special handling for Google Drive files
            if "drive.google.com" in file_url or "docs.google.com" in file_url:
                # For view-only Google Drive files, use specialized method
                is_view_only = file_info.get('metadata', {}).get('view_only', False)
                if is_view_only:
                    result_path = await self.download_viewonly_google_drive(file_info, path)
                    if result_path:
                        return result_path
                
                # Try standard Google Drive download
                file_id = None
                for pattern in [r'/file/d/([^/]+)', r'id=([^&]+)', r'open\?id=([^&]+)']:
                    match = re.search(pattern, file_url)
                    if match:
                        file_id = match.group(1)
                        break
                
                if file_id:
                    # Try direct download
                    download_url = f"https://drive.google.com/uc?id={file_id}&export=download"
                    headers = {
                        'User-Agent': get_random_user_agent(),
                        'Referer': referer
                    }
                    
                    with requests.get(download_url, headers=headers, stream=True) as r:
                        r.raise_for_status()
                        with open(path, 'wb') as f:
                            for chunk in r.iter_content(chunk_size=8192):
                                f.write(chunk)
                    
                    if os.path.exists(path) and os.path.getsize(path) > 0:
                        return path
            
            # Standard file download
            headers = {
                'User-Agent': get_random_user_agent(),
                'Referer': referer,
                'Accept': '*/*',
                'Accept-Encoding': 'gzip, deflate, br'
            }
            
            with requests.get(file_url, headers=headers, stream=True) as r:
                r.raise_for_status()
                with open(path, 'wb') as f:
                    for chunk in r.iter_content(chunk_size=8192):
                        f.write(chunk)
            
            if os.path.exists(path) and os.path.getsize(path) > 0:
                return path
            else:
                return None
                
        except Exception as e:
            logger.error(f"Error downloading {file_url}: {e}")
            return None

    async def download_viewonly_google_drive(self, file_info, save_path):
        """Download view-only Google Drive documents"""
        try:
            # Extract file ID
            file_id = file_info.get('metadata', {}).get('file_id')
            if not file_id:
                url = file_info['url']
                for pattern in [r'/file/d/([^/]+)', r'id=([^&]+)', r'open\?id=([^&]+)']:
                    match = re.search(pattern, url)
                    if match:
                        file_id = match.group(1)
                        break
            
            if not file_id:
                logger.error("Could not extract file ID")
                return None
            
            # Determine file type
            file_type = get_file_extension(file_info['url'], '.pdf').lstrip('.')
            
            # Ensure appropriate extension on save path
            base, ext = os.path.splitext(save_path)
            if not ext:
                save_path = f"{base}.{file_type}"
            
            logger.info(f"Downloading view-only Google Drive file: {file_id}")
            
            # Create a dedicated browser session
            if self.browser_engine == "playwright":
                from playwright.async_api import async_playwright
                
                async with async_playwright() as p:
                    browser = await p.chromium.launch(
                        headless=True,
                        args=[
                            '--no-sandbox',
                            '--disable-setuid-sandbox',
                            '--disable-dev-shm-usage',
                            '--disable-web-security',
                            '--disable-features=IsolateOrigins,site-per-process',
                            '--disable-site-isolation-trials',
                            '--disable-blink-features=AutomationControlled'
                        ]
                    )
                    
                    # Create context with options for better handling
                    context = await browser.new_context(
                        viewport={'width': 1600, 'height': 1200},
                        user_agent=get_random_user_agent(),
                        accept_downloads=True,
                        ignore_https_errors=True
                    )
                    
                    # Add stealth script
                    await context.add_init_script("""
                        Object.defineProperty(navigator, 'webdriver', { get: () => false });
                        Object.defineProperty(navigator, 'plugins', { 
                            get: () => [1, 2, 3, 4, 5].map(() => ({ length: 1 }))
                        });
                        Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
                        window.chrome = { runtime: {} };
                    """)
                    
                    page = await context.new_page()
                    
                    try:
                        # Visit the file
                        await page.goto(f"https://drive.google.com/file/d/{file_id}/view", timeout=60000)
                        await page.wait_for_load_state('networkidle')
                        
                        # Wait for content to load
                        await page.wait_for_timeout(5000)
                        
                        # Create temporary directory for processing
                        temp_dir = tempfile.mkdtemp()
                        
                        # For PDF handling
                        if file_type == 'pdf':
                            # Create directory for screenshots
                            screenshots_dir = os.path.join(temp_dir, "screenshots")
                            os.makedirs(screenshots_dir, exist_ok=True)
                            
                            # Get page count
                            total_pages = await page.evaluate("""
                                () => {
                                    // Look for page counters in the interface
                                    const pageCounters = document.querySelectorAll('*');
                                    for (const el of pageCounters) {
                                        const text = el.textContent || '';
                                        const match = text.match(/(\\d+)\\s*\\/\\s*(\\d+)/);
                                        if (match && match[2]) {
                                            return parseInt(match[2]);
                                        }
                                    }
                                    
                                    // Look for paginated pages
                                    const pages = document.querySelectorAll('.drive-viewer-paginated-page');
                                    if (pages.length > 0) return pages.length;
                                    
                                    // Default if we can't determine
                                    return 20;
                                }
                            """)
                            
                            logger.info(f"PDF has approximately {total_pages} pages")
                            
                            # Take screenshots of each page
                            screenshots = []
                            
                            # First try with the page element method
                            for i in range(min(total_pages, 100)):  # Limit to 100 pages for safety
                                try:
                                    # Navigate to specific page
                                    if i > 0:
                                        await page.evaluate(f"document.querySelector('.drive-viewer-paginated-page:nth-child({i+1})').scrollIntoView()")
                                        await page.wait_for_timeout(500)
                                    
                                    # Wait for the page to render
                                    await page.wait_for_timeout(500)
                                    
                                    # Take screenshot
                                    screenshot_path = os.path.join(screenshots_dir, f"page_{i+1}.png")
                                    
                                    # Try to find the page element
                                    page_element = await page.query_selector(f'.drive-viewer-paginated-page:nth-child({i+1})')
                                    if page_element:
                                        await page_element.screenshot(path=screenshot_path)
                                    else:
                                        # Fallback to viewport screenshot
                                        await page.screenshot(path=screenshot_path)
                                    
                                    screenshots.append(screenshot_path)
                                    
                                    # Check if we should continue to next page
                                    if i < total_pages - 1:
                                        next_button = await page.query_selector('button[aria-label="Next page"]')
                                        if next_button:
                                            # Check if button is disabled
                                            is_disabled = await next_button.get_attribute('disabled')
                                            if is_disabled:
                                                logger.info(f"Reached last page at page {i+1}")
                                                break
                                            
                                            # Click next page
                                            await next_button.click()
                                            await page.wait_for_timeout(1000)
                                        else:
                                            logger.info("Next page button not found")
                                            break
                                except Exception as e:
                                    logger.error(f"Error capturing page {i+1}: {e}")
                                    continue
                            
                            # Create PDF from screenshots
                            if screenshots:
                                # Get dimensions from first screenshot
                                first_img = Image.open(screenshots[0])
                                width, height = first_img.size
                                
                                # Create PDF
                                c = canvas.Canvas(save_path, pagesize=(width, height))
                                for screenshot in screenshots:
                                    c.drawImage(screenshot, 0, 0, width, height)
                                    c.showPage()
                                c.save()
                                
                                # Clean up screenshots
                                for screenshot in screenshots:
                                    os.remove(screenshot)
                                
                                # Clean up temp directory
                                shutil.rmtree(temp_dir, ignore_errors=True)
                                
                                return save_path
                            else:
                                logger.error("No screenshots captured")
                        else:
                            # For non-PDF files, just take a screenshot
                            screenshot_path = os.path.join(temp_dir, "file.png")
                            await page.screenshot(path=screenshot_path)
                            
                            # Copy to destination
                            shutil.copy(screenshot_path, save_path)
                            
                            # Clean up
                            os.remove(screenshot_path)
                            shutil.rmtree(temp_dir, ignore_errors=True)
                            
                            return save_path
                    finally:
                        await browser.close()
            elif self.browser_engine == "pyppeteer":
                # Similar implementation for Pyppeteer
                pass
            
            return None
        except Exception as e:
            logger.error(f"Error downloading view-only file: {e}")
            return None

    async def get_sublinks(self, url, limit=10000):
        """Extract all sublinks from a website"""
        links = set()
        try:
            logger.info(f"Extracting sublinks from {url}")
            
            # Special handling for educational sites
            if "phsms.cloud.ncnu.edu.tw" in url or any(keyword in url.lower() for keyword in 
                                                    ["exam", "test", "pastpaper", "eduexp"]):
                edu_links = await self.get_edu_exam_links(url)
                for link in edu_links:
                    links.add(link)
                
                if len(links) > 5:
                    logger.info(f"Found {len(links)} sublinks with specialized method")
                    return list(links)[:limit]
            
            # Standard link extraction for all sites
            await self.browser.goto(url, timeout=30000)
            
            # Get page content
            content = await self.browser.content()
            soup = BeautifulSoup(content, 'html.parser')
            
            # Get base URL for resolving relative links
            parsed_base = urlparse(url)
            base_url = f"{parsed_base.scheme}://{parsed_base.netloc}"
            
            # Extract all links from the page
            for a in soup.find_all('a', href=True):
                href = a['href']
                if href and not href.startswith('javascript:') and not href.startswith('#'):
                    # Resolve relative URLs
                    if href.startswith('/'):
                        full_url = f"{base_url}{href}"
                    elif href.startswith('http'):
                        full_url = href
                    else:
                        full_url = urljoin(url, href)
                    
                    links.add(full_url)
            
            # Extract iframe sources
            for iframe in soup.find_all('iframe', src=True):
                src = iframe['src']
                if src and not src.startswith('javascript:') and not src.startswith('about:'):
                    full_url = src if src.startswith('http') else urljoin(url, src)
                    links.add(full_url)
            
            return list(links)[:limit]
        except Exception as e:
            logger.error(f"Error extracting sublinks: {e}")
            return list(links)[:limit]

    @celery_app.task
    def download_file_task(file_info, save_dir, referer=None):
        """Celery task for downloading files asynchronously"""
        # This function runs in a separate worker process
        file_url = file_info['url']
        fname = file_info['filename']
        referer = referer or file_info.get('source_url', 'https://www.google.com')
        
        # Create unique filename
        path = os.path.join(save_dir, fname)
        base, ext = os.path.splitext(fname)
        counter = 1
        while os.path.exists(path):
            path = os.path.join(save_dir, f"{base}_{counter}{ext}")
            counter += 1
        
        os.makedirs(save_dir, exist_ok=True)
        
        try:
            # Handle Google Drive files
            if "drive.google.com" in file_url or "docs.google.com" in file_url:
                # Extract file ID
                file_id = None
                for pattern in [r'/file/d/([^/]+)', r'id=([^&]+)', r'open\?id=([^&]+)']:
                    match = re.search(pattern, file_url)
                    if match:
                        file_id = match.group(1)
                        break
                
                if file_id:
                    # Try direct download
                    download_url = f"https://drive.google.com/uc?id={file_id}&export=download"
                    headers = {
                        'User-Agent': get_random_user_agent(),
                        'Referer': referer
                    }
                    
                    with requests.get(download_url, headers=headers, stream=True) as r:
                        if r.status_code == 200:
                            with open(path, 'wb') as f:
                                for chunk in r.iter_content(chunk_size=8192):
                                    f.write(chunk)
                            
                            # Check if this is HTML (common for Google Drive restrictions)
                            with open(path, 'rb') as f:
                                content_start = f.read(100).decode('utf-8', errors='ignore')
                                if '<html' in content_start.lower():
                                    os.remove(path)
                                    return {'status': 'error', 'message': 'Received HTML instead of file'}
                            
                            return {'status': 'success', 'path': path}
            
            # Standard download for regular files
            headers = {
                'User-Agent': get_random_user_agent(),
                'Referer': referer,
                'Accept': '*/*',
                'Accept-Encoding': 'gzip, deflate, br'
            }
            
            with requests.get(file_url, headers=headers, stream=True) as r:
                if r.status_code == 200:
                    with open(path, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=8192):
                            f.write(chunk)
                    
                    return {'status': 'success', 'path': path}
                else:
                    return {'status': 'error', 'message': f"HTTP error: {r.status_code}"}
                
        except Exception as e:
            return {'status': 'error', 'message': str(e)}

    async def deep_search(self, url, custom_ext_list=None, sublink_limit=10000, timeout=60):
        """Perform deep search for files on a website and its subpages"""
        if not custom_ext_list:
            custom_ext_list = []
        
        # Create progress indicators
        progress_text = st.empty()
        progress_bar = st.progress(0)
        file_count_text = st.empty()
        
        try:
            progress_text.text("Analyzing main page...")
            
            # Extract files from main page first
            main_files = await self.extract_downloadable_files(url, custom_ext_list)
            initial_count = len(main_files)
            file_count_text.text(f"Found {initial_count} files on main page")
            
            # Get sublinks
            progress_text.text("Getting sublinks...")
            sublinks = await self.get_sublinks(url, sublink_limit)
            total_links = len(sublinks)
            progress_text.text(f"Found {total_links} sublinks to process")
            
            # Initialize all_files with main_files to ensure they're included
            all_files = main_files.copy()
            
            # Process each sublink
            for i, sublink in enumerate(sublinks, 1):
                progress = i / max(total_links, 1)  # Avoid division by zero
                progress_text.text(f"Processing sublink {i}/{total_links}: {sublink}")
                progress_bar.progress(progress)
                
                try:
                    # Extract files from sublink
                    sub_files = await self.extract_downloadable_files(sublink, custom_ext_list)
                    all_files.extend(sub_files)
                    file_count_text.text(f"Found {len(all_files)} total files")
                except Exception as e:
                    logger.warning(f"Error processing sublink {sublink}: {e}")
            
            # Deduplicate files
            seen_urls = set()
            unique_files = []
            for f in all_files:
                if f['url'] not in seen_urls:
                    seen_urls.add(f['url'])
                    unique_files.append(f)
            
            # Complete progress
            progress_text.text(f"Deep search complete!")
            file_count_text.text(f"Found {len(unique_files)} unique files")
            progress_bar.progress(1.0)
            
            return unique_files
            
        except Exception as e:
            logger.error(f"Deep search error: {e}")
            progress_text.text(f"Error during deep search: {str(e)}")
            return []
        
        finally:
            await asyncio.sleep(2)
            if not st.session_state.get('keep_progress', False):
                progress_text.empty()
                progress_bar.empty()

# -------------------- Main App --------------------
def main():
    st.title("Advanced File Downloader")
    
    # Initialize session state
    if "initialized" not in st.session_state:
        st.session_state.initialized = True
        st.session_state.discovered_files = []
        st.session_state.current_url = None
        st.session_state.google_creds = None
        st.session_state.selected_files = []
        st.session_state.do_deep_search = False
        st.session_state.deep_search_url = None
        st.session_state.search_results = []
        st.session_state.download_urls = {}  # For direct download links
    
    # Install dependencies if needed
    if "dependencies_installed" not in st.session_state:
        with st.spinner("Setting up dependencies. This may take a minute..."):
            st.session_state.dependencies_installed = setup_dependencies()
            check_services()
    
    # Sidebar options
    with st.sidebar:
        mode = st.radio("Select Mode", ["Manual URL", "Web Search", "Single File"], key="mode_select")
        
        with st.expander("Search Options", expanded=True):
            search_engine = st.selectbox("Search Engine", ["bing", "google"], index=0, key="search_engine")
            browser_engine = st.selectbox("Browser Engine", ["playwright", "pyppeteer", "splash"], index=0, key="browser_engine")
            custom_extensions = st.text_input("Custom File Extensions", placeholder=".csv, .txt, .epub", key="custom_ext_input", 
                                            help="Enter extensions like .csv, .txt")
            max_sublinks = st.number_input("Maximum Sublinks", min_value=1, max_value=10000, value=100, step=10, key="max_sublinks")
            sublink_timeout = st.number_input("Timeout (seconds)", min_value=1, max_value=300, value=30, step=5, key="timeout")
        
        with st.expander("Advanced Options", expanded=False):
            use_proxy = st.checkbox("Use Proxy", key="use_proxy")
            proxy = st.text_input("Proxy URL", placeholder="http://proxy:port", key="proxy_input")
            use_stealth = st.checkbox("Use Stealth Mode", value=True, key="use_stealth", 
                                    help="Makes browser harder to detect as automated")
            enable_network_intercept = st.checkbox("Enable Network Interception", value=NETWORK_INTERCEPTOR_CONFIG["enabled"], 
                                                key="enable_intercept",
                                                help="Intercept network traffic to find additional files")
            if enable_network_intercept:
                NETWORK_INTERCEPTOR_CONFIG["enabled"] = True
                intercept_types = st.multiselect("Intercept Types", 
                                               ["xhr", "fetch", "document", "media", "stylesheet", "image", "font"],
                                               default=["xhr", "fetch", "document", "media"],
                                               key="intercept_types")
                NETWORK_INTERCEPTOR_CONFIG["intercept_types"] = intercept_types
            else:
                NETWORK_INTERCEPTOR_CONFIG["enabled"] = False
        
        with st.expander("Google Drive Integration", expanded=False):
            if st.button("Start Google Sign-In", key="google_signin_btn"):
                auth_url = get_google_auth_url()
                st.markdown(f"[Click here to authorize]({auth_url})")
            auth_code = st.text_input("Enter authorization code", key="auth_code_input")
            if st.button("Complete Sign-In", key="complete_signin_btn") and auth_code:
                creds, msg = exchange_code_for_credentials(auth_code)
                st.session_state.google_creds = creds
                st.write(msg)
    
    # Main content area
    if mode == "Manual URL":
        st.header("Manual URL Mode")
        url = st.text_input("Enter URL", placeholder="https://example.com/downloads", key="url_input")
        
        col1, col2 = st.columns([3, 1])
        with col1:
            if st.button("Deep Search", use_container_width=True, key="deep_search_btn"):
                if url:
                    # Process custom extensions
                    custom_ext_list = [ext.strip().lower() for ext in custom_extensions.split(',') if ext.strip()]
                    
                    with st.spinner("Searching for files..."):
                        async def run_deep_search():
                            async with DownloadManager(
                                browser_engine=browser_engine,
                                use_proxy=use_proxy,
                                proxy=proxy,
                                use_stealth=use_stealth
                            ) as dm:
                                files = await dm.deep_search(url, custom_ext_list, max_sublinks, sublink_timeout)
                                return files
                        
                        # Run the search
                        files = asyncio.run(run_deep_search())
                        
                        if files:
                            st.session_state.discovered_files = files
                            st.session_state.current_url = url
                            st.success(f"Found {len(files)} files!")
                        else:
                            st.warning("No files found.")
        
        # Display and process discovered files
        if st.session_state.discovered_files:
            files = st.session_state.discovered_files
            
            # Select/deselect buttons
            col1, col2 = st.columns([1, 1])
            with col1:
                if st.button("Select All", key="select_all_btn"):
                    st.session_state.selected_files = list(range(len(files)))
            with col2:
                if st.button("Clear Selection", key="clear_selection_btn"):
                    st.session_state.selected_files = []
            
            # Display file list with metadata
            file_options = []
            for i, file in enumerate(files):
                filename = file['filename']
                size = file['size']
                meta = file.get('metadata', {})
                
                # Format display info
                if meta and 'Pages' in meta:
                    file_info = f"{filename} ({size}) - {meta.get('Pages', '')} pages"
                else:
                    file_info = f"{filename} ({size})"
                
                file_options.append((i, file_info))
                
                # Generate direct download URL for this file
                if i not in st.session_state.download_urls:
                    # Generate a unique key for this file
                    file_key = base64.urlsafe_b64encode(f"{file['url']}_{time.time()}".encode()).decode()
                    st.session_state.download_urls[i] = file_key
            
            # File selection multiselect
            selected_indices = st.multiselect(
                "Select files to download",
                options=[i for i, _ in file_options],
                default=st.session_state.selected_files,
                format_func=lambda i: next(info for idx, info in file_options if idx == i),
                key="file_multiselect"
            )
            
            st.session_state.selected_files = selected_indices
            
            # Display individual files with direct download links
            if files:
                st.subheader("Available Files")
                for i, file in enumerate(files):
                    with st.expander(f"{i+1}. {file['filename']} ({file['size']})"):
                        st.write(f"Source: {file.get('source_url', 'Unknown')}")
                        st.write(f"URL: {file['url']}")
                        
                        # Download button for this specific file
                        if st.button(f"Download this file", key=f"download_single_{i}"):
                            with st.spinner(f"Downloading {file['filename']}..."):
                                # Create downloads directory
                                download_dir = "./downloads"
                                os.makedirs(download_dir, exist_ok=True)
                                
                                # Download the file
                                async def download_single():
                                    async with DownloadManager(
                                        browser_engine=browser_engine,
                                        use_proxy=use_proxy,
                                        proxy=proxy,
                                        use_stealth=use_stealth
                                    ) as dm:
                                        return await dm.download_file(file, download_dir)
                                
                                file_path = asyncio.run(download_single())
                                
                                if file_path:
                                    # Create a download link
                                    with open(file_path, "rb") as f:
                                        file_bytes = f.read()
                                    
                                    file_name = os.path.basename(file_path)
                                    mime_type = mimetypes.guess_type(file_path)[0] or "application/octet-stream"
                                    
                                    st.download_button(
                                        label=f"Download {file_name}",
                                        data=file_bytes,
                                        file_name=file_name,
                                        mime=mime_type,
                                        key=f"download_btn_{i}"
                                    )
                                    
                                    st.success(f"Downloaded successfully to {file_path}")
                                else:
                                    st.error(f"Failed to download {file['filename']}")
            
            # Batch download options
            if selected_indices:
                st.subheader("Batch Download Options")
                
                col1, col2, col3, col4 = st.columns(4)
                with col1:
                    download_dir = st.text_input("Download Directory", value="./downloads", key="download_dir_input")
                with col2:
                    create_zip = st.checkbox("Create ZIP file", value=True, key="create_zip_checkbox")
                with col3:
                    delete_after = st.checkbox("Delete after ZIP", key="delete_after_checkbox")
                with col4:
                    upload_to_drive = st.checkbox("Upload to Google Drive", key="upload_drive_checkbox")
                
                if st.button("Download Selected Files", key="batch_download_btn"):
                    with st.spinner(f"Downloading {len(selected_indices)} files..."):
                        if not os.path.exists(download_dir):
                            os.makedirs(download_dir)
                        
                        # Start download process
                        downloaded_paths = []
                        progress_bar = st.progress(0)
                        status_text = st.empty()
                        
                        async def download_batch():
                            async with DownloadManager(
                                browser_engine=browser_engine,
                                use_proxy=use_proxy,
                                proxy=proxy,
                                use_stealth=use_stealth
                            ) as dm:
                                paths = []
                                for i, idx in enumerate(selected_indices):
                                    file_info = files[idx]
                                    progress = (i + 1) / len(selected_indices)
                                    status_text.text(f"Downloading {file_info['filename']}... ({i+1}/{len(selected_indices)})")
                                    progress_bar.progress(progress)
                                    
                                    path = await dm.download_file(file_info, download_dir)
                                    if path:
                                        paths.append(path)
                                
                                return paths
                        
                        downloaded_paths = asyncio.run(download_batch())
                        status_text.empty()
                        progress_bar.empty()
                        
                        if downloaded_paths:
                            st.success(f"Successfully downloaded {len(downloaded_paths)} files")
                            
                            if create_zip:
                                zip_path = create_zip_file(downloaded_paths, download_dir)
                                st.success(f"Created ZIP file: {zip_path}")
                                
                                # Provide download link for the zip file
                                with open(zip_path, "rb") as f:
                                    zip_data = f.read()
                                
                                st.download_button(
                                    label="Download ZIP",
                                    data=zip_data,
                                    file_name=os.path.basename(zip_path),
                                    mime="application/zip",
                                    key="download_zip_btn"
                                )
                                
                                # Upload to Google Drive if requested
                                if upload_to_drive and st.session_state.google_creds:
                                    with st.spinner("Uploading to Google Drive..."):
                                        drive_service = googleapiclient.discovery.build(
                                            "drive", "v3", credentials=st.session_state.google_creds
                                        )
                                        folder_id = create_drive_folder(
                                            drive_service, f"Downloads_{get_domain(url)}"
                                        )
                                        drive_id = google_drive_upload(
                                            zip_path, st.session_state.google_creds, folder_id
                                        )
                                        
                                        if not isinstance(drive_id, str) or not drive_id.startswith("Error"):
                                            st.success(f"Uploaded to Google Drive. File ID: {drive_id}")
                                        else:
                                            st.error(drive_id)
                                
                                # Delete original files if requested
                                if delete_after:
                                    for path in downloaded_paths:
                                        try:
                                            os.remove(path)
                                        except Exception as e:
                                            st.warning(f"Could not delete {path}: {e}")
                                    st.info("Deleted original files after ZIP creation")
    
    elif mode == "Web Search":
        st.header("Web Search Mode")
        
        # Search query input
        query = st.text_input("Enter search query", placeholder="example file type:pdf", key="search_query")
        num_results = st.slider("Number of results", 1, 50, 10, key="num_results")
        
        if st.button("Search", key="web_search_btn"):
            if query:
                with st.spinner("Searching the web..."):
                    async def run_search():
                        async with DownloadManager(
                            browser_engine=browser_engine,
                            use_proxy=use_proxy,
                            proxy=proxy,
                            query=query,
                            num_results=num_results,
                            use_stealth=use_stealth
                        ) as dm:
                            urls = await dm.search_web(search_engine)
                            return urls
                    
                    urls = asyncio.run(run_search())
                    
                    if urls:
                        st.session_state.search_results = urls
                        st.success(f"Found {len(urls)} results!")
                        
                        # Display search results with deep search option
                        for i, url in enumerate(urls, 1):
                            with st.expander(f"Result {i}: {url}", expanded=(i == 1)):
                                st.write(f"URL: {url}")
                                if st.button(f"Search for files", key=f"search_result_{i}"):
                                    st.session_state.deep_search_url = url
                                    st.session_state.do_deep_search = True
                    else:
                        st.warning("No search results found.")
        
        # Handle deep search of a result if requested
        if st.session_state.do_deep_search and st.session_state.deep_search_url:
            url = st.session_state.deep_search_url
            st.info(f"Searching for files on: {url}")
            
            # Reset the search flag to avoid re-running
            st.session_state.do_deep_search = False
            
            # Process custom extensions
            custom_ext_list = [ext.strip().lower() for ext in custom_extensions.split(',') if ext.strip()]
            
            with st.spinner("Searching for files..."):
                async def deep_search_result():
                    async with DownloadManager(
                        browser_engine=browser_engine,
                        use_proxy=use_proxy,
                        proxy=proxy,
                        use_stealth=use_stealth
                    ) as dm:
                        return await dm.deep_search(url, custom_ext_list, max_sublinks, sublink_timeout)
                
                files = asyncio.run(deep_search_result())
                
                if files:
                    st.session_state.discovered_files = files
                    st.session_state.current_url = url
                    st.success(f"Found {len(files)} files!")
                else:
                    st.warning("No files found on this page.")
    
    elif mode == "Single File":
        st.header("Single File Download")
        
        # View-only Google Drive download
        with st.expander("Download View-Only Google Drive Document", expanded=True):
            st.write("Download protected/view-only Google Drive documents")
            
            file_id = st.text_input(
                "Google Drive File ID", 
                placeholder="Enter ID from drive.google.com/file/d/THIS_IS_THE_ID/view",
                key="drive_file_id"
            )
            
            if st.button("Download Document", key="drive_download_btn") and file_id:
                with st.spinner("Downloading view-only document... (this may take a minute)"):
                    # Create download directory
                    download_dir = "./downloads"
                    os.makedirs(download_dir, exist_ok=True)
                    
                    # Set output path
                    output_path = os.path.join(download_dir, f"gdrive_{file_id}.pdf")
                    
                    # Download the file
                    async def download_drive_file():
                        async with DownloadManager(
                            browser_engine=browser_engine,
                            use_proxy=use_proxy,
                            proxy=proxy,
                            use_stealth=use_stealth
                        ) as dm:
                            file_info = {
                                'url': f"https://drive.google.com/file/d/{file_id}/view",
                                'filename': f"gdrive_{file_id}.pdf",
                                'metadata': {'file_id': file_id, 'view_only': True}
                            }
                            return await dm.download_viewonly_google_drive(file_info, output_path)
                    
                    result_path = asyncio.run(download_drive_file())
                    
                    if result_path:
                        st.success("Document downloaded successfully!")
                        
                        # Provide download link
                        with open(result_path, "rb") as f:
                            file_bytes = f.read()
                        
                        st.download_button(
                            label="Download PDF",
                            data=file_bytes,
                            file_name=os.path.basename(result_path),
                            mime="application/pdf",
                            key="drive_pdf_download"
                        )
                    else:
                        st.error("Failed to download the document. Please check the file ID and try again.")
        
        # Direct URL download
        with st.expander("Download from Direct URL", expanded=True):
            st.write("Download a file from a direct URL")
            
            file_url = st.text_input(
                "File URL",
                placeholder="https://example.com/file.pdf",
                key="direct_url"
            )
            
            file_name = st.text_input(
                "Save as (optional)",
                placeholder="Leave blank to use original filename",
                key="save_filename"
            )
            
            if st.button("Download File", key="direct_download_btn") and file_url:
                with st.spinner("Downloading file..."):
                    # Create download directory
                    download_dir = "./downloads"
                    os.makedirs(download_dir, exist_ok=True)
                    
                    # Determine filename
                    if not file_name:
                        file_name = os.path.basename(urlparse(file_url).path)
                        if not file_name or file_name == '/':
                            file_name = f"downloaded_file_{int(time.time())}{get_file_extension(file_url)}"
                    
                    # Create file info
                    file_info = {
                        'url': file_url,
                        'filename': file_name,
                        'metadata': {}
                    }
                    
                    # Download the file
                    async def download_direct_file():
                        async with DownloadManager(
                            browser_engine=browser_engine,
                            use_proxy=use_proxy,
                            proxy=proxy,
                            use_stealth=use_stealth
                        ) as dm:
                            return await dm.download_file(file_info, download_dir)
                    
                    file_path = asyncio.run(download_direct_file())
                    
                    if file_path:
                        st.success(f"File downloaded successfully to {file_path}")
                        
                        # Provide download link
                        with open(file_path, "rb") as f:
                            file_bytes = f.read()
                        
                        mime_type = mimetypes.guess_type(file_path)[0] or "application/octet-stream"
                        
                        st.download_button(
                            label=f"Download {os.path.basename(file_path)}",
                            data=file_bytes,
                            file_name=os.path.basename(file_path),
                            mime=mime_type,
                            key="direct_file_download"
                        )
                    else:
                        st.error("Failed to download the file. Please check the URL and try again.")
    
    # Footer
    st.markdown("---")
    st.markdown("Created by [Euler314](https://github.com/euler314) | Enhanced with advanced scraping technologies")

# Run the app
if __name__ == "__main__":
    main()