File size: 241,345 Bytes
715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 715888b c5e23a5 |
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 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 |
import streamlit as st
# This MUST be the first Streamlit command
st.set_page_config(page_title="Advanced File Downloader", layout="wide", page_icon="📁")
# Core imports
import os
import subprocess
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError
import asyncio
import logging
from urllib.parse import urlparse, urljoin, unquote, parse_qs, quote
import re
from pathlib import Path
from io import BytesIO
import random
from bs4 import BeautifulSoup
from PyPDF2 import PdfReader
import zipfile
import tempfile
import mimetypes
import requests
import datetime
import traceback
import base64
import shutil
import json
import time
from PIL import Image
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import google_auth_oauthlib.flow
import googleapiclient.discovery
import google.auth.transport.requests
import googleapiclient.http
# Enhanced RAG search imports
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import docx2txt
# Try to import sentence-transformers for better embeddings
try:
from sentence_transformers import SentenceTransformer
HAVE_TRANSFORMERS = True
except ImportError:
HAVE_TRANSFORMERS = False
# Try to download NLTK data if not already present
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
try:
nltk.download('punkt', quiet=True)
except:
pass
try:
nltk.data.find('corpora/stopwords')
except LookupError:
try:
nltk.download('stopwords', quiet=True)
from nltk.corpus import stopwords
STOPWORDS = set(stopwords.words('english'))
except:
STOPWORDS = set(['the', 'and', 'a', 'in', 'to', 'of', 'is', 'it', 'that', 'for', 'with', 'as', 'on', 'by'])
# -------------------- Logging Setup --------------------
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# -------------------- Google OAuth Config --------------------
GOOGLE_OAUTH_CONFIG = {
"web": {
"client_id": os.environ.get("GOOGLE_CLIENT_ID"),
"project_id": os.environ.get("GOOGLE_PROJECT_ID"),
"auth_uri": os.environ.get("GOOGLE_AUTH_URI", "https://accounts.google.com/o/oauth2/auth"),
"token_uri": os.environ.get("GOOGLE_TOKEN_URI", "https://oauth2.googleapis.com/token"),
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": os.environ.get("GOOGLE_CLIENT_SECRET"),
"redirect_uris": [os.environ.get("GOOGLE_REDIRECT_URI")]
}
}
# -------------------- Stealth and UA Settings --------------------
# Extended user agent list for better variety
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',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0'
]
# Stealth browser settings
STEALTH_SETTINGS = {
# Hardware features to modify/disable
"hardware_concurrency": 4,
"device_memory": 8,
# Browser features to enable/disable
"webgl_vendor": "Google Inc. (Intel)",
"webgl_renderer": "Intel Iris OpenGL Engine",
"languages": ["en-US", "en"],
"disable_webrtc": True,
# Additional timing randomization
"navigator_platform": "Win32",
"touch_support": False
}
# Proxy rotation configuration (if using multiple proxies)
PROXY_ROTATION_CONFIG = {
"enabled": False, # Set to True to enable rotation
"rotation_interval": 10, # Rotate every 10 requests
"proxies": [] # Will be populated from the UI if needed
}
# -------------------- Enhanced RAG Search with Small LLM --------------------
class EnhancedRAGSearch:
def __init__(self):
self.file_texts = []
self.chunks = [] # Document chunks for more targeted search
self.chunk_metadata = [] # Metadata for each chunk
self.file_metadata = []
self.languages = []
self.model = None
# Try to load the sentence transformer model if available
if HAVE_TRANSFORMERS:
try:
# Use a small, efficient model
self.model = SentenceTransformer('all-MiniLM-L6-v2')
self.use_transformer = True
logger.info("Using sentence-transformers for RAG")
except Exception as e:
logger.warning(f"Error loading sentence-transformer: {e}")
self.use_transformer = False
else:
self.use_transformer = False
# Fallback to TF-IDF if transformers not available
if not self.use_transformer:
self.vectorizer = TfidfVectorizer(
stop_words='english',
ngram_range=(1, 2), # Use bigrams for better context
max_features=15000, # Use more features for better representation
min_df=1 # Include rare terms
)
self.vectors = None
self.chunk_vectors = None
def add_file(self, file_data, file_info):
"""Add a file to the search index with improved processing"""
file_ext = os.path.splitext(file_info['filename'])[1].lower()
text = self.extract_text(file_data, file_ext)
if text:
# Store the whole document text
self.file_texts.append(text)
self.file_metadata.append(file_info)
# Try to detect language
try:
# Simple language detection based on stopwords
words = re.findall(r'\b\w+\b', text.lower())
english_stopwords_ratio = len([w for w in words[:100] if w in STOPWORDS]) / max(1, len(words[:100]))
lang = 'en' if english_stopwords_ratio > 0.2 else 'unknown'
self.languages.append(lang)
except:
self.languages.append('en') # Default to English
# Create chunks for more granular search
chunks = self.create_chunks(text)
for chunk in chunks:
self.chunks.append(chunk)
self.chunk_metadata.append({
'file_info': file_info,
'chunk_size': len(chunk),
'file_index': len(self.file_texts) - 1
})
return True
return False
def create_chunks(self, text, chunk_size=1000, overlap=200):
"""Split text into overlapping chunks for better search precision"""
# Try to use NLTK for sentence-aware chunking
try:
sentences = nltk.sent_tokenize(text)
chunks = []
current_chunk = ""
for sentence in sentences:
if len(current_chunk) + len(sentence) <= chunk_size:
current_chunk += sentence + " "
else:
# Add current chunk if it has content
if current_chunk:
chunks.append(current_chunk.strip())
# Start new chunk with overlap from previous chunk
if len(current_chunk) > overlap:
# Find the last space within the overlap region
overlap_text = current_chunk[-overlap:]
last_space = overlap_text.rfind(' ')
if last_space != -1:
current_chunk = current_chunk[-(overlap-last_space):] + sentence + " "
else:
current_chunk = sentence + " "
else:
current_chunk = sentence + " "
# Add the last chunk if it has content
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
except:
# Fallback to simpler chunking approach
chunks = []
for i in range(0, len(text), chunk_size - overlap):
chunk = text[i:i + chunk_size]
if chunk:
chunks.append(chunk)
return chunks
def extract_text(self, file_data, file_ext):
"""Extract text from different file types with enhanced support"""
try:
if file_ext.lower() == '.pdf':
reader = PyPDF2.PdfReader(BytesIO(file_data))
text = ""
for page in reader.pages:
extracted = page.extract_text()
if extracted:
text += extracted + "\n"
# If text extraction fails, try to OCR (would need extra libraries)
return text
elif file_ext.lower() in ['.docx', '.doc']:
return docx2txt.process(BytesIO(file_data))
elif file_ext.lower() in ['.txt', '.csv', '.json', '.html', '.htm']:
# Handle both UTF-8 and other common encodings
try:
return file_data.decode('utf-8', errors='ignore')
except:
encodings = ['latin-1', 'iso-8859-1', 'windows-1252']
for enc in encodings:
try:
return file_data.decode(enc, errors='ignore')
except:
pass
# Last resort fallback
return file_data.decode('utf-8', errors='ignore')
elif file_ext.lower() in ['.pptx', '.ppt', '.xlsx', '.xls']:
# For these types, we would need additional libraries
# For now, return a placeholder message
return f"[Content of {file_ext} file - install additional libraries for full text extraction]"
else:
return ""
except Exception as e:
logger.error(f"Error extracting text: {e}")
return ""
def build_index(self):
"""Build both document and chunk search indices"""
if not self.file_texts:
return False
try:
if self.use_transformer:
# Use sentence transformer models for embeddings
logger.info("Building document and chunk embeddings with transformer model...")
self.vectors = self.model.encode(self.file_texts, show_progress_bar=False)
# Build chunk-level index if we have chunks
if self.chunks:
# Process in batches to avoid memory issues
batch_size = 32
chunk_vectors = []
for i in range(0, len(self.chunks), batch_size):
batch = self.chunks[i:i+batch_size]
batch_vectors = self.model.encode(batch, show_progress_bar=False)
chunk_vectors.append(batch_vectors)
self.chunk_vectors = np.vstack(chunk_vectors)
else:
# Build document-level index
self.vectors = self.vectorizer.fit_transform(self.file_texts)
# Build chunk-level index if we have chunks
if self.chunks:
self.chunk_vectors = self.vectorizer.transform(self.chunks)
return True
except Exception as e:
logger.error(f"Error building search index: {e}")
return False
def expand_query(self, query):
"""Add related terms to query for better recall - mini LLM function"""
# Dictionary of related terms for common keywords
expansions = {
"exam": ["test", "assessment", "quiz", "paper", "exam paper", "past paper", "past exam"],
"test": ["exam", "quiz", "assessment", "paper"],
"document": ["file", "paper", "report", "doc", "documentation"],
"manual": ["guide", "instruction", "documentation", "handbook"],
"tutorial": ["guide", "instructions", "how-to", "lesson"],
"article": ["paper", "publication", "journal", "research"],
"research": ["study", "investigation", "paper", "analysis"],
"book": ["textbook", "publication", "volume", "edition"],
"thesis": ["dissertation", "paper", "research", "study"],
"report": ["document", "paper", "analysis", "summary"],
"assignment": ["homework", "task", "project", "work"],
"lecture": ["class", "presentation", "talk", "lesson"],
"notes": ["annotations", "summary", "outline", "study material"],
"syllabus": ["curriculum", "course outline", "program", "plan"],
"paper": ["document", "article", "publication", "exam", "test"],
"question": ["problem", "query", "exercise", "inquiry"],
"solution": ["answer", "resolution", "explanation", "result"],
"reference": ["source", "citation", "bibliography", "resource"],
"analysis": ["examination", "study", "evaluation", "assessment"],
"guide": ["manual", "instruction", "handbook", "tutorial"],
"worksheet": ["exercise", "activity", "handout", "practice"],
"review": ["evaluation", "assessment", "critique", "feedback"],
"material": ["resource", "content", "document", "information"],
"data": ["information", "statistics", "figures", "numbers"]
}
# Enhanced query expansion simulating a mini-LLM
query_words = re.findall(r'\b\w+\b', query.lower())
expanded_terms = set()
# Directly add expansions from our dictionary
for word in query_words:
if word in expansions:
expanded_terms.update(expansions[word])
# Add common academic file formats if not already included
if any(term in query.lower() for term in ["file", "document", "download", "paper"]):
if not any(ext in query.lower() for ext in ["pdf", "docx", "ppt", "excel"]):
expanded_terms.update(["pdf", "docx", "pptx", "xlsx"])
# Add special academic terms when the query seems related to education
if any(term in query.lower() for term in ["course", "university", "college", "school", "class"]):
expanded_terms.update(["syllabus", "lecture", "notes", "textbook"])
# Return original query plus expanded terms
if expanded_terms:
expanded_query = f"{query} {' '.join(expanded_terms)}"
logger.info(f"Expanded query: '{query}' -> '{expanded_query}'")
return expanded_query
return query
def search(self, query, top_k=5, search_chunks=True):
"""Enhanced search with both document and chunk-level search"""
if self.vectors is None:
return []
# Simulate a small LLM by expanding the query with related terms
expanded_query = self.expand_query(query)
try:
results = []
if self.use_transformer:
# Transform the query to embedding
query_vector = self.model.encode([expanded_query])[0]
# First search at document level for higher-level matches
if self.vectors is not None:
# Compute similarities between query and documents
doc_similarities = cosine_similarity(
query_vector.reshape(1, -1),
self.vectors
).flatten()
top_doc_indices = doc_similarities.argsort()[-top_k:][::-1]
for i, idx in enumerate(top_doc_indices):
if doc_similarities[idx] > 0.2: # Threshold to exclude irrelevant results
results.append({
'file_info': self.file_metadata[idx],
'score': float(doc_similarities[idx]),
'rank': i+1,
'match_type': 'document',
'language': self.languages[idx] if idx < len(self.languages) else 'unknown'
})
# Then search at chunk level for more specific matches if enabled
if search_chunks and self.chunk_vectors is not None:
# Compute similarities between query and chunks
chunk_similarities = cosine_similarity(
query_vector.reshape(1, -1),
self.chunk_vectors
).flatten()
top_chunk_indices = chunk_similarities.argsort()[-top_k*2:][::-1] # Get more chunk results
# Use a set to avoid duplicate file results
seen_files = set(r['file_info']['url'] for r in results)
for i, idx in enumerate(top_chunk_indices):
if chunk_similarities[idx] > 0.25: # Higher threshold for chunks
file_index = self.chunk_metadata[idx]['file_index']
file_info = self.file_metadata[file_index]
# Only add if we haven't already included this file
if file_info['url'] not in seen_files:
seen_files.add(file_info['url'])
results.append({
'file_info': file_info,
'score': float(chunk_similarities[idx]),
'rank': len(results) + 1,
'match_type': 'chunk',
'language': self.languages[file_index] if file_index < len(self.languages) else 'unknown',
'chunk_preview': self.chunks[idx][:200] + "..." if len(self.chunks[idx]) > 200 else self.chunks[idx]
})
# Stop after we've found enough results
if len(results) >= top_k*1.5:
break
else:
# Fallback to TF-IDF if transformers not available
query_vector = self.vectorizer.transform([expanded_query])
# First search at document level
if self.vectors is not None:
doc_similarities = cosine_similarity(query_vector, self.vectors).flatten()
top_doc_indices = doc_similarities.argsort()[-top_k:][::-1]
for i, idx in enumerate(top_doc_indices):
if doc_similarities[idx] > 0.1: # Threshold to exclude irrelevant results
results.append({
'file_info': self.file_metadata[idx],
'score': float(doc_similarities[idx]),
'rank': i+1,
'match_type': 'document',
'language': self.languages[idx] if idx < len(self.languages) else 'unknown'
})
# Then search at chunk level if enabled
if search_chunks and self.chunk_vectors is not None:
chunk_similarities = cosine_similarity(query_vector, self.chunk_vectors).flatten()
top_chunk_indices = chunk_similarities.argsort()[-top_k*2:][::-1]
# Avoid duplicates
seen_files = set(r['file_info']['url'] for r in results)
for i, idx in enumerate(top_chunk_indices):
if chunk_similarities[idx] > 0.15:
file_index = self.chunk_metadata[idx]['file_index']
file_info = self.file_metadata[file_index]
if file_info['url'] not in seen_files:
seen_files.add(file_info['url'])
results.append({
'file_info': file_info,
'score': float(chunk_similarities[idx]),
'rank': len(results) + 1,
'match_type': 'chunk',
'language': self.languages[file_index] if file_index < len(self.languages) else 'unknown',
'chunk_preview': self.chunks[idx][:200] + "..." if len(self.chunks[idx]) > 200 else self.chunks[idx]
})
if len(results) >= top_k*1.5:
break
# Sort combined results by score
results.sort(key=lambda x: x['score'], reverse=True)
# Re-rank and truncate
for i, result in enumerate(results[:top_k]):
result['rank'] = i+1
return results[:top_k]
except Exception as e:
logger.error(f"Error during search: {e}")
return []
# -------------------- 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 humanize_file_size(size_bytes):
"""Format file size in human-readable format"""
if size_bytes < 1024:
return f"{size_bytes} bytes"
for unit in ['KB', 'MB', 'GB', 'TB']:
size_bytes /= 1024.0
if size_bytes < 1024.0:
return f"{size_bytes:.1f} {unit}"
return f"{size_bytes:.1f} PB"
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)
def detect_captcha(html_content):
"""Detect common captcha patterns in HTML content"""
captcha_patterns = [
'captcha', 'recaptcha', 'g-recaptcha', 'hcaptcha', 'cf-turnstile',
'challenge', 'solve the following', 'verify you are human'
]
html_lower = html_content.lower()
return any(pattern in html_lower for pattern in captcha_patterns)
def is_download_link(url):
"""Enhanced function to detect if a URL is likely a download link"""
# Check for obvious download indicators in URL
url_lower = url.lower()
# Check for common download-related terms in the URL
download_terms = [
'download', 'dl', 'get', 'file', 'attachment', 'export', 'view',
'retrieve', 'fetch', 'load', 'open', 'access', 'doc', 'document'
]
if any(term in url_lower for term in download_terms):
return True
# Check for common download script patterns
script_patterns = [
'download.php', 'getfile.php', 'fetch.php', 'view.php', 'dl.php',
'download.aspx', 'getfile.aspx', 'file.aspx',
'downloadhandler', 'filehandler', 'filedownload',
'download.jsp', 'download.cgi', 'download.do',
'download-file', 'get-file',
'downloadfile', 'getfile', 'viewfile',
'Action=downloadfile', 'action=download', 'action=view',
'download?', 'file?', 'get?', 'view?'
]
if any(pattern in url_lower for pattern in script_patterns):
return True
# Check for common file extensions in the URL path or parameters
path = urlparse(url).path
common_extensions = ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx',
'.zip', '.rar', '.txt', '.csv', '.json', '.xml', '.jpg',
'.png', '.gif', '.mp3', '.mp4', '.avi', '.mov']
if any(ext in path.lower() for ext in common_extensions):
return True
# Check for file ID or file parameters in URL
params = parse_qs(urlparse(url).query)
param_keys = params.keys()
file_param_indicators = ['file', 'id', 'key', 'filename', 'name', 'fileid', 'attachment', 'attid']
if any(key.lower() in file_param_indicators for key in param_keys):
return True
# Check for complex encoding patterns like in the example URL
if 'Action=downloadfile' in url or 'fname=' in url:
return True
return False
def normalize_download_url(url):
"""Normalize download URLs to handle various formats and encodings"""
try:
# Handle common URL shorteners and redirections
parsed = urlparse(url)
# Handle phpMyAdmin-style encoded URLs
if 'Action=downloadfile' in url and 'file=' in url:
# Extract the encoded file parameter
params = parse_qs(parsed.query)
if 'file' in params:
# This is just a placeholder - in a real implementation,
# you would need to handle the specific encoding used
encoded_file = params['file'][0]
# Keep the URL as is for now, since we'll handle it during download
return url
# Handle URLs with fname parameter (like in the example)
if 'fname=' in url:
# Keep as is - we'll handle this specially during download
return url
# For other URLs, make sure they are properly quoted
path = parsed.path
# Only quote the path portion if needed
if '%' not in path and ' ' in path:
path = quote(path)
# Reconstruct the URL
normalized = parsed._replace(path=path).geturl()
return normalized
except Exception as e:
logger.error(f"Error normalizing URL {url}: {e}")
return url
# -------------------- 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')
# -------------------- Playwright Setup --------------------
def install_playwright_dependencies():
try:
# Set environment variable for Playwright browsers path
os.environ['PLAYWRIGHT_BROWSERS_PATH'] = os.path.expanduser("~/.cache/ms-playwright")
# 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'
]
subprocess.run(['apt-get', 'install', '-y', '--no-install-recommends'] + packages, check=True)
# Install Playwright and dependencies
subprocess.run(['pip', 'install', 'playwright'], check=True)
subprocess.run(['python3', '-m', 'playwright', 'install', 'chromium'], check=True)
st.success("Playwright dependencies installed successfully!")
except Exception as e:
st.error(f"Error installing Playwright dependencies: {e}")
st.info("You may need to manually install dependencies. Check console for details.")
logger.error(f"Playwright setup error: {e}")
traceback.print_exc()
# -------------------- Download Manager Class --------------------
class DownloadManager:
def __init__(self, use_proxy=False, proxy=None, query=None, num_results=5, use_stealth=True, proxy_rotation=False):
self.use_proxy = use_proxy
self.proxy = proxy
self.query = query
self.num_results = num_results
self.playwright = None
self.browser = None
self.context = None
self.page = None
self.use_stealth = use_stealth
self.proxy_rotation = proxy_rotation
self.request_count = 0
self.captcha_detected = False
self.download_timeout = 300 # 5 minutes timeout for downloads
# Track visited URLs to avoid revisiting the same URL multiple times
self.visited_urls = set()
# Track successfully downloaded files to avoid redownloading
self.downloaded_files = set()
async def __aenter__(self):
self.playwright = await async_playwright().start()
# Prepare browser args with stealth settings
browser_args = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-gpu',
'--no-zygote',
'--single-process',
'--disable-web-security',
'--disable-features=IsolateOrigins',
'--disable-site-isolation-trials'
]
# Add stealth-specific args
if self.use_stealth:
browser_args.extend([
'--disable-blink-features=AutomationControlled',
'--disable-features=IsolateOrigins,site-per-process',
'--disable-webgl',
'--disable-webrtc'
])
# Setup browser options
opts = {
"headless": True,
"args": browser_args
}
# Configure proxy if specified
if self.use_proxy and self.proxy:
opts["proxy"] = {"server": self.proxy}
# Launch browser with options
self.browser = await self.playwright.chromium.launch(**opts)
# Setup browser context with enhanced settings
context_opts = {
"user_agent": get_random_user_agent(),
"viewport": {"width": 1920, "height": 1080},
"device_scale_factor": 1,
"has_touch": False,
"is_mobile": False,
"ignore_https_errors": True,
"accept_downloads": True
}
# Apply stealth-specific settings to the context
if self.use_stealth:
# Apply JS-injection for enhanced stealth
context_opts["bypass_csp"] = True
self.context = await self.browser.new_context(**context_opts)
# Execute stealth JS to avoid detection
await self.context.add_init_script("""
() => {
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
// Change navigator properties
const newProto = navigator.__proto__;
delete newProto.webdriver;
// Overwrite the plugins
Object.defineProperty(navigator, 'plugins', {
get: () => [1, 2, 3, 4, 5].map(() => ({
lengthComputable: true,
loaded: 100,
total: 100
}))
});
// Handle languages more naturally
Object.defineProperty(navigator, 'languages', {
get: () => ['en-US', 'en', 'es']
});
// Modify hardware concurrency
Object.defineProperty(navigator, 'hardwareConcurrency', {
get: () => 4
});
// Modify deviceMemory
Object.defineProperty(navigator, 'deviceMemory', {
get: () => 8
});
// WebGL modifications
const getParameter = WebGLRenderingContext.prototype.getParameter;
WebGLRenderingContext.prototype.getParameter = function(parameter) {
if (parameter === 37445) {
return 'Intel Inc.';
}
if (parameter === 37446) {
return 'Intel Iris OpenGL Engine';
}
return getParameter.apply(this, arguments);
};
}
""")
else:
# Regular context without stealth
self.context = await self.browser.new_context(**context_opts)
# Create page with enhanced headers
self.page = await self.context.new_page()
await self.page.set_extra_http_headers({
'Accept-Language': 'en-US,en;q=0.9,es;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Cache-Control': 'max-age=0',
'DNT': '1', # Do Not Track
'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'
})
# Add delay for mouse movements to simulate human behavior
if self.use_stealth:
await self.page.mouse.move(x=random.randint(100, 500), y=random.randint(100, 500))
await self.page.wait_for_timeout(random.randint(200, 500))
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.browser:
await self.browser.close()
if self.playwright:
await self.playwright.stop()
async def rotate_proxy_if_needed(self):
"""Rotate proxy if proxy rotation is enabled and threshold is reached"""
if self.proxy_rotation and PROXY_ROTATION_CONFIG["enabled"]:
self.request_count += 1
if self.request_count >= PROXY_ROTATION_CONFIG["rotation_interval"] and PROXY_ROTATION_CONFIG["proxies"]:
# Get next proxy from the pool
next_proxy = PROXY_ROTATION_CONFIG["proxies"].pop(0)
PROXY_ROTATION_CONFIG["proxies"].append(next_proxy) # Move to end of list
# Close existing context and create new one with the new proxy
if self.context:
await self.context.close()
# Create new context with the new proxy
context_opts = {
"user_agent": get_random_user_agent(),
"proxy": {"server": next_proxy},
"accept_downloads": True
}
self.context = await self.browser.new_context(**context_opts)
self.page = await self.context.new_page()
# Reset counter
self.request_count = 0
logger.info(f"Rotated to new proxy: {next_proxy}")
async def handle_captcha(self, page):
"""Detect and handle captchas if possible"""
# Check for common captcha patterns
content = await page.content()
if detect_captcha(content):
self.captcha_detected = True
logger.warning("Captcha detected on page")
# Strategies for handling captchas:
# 1. For simple captchas, try to extract the image and solve it
captcha_img = await page.query_selector('img[alt*="captcha" i], img[src*="captcha" i]')
if captcha_img:
logger.info("Found captcha image, attempting to capture")
# Take screenshot of the captcha
captcha_path = os.path.join(tempfile.gettempdir(), "captcha.png")
await captcha_img.screenshot(path=captcha_path)
# In a real implementation, you would send this to a captcha solving service
# For now, just log the detection
logger.info(f"Captcha image saved to {captcha_path}")
# For demonstration, we'll notify the user but not actually solve it
return False
# 2. For reCAPTCHA, special handling would be required
recaptcha = await page.query_selector('iframe[src*="recaptcha"]')
if recaptcha:
logger.warning("reCAPTCHA detected, would require external solving service")
return False
# 3. Try to perform human-like actions that might bypass simple bot checks
await self.perform_human_actions(page)
# Check if captcha is still present
content = await page.content()
if detect_captcha(content):
logger.warning("Captcha still present after human-like actions")
return False
else:
logger.info("Captcha appears to be resolved")
return True
return True # No captcha detected
async def perform_human_actions(self, page):
"""Perform human-like actions on the page to possibly bypass simple bot checks"""
try:
# 1. Slowly scroll down the page
for i in range(3):
await page.evaluate(f"window.scrollTo(0, {i * 300})")
await page.wait_for_timeout(random.randint(300, 700))
# 2. Random mouse movements
for _ in range(3):
x = random.randint(100, 800)
y = random.randint(100, 600)
await page.mouse.move(x=x, y=y)
await page.wait_for_timeout(random.randint(200, 500))
# 3. Click on a non-essential part of the page
try:
await page.click("body", position={"x": 50, "y": 50})
except:
pass
# 4. Wait a bit before continuing
await page.wait_for_timeout(1000)
except Exception as e:
logger.warning(f"Error during human-like actions: {e}")
async def search_bing(self):
urls = []
try:
# Rotate proxy if needed
await self.rotate_proxy_if_needed()
search_url = f"https://www.bing.com/search?q={self.query}"
await self.page.goto(search_url, timeout=30000)
await self.page.wait_for_load_state('networkidle')
# Check for captchas
if not await self.handle_captcha(self.page):
logger.warning("Captcha detected during search, results may be limited")
# More natural scrolling behavior
for i in range(3):
await self.page.evaluate(f"window.scrollTo(0, {i * 400})")
await self.page.wait_for_timeout(random.randint(300, 800))
# Extract search results
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)
# If we didn't find enough results, try an alternative selector
if len(urls) < self.num_results:
alt_links = await self.page.query_selector_all(".b_caption a")
for link in alt_links:
href = await link.get_attribute('href')
if href and href not in urls:
urls.append(href)
if len(urls) >= self.num_results:
break
return urls
except Exception as e:
logger.error(f"Error searching Bing: {e}")
return []
async def get_file_size(self, url):
try:
await self.rotate_proxy_if_needed()
# For complex download URLs, we need to be careful with HEAD requests
if '?' in url or 'Action=downloadfile' in url or 'fname=' in url:
# For these URLs, we'll try a more reliable approach using range headers
headers = {
'User-Agent': get_random_user_agent(),
'Range': 'bytes=0-0' # Just request the first byte to check headers
}
try:
with requests.get(url, headers=headers, stream=True, timeout=10) as r:
if 'Content-Range' in r.headers:
content_range = r.headers['Content-Range']
match = re.search(r'bytes 0-0/(\d+)', content_range)
if match:
size = int(match.group(1))
return sizeof_fmt(size)
if 'Content-Length' in r.headers:
size = int(r.headers['Content-Length'])
# If size is 1, it's likely just our single requested byte
if size > 1:
return sizeof_fmt(size)
except Exception as e:
logger.warning(f"Error getting file size with Range request: {e}")
# Fallback to browser approach
try:
async with self.context.new_page() as page:
response = await page.request.head(url, timeout=15000)
length = response.headers.get('Content-Length', None)
if length:
return sizeof_fmt(int(length))
except Exception as e:
logger.warning(f"Error getting file size with browser: {e}")
return "Unknown Size"
else:
# Standard approach for normal URLs
async with self.context.new_page() as page:
response = await page.request.head(url, timeout=15000)
length = response.headers.get('Content-Length', None)
if length:
return sizeof_fmt(int(length))
else:
return "Unknown Size"
except Exception as e:
logger.warning(f"Error getting file size: {e}")
return "Unknown Size"
async def get_pdf_metadata(self, url):
try:
await self.rotate_proxy_if_needed()
async with self.context.new_page() as page:
resp = await page.request.get(url, timeout=15000)
if resp.ok:
content = await resp.body()
pdf = BytesIO(content)
reader = PdfReader(pdf)
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 as e:
logger.warning(f"Error reading PDF metadata: {e}")
return {}
async def extract_real_download_url(self, url):
"""Enhanced method to extract real download URL, handling complex URLs"""
try:
# Check if this is a complex download URL that needs special handling
if 'Action=downloadfile' in url or 'fname=' in url:
logger.info(f"Complex download URL detected: {url}")
# For these special cases, we'll use the browser to navigate and intercept redirects
await self.rotate_proxy_if_needed()
async with self.context.new_page() as page:
# Set up request interception to capture redirects
await page.route('**', lambda route: route.continue_())
# Listen for all responses
responses = []
page.on('response', lambda response: responses.append(response))
try:
# Go to the URL
await page.goto(url, wait_until='networkidle', timeout=30000)
# Check all responses for potential downloads
for response in responses:
# Look for content-disposition headers indicating a download
content_disposition = response.headers.get('Content-Disposition', '')
if 'attachment' in content_disposition or 'filename=' in content_disposition:
return response.url
# Look for content-type headers indicating a file
content_type = response.headers.get('Content-Type', '')
if content_type and content_type != 'text/html' and not content_type.startswith('text/'):
return response.url
# If no clear download was detected, return the final URL
return page.url
except Exception as e:
logger.warning(f"Error extracting real download URL: {e}")
return url
else:
# Standard approach for normal URLs
await self.rotate_proxy_if_needed()
async with self.context.new_page() as page:
response = await page.goto(url, wait_until='networkidle', timeout=30000)
if response and response.headers.get('location'):
return response.headers['location']
return page.url
except Exception as e:
logger.error(f"Error extracting real download URL: {e}")
return url
# IMPROVED: Enhanced exam links extraction method
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 (but with proper headers)
headers = {
"User-Agent": get_random_user_agent(),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9",
"Referer": "https://www.google.com/",
"DNT": "1"
}
try:
response = requests.get(url, headers=headers, timeout=30)
if response.status_code == 200:
# Parse with BeautifulSoup first 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 for patterns
if any(pattern in full_url.lower() for pattern in url_patterns):
links.add(full_url)
continue
# Check link text for patterns
if any(pattern in link_text for pattern in text_patterns):
links.add(full_url)
continue
# Check for common file extensions
if any(full_url.lower().endswith(ext) for ext in
['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx', '.zip']):
links.add(full_url)
# Check for download script parameters
if "Action=downloadfile" in url or "fname=" in url:
links.add(url) # Add the URL itself as it's a download link
except Exception as e:
logger.warning(f"Request-based extraction failed: {e}")
# Browser-based approach for more thorough extraction or if initial approach was inadequate
try:
# Check if we need to proceed with browser-based extraction
if len(links) < 5 or "phsms.cloud.ncnu.edu.tw" in url or "Action=downloadfile" in url:
logger.info("Using browser for enhanced link extraction")
# Rotate proxy if needed
await self.rotate_proxy_if_needed()
# Navigate to the page with more natural timing
await self.page.goto(url, timeout=45000, wait_until='networkidle')
await self.page.wait_for_timeout(random.randint(1000, 2000))
# Handle captchas if present
if not await self.handle_captcha(self.page):
logger.warning("Captcha detected, extraction may be limited")
# Get base URL for resolving relative links
parsed_base = urlparse(url)
base_url = f"{parsed_base.scheme}://{parsed_base.netloc}"
# Perform natural scrolling to trigger lazy-loaded content
page_height = await self.page.evaluate("document.body.scrollHeight")
viewport_height = await self.page.evaluate("window.innerHeight")
for scroll_pos in range(0, page_height, viewport_height // 2):
await self.page.evaluate(f"window.scrollTo(0, {scroll_pos})")
await self.page.wait_for_timeout(random.randint(300, 800))
# Scroll back to top
await self.page.evaluate("window.scrollTo(0, 0)")
await self.page.wait_for_timeout(500)
# Extract all links with Playwright (better than just anchor tags)
all_links = await self.page.evaluate("""
() => {
const results = [];
// Get all anchor tags
const anchors = document.querySelectorAll('a[href]');
for (const a of anchors) {
if (a.href) {
results.push({
href: a.href,
text: a.innerText || a.textContent || '',
isButton: a.classList.contains('btn') || a.role === 'button'
});
}
}
// Get buttons that might contain links
const buttons = document.querySelectorAll('button');
for (const btn of buttons) {
const onclick = btn.getAttribute('onclick') || '';
if (onclick.includes('window.location') || onclick.includes('download')) {
results.push({
href: '#button',
text: btn.innerText || btn.textContent || '',
isButton: true,
onclick: onclick
});
}
}
return results;
}
""")
# Process the extracted links
for link_info in all_links:
href = link_info.get('href', '')
text = link_info.get('text', '').lower()
if href and href != '#button':
# Check URL patterns
url_patterns = [
"/eduexp/docs/", "/exam/", "/pastexam/", "/papers/",
"/test/", "/download/", "/files/", "/assignments/",
"paper_", "question_", "exam_", "test_", "past_",
"assignment_", "sample_", "study_material", "notes_"
]
# Check text patterns
text_patterns = [
"exam", "paper", "test", "question", "past", "download",
"assignment", "sample", "study", "material", "notes",
"pdf", "document", "view", "open", "solution"
]
if any(pattern in href.lower() for pattern in url_patterns) or \
any(pattern in text for pattern in text_patterns) or \
any(href.lower().endswith(ext) for ext in
['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx', '.zip']):
links.add(href)
# Check for download links in the page
download_links = await self.page.evaluate("""
() => {
// Find all links that might be download links
const links = Array.from(document.querySelectorAll('a[href]'));
return links
.filter(a => {
const href = a.href.toLowerCase();
return href.includes('download') ||
href.includes('getfile') ||
href.includes('view.php') ||
href.includes('action=downloadfile') ||
href.includes('fname=');
})
.map(a => a.href);
}
""")
for dl_link in download_links:
links.add(dl_link)
# Check for ASP.NET specific elements that might contain exam links
grid_elements = await self.page.query_selector_all('table.grid, .GridView, #GridView1, .rgMasterTable, .table-responsive')
for grid in grid_elements:
grid_links = await grid.query_selector_all('a[href]')
for a in grid_links:
href = await a.get_attribute('href')
text = await a.text_content()
if href:
full_url = href if href.startswith('http') else urljoin(url, href)
links.add(full_url)
# Try clicking pagination controls to reveal more content
pagination_buttons = await self.page.query_selector_all('a[href*="page"], .pagination a, .pager a')
for i, button in enumerate(pagination_buttons[:5]): # Limit to first 5 pagination buttons
try:
# Check if this is a numeric pagination button (more likely to be useful)
button_text = await button.text_content()
if button_text and button_text.strip().isdigit():
logger.info(f"Clicking pagination button: {button_text}")
await button.click()
await self.page.wait_for_timeout(2000)
await self.page.wait_for_load_state('networkidle', timeout=10000)
# Extract links from this page
new_page_links = await self.page.evaluate("""
() => {
return Array.from(document.querySelectorAll('a[href]')).map(a => a.href);
}
""")
for href in new_page_links:
if href and not href.startswith('javascript:'):
if any(pattern in href.lower() for pattern in url_patterns) or \
any(href.lower().endswith(ext) for ext in
['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx', '.zip']):
links.add(href)
except Exception as e:
logger.warning(f"Error clicking pagination button: {e}")
# Try clicking any controls that might reveal more exam links (more focused approach)
show_buttons = await self.page.query_selector_all('input[type="button"], button, a.btn')
for button in show_buttons:
button_text = (await button.text_content() or "").lower()
button_value = (await button.get_attribute("value") or "").lower()
button_id = (await button.get_attribute("id") or "").lower()
# Look for buttons that seem likely to reveal file lists
promising_terms = ["show", "view", "display", "list", "exam", "paper", "test",
"download", "resource", "material", "browse", "file"]
if any(term in button_text or term in button_value or term in button_id
for term in promising_terms):
try:
logger.info(f"Clicking button: {button_text or button_value}")
await button.click()
await self.page.wait_for_timeout(2000)
await self.page.wait_for_load_state('networkidle', timeout=10000)
# Get any new links that appeared
new_links = await self.page.query_selector_all('a[href]')
for a in new_links:
href = await a.get_attribute('href')
if href:
full_url = href if href.startswith('http') else urljoin(url, href)
# Focus on file extensions and patterns
if any(full_url.lower().endswith(ext) for ext in
['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.xls', '.xlsx', '.zip']) or \
any(pattern in full_url.lower() for pattern in url_patterns):
links.add(full_url)
except Exception as e:
logger.warning(f"Error clicking button: {e}")
# Special handling for ASP.NET PostBack links
try:
# Find and interact with ASP.NET __doPostBack elements
postback_elements = await self.page.query_selector_all('[onclick*="__doPostBack"]')
for i, element in enumerate(postback_elements[:10]): # Limit to avoid too many clicks
try:
onclick = await element.get_attribute('onclick')
if onclick and '__doPostBack' in onclick:
element_text = await element.text_content()
# Only interact with elements that seem likely to contain exam links
promising_terms = ["show", "view", "list", "exam", "paper", "test",
"download", "resource", "material"]
if any(term in element_text.lower() for term in promising_terms):
logger.info(f"Clicking ASP.NET postback element: {element_text}")
# Click the element
await element.click()
await self.page.wait_for_timeout(2000)
await self.page.wait_for_load_state('networkidle', timeout=10000)
# Extract any new links
new_links = await self.page.query_selector_all('a[href]')
for a in new_links:
href = await a.get_attribute('href')
if href:
full_url = href if href.startswith('http') else urljoin(url, href)
if 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"Error interacting with postback element: {e}")
except Exception as e:
logger.warning(f"Error during postback handling: {e}")
except Exception as e:
logger.error(f"Browser-based extraction failed: {e}")
# Filter links to likely contain exam documents
filtered_links = []
for link in links:
# Common file extensions for exam documents
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)
continue
# Check for download links (these may not have obvious extensions)
if is_download_link(link):
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 discover_hidden_links(self, page):
"""Discover hidden links that might be in JavaScript, iframes, or dynamic content"""
hidden_links = set()
# Execute JavaScript to find links in script tags and data attributes
js_links = await page.evaluate("""
() => {
const links = new Set();
// Extract URLs from script tags
const scripts = document.querySelectorAll('script');
for (const script of scripts) {
const content = script.textContent || '';
const urlMatches = content.match(/["'](https?:\/\/[^"']+)["']/g) || [];
for (let match of urlMatches) {
links.add(match.replace(/["']/g, ''));
}
}
// Look for download-related variables in scripts
for (const script of scripts) {
const content = script.textContent || '';
// Look for common patterns for file URLs in JavaScript
if (content.includes('downloadURL') || content.includes('fileURL') ||
content.includes('pdfURL') || content.includes('documentURL')) {
// Extract potential URLs
const potentialUrls = content.match(/["']([^"']+\.(pdf|doc|docx|xls|xlsx|zip|ppt|pptx))["']/gi) || [];
for (let match of potentialUrls) {
const url = match.replace(/["']/g, '');
// Try to resolve relative URLs
if (url.startsWith('/') || !url.includes('://')) {
if (url.startsWith('/')) {
links.add(window.location.origin + url);
} else {
// Handle relative paths more carefully
const base = window.location.href.substring(0, window.location.href.lastIndexOf('/') + 1);
links.add(base + url);
}
} else if (url.startsWith('http')) {
links.add(url);
}
}
}
}
// Check for links in data attributes
const elements = document.querySelectorAll('*[data-url], *[data-href], *[data-src], *[data-link], *[data-file], *[data-download]');
for (const el of elements) {
for (const attr of ['data-url', 'data-href', 'data-src', 'data-link', 'data-file', 'data-download']) {
const val = el.getAttribute(attr);
if (val) {
// Try to resolve relative URLs
if (val.startsWith('/')) {
links.add(window.location.origin + val);
} else if (val.startsWith('http')) {
links.add(val);
} else if (!val.startsWith('javascript:') && !val.startsWith('#')) {
// Handle relative paths
const base = window.location.href.substring(0, window.location.href.lastIndexOf('/') + 1);
links.add(base + val);
}
}
}
}
// Look for URLs in inline event handlers
const clickableElements = document.querySelectorAll('*[onclick], *[onmousedown], *[onmouseup], *[href]');
for (const el of clickableElements) {
for (const attr of ['onclick', 'onmousedown', 'onmouseup', 'href']) {
const val = el.getAttribute(attr);
if (val) {
// Check for JavaScript URLs with window.location
if (val.includes('window.location') || val.includes('document.location')) {
const urlMatch = val.match(/location(?:.*)=\s*["']([^"']+)["']/);
if (urlMatch && urlMatch[1]) {
const url = urlMatch[1];
if (url.startsWith('/')) {
links.add(window.location.origin + url);
} else if (url.startsWith('http')) {
links.add(url);
} else if (!url.startsWith('javascript:') && !url.startsWith('#')) {
const base = window.location.href.substring(0, window.location.href.lastIndexOf('/') + 1);
links.add(base + url);
}
}
}
// Check for direct URLs in attributes
const urlMatches = val.match(/["'](https?:\/\/[^"']+)["']/g) || [];
for (let match of urlMatches) {
links.add(match.replace(/["']/g, ''));
}
// Check for download.php and similar patterns
if (val.includes('download.php') || val.includes('getfile.php') ||
val.includes('Action=downloadfile') || val.includes('viewfile.php')) {
// Handle both onclick handlers and direct hrefs
let url = '';
if (attr === 'href') {
url = val;
} else {
// Extract URL from JavaScript
const jsUrlMatch = val.match(/["']([^"']+(?:download|getfile|viewfile|downloadfile)[^"']*)["']/i);
if (jsUrlMatch) {
url = jsUrlMatch[1];
}
}
// Resolve URL if needed
if (url) {
if (url.startsWith('/')) {
links.add(window.location.origin + url);
} else if (url.startsWith('http')) {
links.add(url);
} else if (!url.startsWith('javascript:') && !url.startsWith('#')) {
const base = window.location.href.substring(0, window.location.href.lastIndexOf('/') + 1);
links.add(base + url);
}
}
}
}
}
}
// Find PHP/ASP file download links
const fileLinks = document.querySelectorAll('a[href*="download.php"], a[href*="getfile.php"], a[href*="viewfile.php"], a[href*="file.aspx"], a[href*="download.aspx"], a[href*="Action=downloadfile"]');
for (const link of fileLinks) {
links.add(link.href);
}
return Array.from(links);
}
""")
for link in js_links:
hidden_links.add(link)
# Extract links from iframes
iframes = await page.query_selector_all('iframe')
for iframe in iframes:
try:
frame = await iframe.content_frame()
if frame:
iframe_links = await frame.evaluate("""
() => {
return Array.from(document.querySelectorAll('a[href]'))
.map(a => a.href)
.filter(href => href.startsWith('http'));
}
""")
for link in iframe_links:
hidden_links.add(link)
except Exception as e:
logger.warning(f"Could not extract links from iframe: {e}")
# Look for links in shadow DOM (used in modern web components)
shadow_links = await page.evaluate("""
() => {
const links = new Set();
// Helper function to recursively process shadow roots
function processShadowRoot(root) {
if (!root) return;
// Get links in this shadow root
const shadowLinks = root.querySelectorAll('a[href]');
for (const link of shadowLinks) {
if (link.href && link.href.startsWith('http')) {
links.add(link.href);
}
}
// Process nested shadow roots
const elements = root.querySelectorAll('*');
for (const el of elements) {
if (el.shadowRoot) {
processShadowRoot(el.shadowRoot);
}
}
}
// Find all shadow roots in the document
const elements = document.querySelectorAll('*');
for (const el of elements) {
if (el.shadowRoot) {
processShadowRoot(el.shadowRoot);
}
}
return Array.from(links);
}
""")
for link in shadow_links:
hidden_links.add(link)
# Look for download links in forms
form_links = await page.evaluate("""
() => {
const links = new Set();
// Check for form actions that might be download endpoints
const forms = document.querySelectorAll('form');
for (const form of forms) {
const action = form.action || '';
if (action && (
action.includes('download') ||
action.includes('getfile') ||
action.includes('viewfile') ||
action.includes('Action=downloadfile')
)) {
// Collect input values that might be needed for the download
const inputs = {};
const formInputs = form.querySelectorAll('input[name]');
for (const input of formInputs) {
inputs[input.name] = input.value;
}
// Store both the form action and any important inputs
links.add(action);
}
}
return Array.from(links);
}
""")
for link in form_links:
hidden_links.add(link)
return hidden_links
async def extract_downloadable_files(self, url, custom_ext_list):
found_files = []
try:
# Normalize the URL to handle special cases
normalized_url = normalize_download_url(url)
# Skip if we've already visited this URL
if normalized_url in self.visited_urls:
logger.info(f"Skipping already visited URL: {normalized_url}")
return []
# Mark this URL as visited
self.visited_urls.add(normalized_url)
# Rotate proxy if needed
await self.rotate_proxy_if_needed()
# First check if this is a direct download link (Action=downloadfile or fname parameter)
if is_download_link(normalized_url):
logger.info(f"Processing potential direct download link: {normalized_url}")
# Try to extract the real download URL if needed
real_url = await self.extract_real_download_url(normalized_url)
# Determine filename - for complex URLs this can be tricky
filename = os.path.basename(urlparse(real_url).path)
# Handle URL-encoded filenames
if '%' in filename:
try:
filename = unquote(filename)
except Exception:
pass
# For URLs with download parameters, try to extract filename from query
if not filename or filename == '/' or filename.endswith('.php') or filename.endswith('.aspx'):
# Look for file parameter
params = parse_qs(urlparse(normalized_url).query)
# Check common filename parameters
for param in ['file', 'filename', 'name', 'fname', 'f']:
if param in params and params[param]:
potential_filename = params[param][0]
if potential_filename and '/' not in potential_filename and '\\' not in potential_filename:
filename = os.path.basename(potential_filename)
break
# If still no valid filename, use domain-based fallback
if not filename or filename == '/' or filename.endswith('.php') or filename.endswith('.aspx'):
domain = get_domain(real_url)
# Try to determine file type from content-type or extension hints in URL
ext = '.pdf' # Default
for common_ext in ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.zip']:
if common_ext in normalized_url.lower():
ext = common_ext
break
filename = f"file_from_{domain}{ext}"
# Get file size
size_str = await self.get_file_size(real_url)
# Add to found files
found_files.append({
'url': real_url,
'filename': filename,
'size': size_str,
'metadata': {},
'download_url': normalized_url # Keep original URL for downloading
})
# For direct download links, we can return early
if len(found_files) > 0 and (normalized_url.startswith(url) or real_url.startswith(url)):
return found_files
# 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 = '.pdf' # Default
for common_ext in ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.zip']:
if common_ext in link.lower():
ext = common_ext
break
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,
'download_url': link # Store original link for downloading
})
# If we found exam files with the specialized method, return them
if found_files:
return found_files
# Standard extraction method if specialized method didn't find files
response = await self.page.goto(url, timeout=30000, wait_until='networkidle')
if not response:
return []
# Check for captchas
if not await self.handle_captcha(self.page):
logger.warning("Captcha detected, file extraction may be limited")
# Scroll through the page naturally to trigger lazy loading
await self.page.evaluate("""
(async () => {
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const height = document.body.scrollHeight;
const scrollStep = Math.floor(window.innerHeight / 2);
for (let i = 0; i < height; i += scrollStep) {
window.scrollTo(0, i);
await delay(100);
}
window.scrollTo(0, 0);
})()
""")
await self.page.wait_for_timeout(1000)
final_url = self.page.url
if '.php' in final_url or 'download' in final_url:
real_url = await self.extract_real_download_url(final_url)
if real_url != final_url:
# Try to detect the filename from headers or URL
response = await self.page.request.head(real_url, timeout=15000)
filename = None
# Try to get from Content-Disposition header
content_disposition = response.headers.get('Content-Disposition', '')
if 'filename=' in content_disposition:
filename_match = re.search(r'filename=["\'](.*?)["\']', content_disposition)
if filename_match:
filename = filename_match.group(1)
# If not found in headers, use URL basename
if not filename:
filename = os.path.basename(urlparse(real_url).path)
if not filename or filename == '/':
# Generate a name based on domain
domain = get_domain(real_url)
ext = '.pdf' # Default
for common_ext in ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.zip']:
if common_ext in real_url.lower():
ext = common_ext
break
filename = f"file_from_{domain}{ext}"
found_files.append({
'url': real_url,
'filename': filename,
'size': await self.get_file_size(real_url),
'metadata': {},
'download_url': final_url # Keep original URL for downloading
})
return found_files
await self.page.wait_for_load_state('networkidle', timeout=30000)
content = await self.page.content()
soup = BeautifulSoup(content, 'html.parser')
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()])
parsed_base = urlparse(final_url)
base_url = f"{parsed_base.scheme}://{parsed_base.netloc}"
path_base = os.path.dirname(parsed_base.path)
# Process all anchor tags
for a in soup.find_all('a', href=True):
href = a['href'].strip()
if '.php' in href.lower() or 'download' in href.lower() or 'action=' in href.lower():
full_url = href if href.startswith('http') else self.resolve_relative_url(href, base_url, path_base)
real_url = await self.extract_real_download_url(full_url)
if real_url and real_url != full_url:
found_files.append({
'url': real_url,
'filename': os.path.basename(urlparse(real_url).path) or 'downloaded_file',
'size': await self.get_file_size(real_url),
'metadata': {},
'download_url': full_url # Original URL for download
})
continue
if any(href.lower().endswith(ext) for ext in all_exts):
file_url = href if href.startswith('http') else self.resolve_relative_url(href, base_url, path_base)
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,
'download_url': file_url # Same as URL for direct links
})
# 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:
# Get file info to determine type and view-only status
file_type, is_view_only = await self.get_google_drive_file_info(file_id)
# Create a more informative filename based on info
filename = f"gdrive_{file_id}"
if file_type:
filename = f"{filename}.{file_type}"
size_str = "View-only" if is_view_only else await self.get_file_size(f"https://drive.google.com/uc?export=download&id={file_id}")
found_files.append({
'url': href, # Use original URL
'filename': filename,
'size': size_str,
'metadata': {
'view_only': is_view_only,
'file_type': file_type,
'file_id': file_id
},
'download_url': href # Same as URL for Google Drive
})
# Also check for files in other elements (iframe, embed, object, etc.)
other_elements = soup.find_all(['iframe', 'embed', 'object', 'source'])
for elem in other_elements:
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 self.resolve_relative_url(src, base_url, path_base)
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,
'download_url': file_url
})
# Check for file links in onclick attributes
onclick_elements = await self.page.query_selector_all('*[onclick*="download"], *[onclick*="file"]')
for elem in onclick_elements:
onclick = await elem.get_attribute('onclick')
urls = re.findall(r'(https?://[^\'"]+)', onclick)
for url_match in urls:
if any(url_match.lower().endswith(ext) for ext in all_exts):
size_str = await self.get_file_size(url_match)
meta = {}
if url_match.lower().endswith('.pdf'):
meta = await self.get_pdf_metadata(url_match)
found_files.append({
'url': url_match,
'filename': os.path.basename(url_match.split('?')[0]),
'size': size_str,
'metadata': meta,
'download_url': url_match
})
# Also check for data-src and data-url attributes (common in lazy-loaded sites)
data_elements = await self.page.query_selector_all('[data-src], [data-url], [data-href], [data-download]')
for elem in data_elements:
for attr in ['data-src', 'data-url', 'data-href', 'data-download']:
try:
value = await elem.get_attribute(attr)
if value and any(value.lower().endswith(ext) for ext in all_exts):
file_url = value if value.startswith('http') else self.resolve_relative_url(value, base_url, path_base)
found_files.append({
'url': file_url,
'filename': os.path.basename(file_url.split('?')[0]),
'size': await self.get_file_size(file_url),
'metadata': {},
'download_url': file_url
})
except:
pass
# Check script tags for JSON data that might contain file URLs
script_elements = soup.find_all('script', type='application/json')
for script in script_elements:
try:
json_data = json.loads(script.string)
# Look for URL patterns in the JSON data
def extract_urls_from_json(obj, urls_found=None):
if urls_found is None:
urls_found = []
if isinstance(obj, dict):
for k, v in obj.items():
# Check if any key contains url-like terms
url_keys = ['url', 'href', 'src', 'link', 'file', 'path', 'download']
if any(url_key in k.lower() for url_key in url_keys) and isinstance(v, str) and v.startswith('http'):
urls_found.append(v)
else:
extract_urls_from_json(v, urls_found)
elif isinstance(obj, list):
for item in obj:
extract_urls_from_json(item, urls_found)
return urls_found
json_urls = extract_urls_from_json(json_data)
for json_url in json_urls:
if any(json_url.lower().endswith(ext) for ext in all_exts):
found_files.append({
'url': json_url,
'filename': os.path.basename(json_url.split('?')[0]),
'size': await self.get_file_size(json_url),
'metadata': {},
'download_url': json_url
})
except:
pass
# Check for hidden download buttons or forms
hidden_elements = await self.page.evaluate("""
() => {
const results = [];
// Check for hidden forms with download actions
const forms = document.querySelectorAll('form[action*="download"], form[action*="file"]');
for (const form of forms) {
const action = form.getAttribute('action') || '';
results.push({
type: 'form',
action: action,
inputs: Array.from(form.querySelectorAll('input[name]')).map(input => {
return {name: input.name, value: input.value};
})
});
}
// Check for hidden download links/buttons
const hiddenLinks = Array.from(document.querySelectorAll('a[href]')).filter(a => {
const style = window.getComputedStyle(a);
return (style.display === 'none' || style.visibility === 'hidden') &&
(a.href.includes('download') || a.href.includes('file'));
});
for (const link of hiddenLinks) {
results.push({
type: 'link',
href: link.href,
text: link.innerText || link.textContent
});
}
return results;
}
""")
# Process hidden elements
for elem in hidden_elements:
if elem['type'] == 'link' and 'href' in elem:
href = elem['href']
if any(href.lower().endswith(ext) for ext in all_exts):
found_files.append({
'url': href,
'filename': os.path.basename(href.split('?')[0]),
'size': await self.get_file_size(href),
'metadata': {},
'download_url': href
})
# Check for hidden links that might be in JavaScript, iframes, or dynamic content
hidden_links = await self.discover_hidden_links(self.page)
for link in hidden_links:
if any(link.lower().endswith(ext) for ext in all_exts):
found_files.append({
'url': link,
'filename': os.path.basename(link.split('?')[0]),
'size': await self.get_file_size(link),
'metadata': {},
'download_url': link
})
# Deduplicate files by URL
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}")
traceback.print_exc()
return []
async def download_file(self, file_info, save_dir, referer):
file_url = file_info.get('download_url', file_info['url']) # Use download_url if available
fname = file_info['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)
# Check if we've already downloaded this file
if file_url in self.downloaded_files:
logger.info(f"File already downloaded: {file_url}")
return None
try:
# Special handling for Google Drive files
if "drive.google.com" in file_url or "docs.google.com" in file_url:
# Check if it's marked as view-only in metadata
is_view_only = file_info.get('metadata', {}).get('view_only', False)
# For view-only files, try our most robust approach first
if is_view_only:
logger.info(f"Attempting to download view-only file: {file_url}")
result_path = await self.force_download_viewonly(file_info, path)
if result_path:
self.downloaded_files.add(file_url)
return result_path
# If that failed, try the regular download approach
logger.info("Primary method failed, trying fallback methods")
# Try regular download methods
success = await self.download_from_google_drive(file_url, path)
if success:
self.downloaded_files.add(file_url)
return path
# If all methods failed for Google Drive, try one last approach
logger.warning("All standard methods failed, attempting force download")
result_path = await self.force_download_viewonly(file_info, path)
if result_path:
self.downloaded_files.add(file_url)
return result_path if result_path else None
# Special handling for complex download URLs
if 'Action=downloadfile' in file_url or 'fname=' in file_url:
logger.info(f"Using browser download approach for complex URL: {file_url}")
# For these URLs, we'll need to navigate to the page and handle the download
await self.rotate_proxy_if_needed()
async with self.context.new_page() as page:
# Set up download event listener
download_promise = page.wait_for_event("download")
# Navigate to the URL
await page.goto(file_url, timeout=60000)
# Wait for the download to start
try:
download = await download_promise
await download.save_as(path)
if os.path.exists(path) and os.path.getsize(path) > 0:
self.downloaded_files.add(file_url)
return path
except Exception as e:
logger.error(f"Browser download failed: {e}")
# If download didn't start automatically, try to find and click download buttons
download_buttons = await page.query_selector_all('input[type="submit"], button[type="submit"], a.btn, a[href*="download"]')
for button in download_buttons:
try:
await button.click()
try:
download = await download_promise
await download.save_as(path)
if os.path.exists(path) and os.path.getsize(path) > 0:
self.downloaded_files.add(file_url)
return path
except:
pass
except:
continue
# If browser approach failed, try direct request as last resort
logger.info("Browser approach failed, trying direct request")
# Rotate proxy if needed
await self.rotate_proxy_if_needed()
# Try with direct requests first (faster)
try:
headers = {
'User-Agent': get_random_user_agent(),
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': referer,
'DNT': '1'
}
with requests.get(file_url, headers=headers, stream=True, timeout=30) as response:
if response.status_code == 200:
# Check content type to verify it's not HTML/error page
content_type = response.headers.get('Content-Type', '')
if 'text/html' in content_type and not file_url.endswith('.html'):
logger.warning(f"Received HTML instead of expected file: {file_url}")
else:
with open(path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
# Verify file was downloaded correctly
if os.path.exists(path) and os.path.getsize(path) > 0:
self.downloaded_files.add(file_url)
return path
except Exception as e:
logger.warning(f"Direct download failed: {e}, trying browser approach")
# Original code for non-Google Drive downloads using Playwright
async with self.context.new_page() as page:
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': referer
}
# Try to download with timeout protection
try:
response = await page.request.get(file_url, headers=headers, timeout=self.download_timeout * 1000)
if response.status == 200:
content = await response.body()
with open(path, 'wb') as f:
f.write(content)
if os.path.exists(path) and os.path.getsize(path) > 0:
self.downloaded_files.add(file_url)
return path
else:
logger.error(f"Download failed with status {response.status}: {file_url}")
# Try to extract error information
error_info = await response.text()
logger.debug(f"Error response: {error_info[:200]}...")
# Check if this might be a captcha or login issue
if detect_captcha(error_info):
logger.warning("Captcha detected during download")
# For HF Spaces, we can't implement browser-based captcha solving here
# Just log the issue for now
except PlaywrightTimeoutError:
logger.error(f"Download timed out after {self.download_timeout} seconds: {file_url}")
# Try an alternative approach - using the browser's download manager
try:
logger.info("Trying browser download manager approach")
download_promise = page.wait_for_event("download")
await page.goto(file_url, timeout=60000)
# Wait for download to start (with timeout)
download = await download_promise
await download.save_as(path)
if os.path.exists(path) and os.path.getsize(path) > 0:
self.downloaded_files.add(file_url)
return path
except Exception as e:
logger.error(f"Browser download manager approach failed: {e}")
return None
except Exception as e:
logger.error(f"Error downloading {file_url}: {e}")
return None
async def force_download_viewonly(self, file_info, save_path):
"""Completely rewritten method to handle view-only files reliably, especially multi-page PDFs"""
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
file_type = file_info.get('metadata', {}).get('file_type', 'pdf')
base, ext = os.path.splitext(save_path)
if not ext:
save_path = f"{base}.{file_type}"
logger.info(f"Starting reliable download of Google Drive file {file_id} (type: {file_type})")
# Create a dedicated browser instance with better resolution and stealth
browser_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' # Anti-detection
]
browser = await self.playwright.chromium.launch(
headless=True,
args=browser_args
)
# Use higher resolution for better quality
context = await browser.new_context(
viewport={'width': 1600, 'height': 1200},
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
device_scale_factor=2.0,
accept_downloads=True # Critical for the download workflow
)
# Add anti-detection script
await context.add_init_script("""
() => {
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
// Change plugins
Object.defineProperty(navigator, 'plugins', {
get: () => [1, 2, 3, 4, 5].map(() => ({
lengthComputable: true,
loaded: 100,
total: 100
}))
});
// Handle languages
Object.defineProperty(navigator, 'languages', {
get: () => ['en-US', 'en', 'es']
});
// Modify hardware concurrency
Object.defineProperty(navigator, 'hardwareConcurrency', {
get: () => 4
});
}
""")
page = await context.new_page()
try:
# Go to the file view page
logger.info(f"Opening file view page: https://drive.google.com/file/d/{file_id}/view")
await page.goto(f"https://drive.google.com/file/d/{file_id}/view", timeout=90000)
await page.wait_for_load_state('networkidle')
# Check for any barriers or permissions issues
content = await page.content()
if "the owner has not granted you permission to" in content:
logger.warning("Permission denied error detected")
# Randomized wait to appear more human-like
await page.wait_for_timeout(random.randint(3000, 7000))
# Create temp directory
temp_dir = tempfile.mkdtemp()
# Special handling for PDFs
if file_type.lower() == 'pdf':
# Use the improved scrolling and detection approach
# Perform some natural mouse movements and scrolling
await page.mouse.move(x=random.randint(200, 400), y=random.randint(200, 400))
await page.wait_for_timeout(random.randint(500, 1000))
# Estimate number of pages
estimated_pages = await page.evaluate("""
() => {
// Method 1: Check page counter text
const pageCounters = Array.from(document.querySelectorAll('*')).filter(el => {
const text = el.textContent || '';
return /\\d+\\s*\\/\\s*\\d+/.test(text);
});
if (pageCounters.length > 0) {
const text = pageCounters[0].textContent || '';
const match = text.match(/(\\d+)\\s*\\/\\s*(\\d+)/);
if (match && match[2]) return parseInt(match[2]);
}
// Method 2: Check actual page elements
const pageElements = document.querySelectorAll('.drive-viewer-paginated-page');
if (pageElements.length > 0) return pageElements.length;
// Method 3: Look for page thumbnails
const thumbnails = document.querySelectorAll('.drive-viewer-paginated-thumb');
if (thumbnails.length > 0) return thumbnails.length;
// Fallback: conservative guess
return 50;
}
""")
logger.info(f"Estimated {estimated_pages} pages in PDF")
# Initial scroll to trigger lazy loading
logger.info("Initial scroll to bottom to trigger lazy loading...")
await page.keyboard.press("End")
await page.wait_for_timeout(3000)
# Scroll page by page to ensure all pages are loaded
logger.info("Scrolling page by page...")
max_attempts = min(estimated_pages * 3, 300)
attempt = 0
prev_blob_count = 0
while attempt < max_attempts:
blob_count = await page.evaluate("""
Array.from(document.getElementsByTagName('img'))
.filter(img => img.src.startsWith('blob:') && img.width > 100)
.length
""")
logger.info(f"Attempt {attempt+1}: Found {blob_count} blob images")
if blob_count >= estimated_pages or (blob_count > 0 and blob_count == prev_blob_count and attempt > 10):
logger.info("All pages appear to be loaded.")
break
# Alternate between PageDown and End keys for more natural scrolling
if attempt % 3 == 0:
await page.keyboard.press("End")
else:
await page.keyboard.press("PageDown")
# Randomized wait times
await page.wait_for_timeout(random.randint(1500, 3000))
# Move mouse randomly to appear more human-like
if attempt % 4 == 0:
await page.mouse.move(x=random.randint(200, 800), y=random.randint(200, 800))
prev_blob_count = blob_count
attempt += 1
# Extra wait to ensure everything is loaded
await page.wait_for_timeout(5000)
# Set up download event listener for the PDF
download_promise = page.wait_for_event("download")
# Use jsPDF to generate PDF from loaded pages
logger.info("Generating PDF from loaded pages...")
result = await page.evaluate(r'''
(function() {
return new Promise((resolve, reject) => {
let script = document.createElement("script");
script.onload = function () {
try {
let pdf = new jsPDF();
let imgs = Array.from(document.getElementsByTagName("img"))
.filter(img => img.src.startsWith('blob:') && img.width > 100)
.sort((a, b) => {
const rectA = a.getBoundingClientRect();
const rectB = b.getBoundingClientRect();
return rectA.top - rectB.top;
});
console.log(`Found ${imgs.length} valid page images to add to PDF`);
let added = 0;
for (let i = 0; i < imgs.length; i++) {
let img = imgs[i];
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
let imgData = canvas.toDataURL("image/jpeg", 1.0);
if (added > 0) {
pdf.addPage();
}
pdf.addImage(imgData, 'JPEG', 0, 0);
added++;
}
pdf.save("download.pdf");
resolve({success: true, pageCount: added});
} catch (error) {
reject({success: false, error: error.toString()});
}
};
script.onerror = function() {
reject({success: false, error: "Failed to load jsPDF library"});
};
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
document.body.appendChild(script);
});
})();
''')
if not result.get('success', False):
logger.error(f"Error in PDF generation: {result.get('error', 'Unknown error')}")
# Try fallback approach - screenshot method
logger.info("Trying fallback screenshot method...")
# Navigate back to the first page
await page.evaluate("""
() => {
// Find and click the "first page" button if available
const buttons = Array.from(document.querySelectorAll('button'));
const firstPageBtn = buttons.find(b => b.getAttribute('aria-label')?.includes('First page'));
if (firstPageBtn) firstPageBtn.click();
}
""")
await page.wait_for_timeout(1000);
# Create a PDF by taking screenshots of each page
screenshots = []
current_page = 1
max_pages = estimated_pages
# Create a PDF using the reportlab package
while current_page <= max_pages:
screenshot_path = os.path.join(temp_dir, f"page_{current_page}.png")
# Try to find the current page element
page_elem = await page.query_selector('.drive-viewer-paginated-page')
if page_elem:
await page_elem.screenshot(path=screenshot_path)
else:
# Fallback to full page screenshot
await page.screenshot(path=screenshot_path)
screenshots.append(screenshot_path)
# Try to navigate to next page
next_btn = await page.query_selector('button[aria-label="Next page"]')
if next_btn:
is_disabled = await next_btn.get_attribute('disabled')
if is_disabled:
logger.info(f"Reached end of document at page {current_page}")
break
await next_btn.click()
await page.wait_for_timeout(1000)
current_page += 1
else:
break
# Create PDF from screenshots
if screenshots:
first_img = Image.open(screenshots[0])
width, height = first_img.size
c = canvas.Canvas(save_path, pagesize=(width, height))
for screenshot in screenshots:
img = Image.open(screenshot)
c.drawImage(screenshot, 0, 0, width, height)
c.showPage()
c.save()
# Clean up screenshots
for screenshot in screenshots:
os.remove(screenshot)
return save_path
return None
logger.info(f"PDF generation triggered with {result.get('pageCount')} pages")
# Wait for the download and save it
download = await download_promise
await download.save_as(save_path)
# Clean up temp directory
try:
os.rmdir(temp_dir)
except:
pass
else:
# Non-PDF file handling
screenshot_path = os.path.join(temp_dir, "file.png")
await page.screenshot(path=screenshot_path)
if file_type.lower() in ['doc', 'docx', 'xlsx', 'pptx']:
# For document types, try to export directly
await self.export_google_doc(file_id, file_type, save_path)
else:
# For other types, save the screenshot with appropriate extension
shutil.copy(screenshot_path, save_path)
os.remove(screenshot_path)
# Close browser
await browser.close()
# Verify file exists and has content
if os.path.exists(save_path) and os.path.getsize(save_path) > 1000:
logger.info(f"Successfully downloaded file to {save_path}")
return save_path
else:
logger.error(f"Generated file is too small or missing: {save_path}")
return None
except Exception as e:
logger.error(f"Error during force download: {e}")
if browser:
await browser.close()
return None
except Exception as e:
logger.error(f"Force download preparation failed: {e}")
return None
async def download_from_google_drive(self, url, save_path):
"""Enhanced method to download from Google Drive with multiple fallback approaches"""
# Extract the file ID from different URL formats
file_id = None
url_patterns = [
r'drive\.google\.com/file/d/([^/]+)',
r'drive\.google\.com/open\?id=([^&]+)',
r'docs\.google\.com/\w+/d/([^/]+)',
r'id=([^&]+)',
r'drive\.google\.com/uc\?id=([^&]+)',
]
for pattern in url_patterns:
match = re.search(pattern, url)
if match:
file_id = match.group(1)
break
if not file_id:
logger.error(f"Could not extract file ID from URL: {url}")
return False
# Determine file type first (important for handling different file types)
file_type, is_view_only = await self.get_google_drive_file_info(file_id)
logger.info(f"Google Drive file type: {file_type}, View-only: {is_view_only}")
base, ext = os.path.splitext(save_path)
if not ext and file_type:
# Add the correct extension if missing
save_path = f"{base}.{file_type}"
# For view-only files, use specialized approaches
if is_view_only:
# Approach 1: For PDFs, use the JS method
if file_type == 'pdf':
success = await self.download_viewonly_pdf_with_js(file_id, save_path)
if success:
return True
# Approach 2: For Google Docs, Sheets, etc., use export API
if file_type in ['doc', 'docx', 'sheet', 'ppt', 'xlsx', 'pptx']:
success = await self.export_google_doc(file_id, file_type, save_path)
if success:
return True
# Approach 3: Try the direct screenshot method for any view-only file
success = await self.download_viewonly_with_screenshots(file_id, save_path, file_type)
if success:
return True
# Try standard approaches for non-view-only files
try:
# Try direct download link first (fastest)
direct_url = f"https://drive.google.com/uc?id={file_id}&export=download&confirm=t"
# Add anti-bot headers
headers = {
'User-Agent': get_random_user_agent(),
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.9',
'Referer': 'https://drive.google.com/',
'DNT': '1'
}
# Try with streaming to handle larger files
with requests.get(direct_url, headers=headers, stream=True, timeout=60) as r:
if r.status_code == 200:
# Check if we got HTML instead of the file
content_type = r.headers.get('Content-Type', '')
if 'text/html' in content_type and not file_id.endswith('.html'):
logger.warning("Received HTML instead of file, trying with session cookies")
else:
# Looks like we got the actual file
with open(save_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
# Verify file exists and has content
if os.path.exists(save_path) and os.path.getsize(save_path) > 0:
logger.info("Direct download successful")
return True
# Try with requests and session cookies
session = requests.Session()
session.headers.update({'User-Agent': get_random_user_agent()})
# Visit the page first to get cookies
session.get(f"https://drive.google.com/file/d/{file_id}/view", timeout=30)
# Try download
url = f"https://drive.google.com/uc?id={file_id}&export=download"
response = session.get(url, stream=True, timeout=30)
# Check for confirmation token
confirmation_token = None
for k, v in response.cookies.items():
if k.startswith('download_warning'):
confirmation_token = v
break
# Use confirmation token if found
if confirmation_token:
url = f"{url}&confirm={confirmation_token}"
response = session.get(url, stream=True, timeout=60)
# Check if we're getting HTML instead of the file
content_type = response.headers.get('Content-Type', '')
if 'text/html' in content_type:
logger.warning("Received HTML instead of file - likely download restriction")
else:
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024*1024):
if chunk:
f.write(chunk)
if os.path.exists(save_path) and os.path.getsize(save_path) > 0:
with open(save_path, 'rb') as f:
content = f.read(100)
if b'<!DOCTYPE html>' not in content:
logger.info("Successfully downloaded with requests session")
return True
except Exception as e:
logger.warning(f"Requests session download failed: {e}")
# Try browser-based approach as last resort
try:
async with self.context.new_page() as page:
# Visit the file view page first to get cookies
await page.goto(f"https://drive.google.com/file/d/{file_id}/view", timeout=30000)
await page.wait_for_timeout(3000)
# Set up download event listener
download_promise = page.wait_for_event("download")
# Try to trigger the download button click
download_button = await page.query_selector('button[aria-label*="Download"], [data-tooltip*="Download"]')
if download_button:
await download_button.click()
# Wait for download to start
try:
download = await download_promise
await download.save_as(save_path)
return os.path.exists(save_path) and os.path.getsize(save_path) > 0
except Exception as e:
logger.error(f"Error during browser download: {e}")
return False
else:
# Try the export download URL
await page.goto(f"https://drive.google.com/uc?id={file_id}&export=download", timeout=30000)
# Look for and click any download buttons or links
download_elements = await page.query_selector_all('a[href*="download"], a[href*="export"], form[action*="download"], button:has-text("Download")')
for elem in download_elements:
try:
await elem.click()
# Wait a bit to see if download starts
try:
download = await download_promise
await download.save_as(save_path)
return os.path.exists(save_path) and os.path.getsize(save_path) > 0
except:
pass
except:
continue
except Exception as e:
logger.error(f"Browser-based download attempt failed: {e}")
logger.warning("All standard download methods failed")
return False
async def download_viewonly_pdf_with_js(self, file_id, save_path):
"""Download view-only PDF using the enhanced blob image caching technique"""
try:
# Create a dedicated browser instance with stealth capabilities
browser_args = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-web-security',
'--disable-blink-features=AutomationControlled' # Anti-detection
]
browser = await self.playwright.chromium.launch(
headless=True,
args=browser_args
)
# Setup stealth context
context = await browser.new_context(
viewport={'width': 1600, 'height': 1200},
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
accept_downloads=True, # Critical for handling the download event
ignore_https_errors=True
)
# Add stealth script
await context.add_init_script("""
() => {
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
// Change plugins and languages to appear more human
Object.defineProperty(navigator, 'plugins', {
get: () => [1, 2, 3, 4, 5].map(() => ({
lengthComputable: true,
loaded: 100,
total: 100
}))
});
Object.defineProperty(navigator, 'languages', {
get: () => ['en-US', 'en', 'es']
});
}
""")
page = await context.new_page()
try:
# Step 1: Navigate to the file with human-like behavior
logger.info(f"Opening view-only PDF: https://drive.google.com/file/d/{file_id}/view")
await page.goto(f"https://drive.google.com/file/d/{file_id}/view", timeout=60000)
await page.wait_for_load_state('networkidle')
# Perform human-like interactions
await page.mouse.move(x=random.randint(100, 500), y=random.randint(100, 300))
await page.wait_for_timeout(random.randint(2000, 5000))
# Step 2: Estimate the number of pages
estimated_pages = await page.evaluate("""
() => {
// Look for page counter in the interface
const pageCounters = Array.from(document.querySelectorAll('*')).filter(el => {
const text = el.textContent || '';
return /\\d+\\s*\\/\\s*\\d+/.test(text);
});
if (pageCounters.length > 0) {
const text = pageCounters[0].textContent || '';
const match = text.match(/(\\d+)\\s*\\/\\s*(\\d+)/);
if (match && match[2]) return parseInt(match[2]);
}
// If we can't find a counter, check actual pages
const pages = document.querySelectorAll('.drive-viewer-paginated-page');
if (pages.length > 0) return pages.length;
// Default to a reasonable number if we can't determine
return 50;
}
""")
logger.info(f"Estimated number of pages: {estimated_pages}")
# Step 3: Initial scroll to trigger loading
logger.info("Initial scroll to bottom to trigger lazy loading...")
await page.keyboard.press("End")
await page.wait_for_timeout(3000)
# Step 4: Wait for all pages to load with better feedback and randomization
logger.info("Scrolling through document to load all pages...")
max_attempts = min(estimated_pages * 3, 300)
attempt = 0
prev_blob_count = 0
consecutive_same_count = 0
while attempt < max_attempts:
# Count blob images (which are the PDF pages)
blob_count = await page.evaluate("""
Array.from(document.getElementsByTagName('img'))
.filter(img => img.src.startsWith('blob:') && img.width > 100)
.length
""")
logger.info(f"Attempt {attempt+1}: Found {blob_count} blob images")
# Check if we've loaded all pages or if we're stuck
if blob_count >= estimated_pages:
logger.info(f"All {estimated_pages} pages appear to be loaded.")
break
if blob_count == prev_blob_count:
consecutive_same_count += 1
if consecutive_same_count >= 5 and blob_count > 0:
logger.info(f"No new pages loaded after {consecutive_same_count} attempts. Assuming all available pages ({blob_count}) are loaded.")
break
else:
consecutive_same_count = 0
# Mix up the scrolling approach for more human-like behavior
scroll_action = random.choice(["PageDown", "End", "ArrowDown", "mouse"])
if scroll_action == "PageDown":
await page.keyboard.press("PageDown")
elif scroll_action == "End":
await page.keyboard.press("End")
elif scroll_action == "ArrowDown":
# Press arrow down multiple times
for _ in range(random.randint(5, 15)):
await page.keyboard.press("ArrowDown")
await page.wait_for_timeout(random.randint(50, 150))
else: # mouse
# Scroll using mouse wheel
current_y = random.randint(300, 700)
await page.mouse.move(x=random.randint(300, 800), y=current_y)
await page.mouse.wheel(0, random.randint(300, 800))
# Random wait between scrolls
await page.wait_for_timeout(random.randint(1000, 3000))
prev_blob_count = blob_count
attempt += 1
# Extra wait to ensure everything is fully loaded
await page.wait_for_timeout(5000)
# Step 5: Set up a download event listener
download_promise = page.wait_for_event("download")
# Step 6: Inject the jsPDF script to generate PDF
logger.info("Generating PDF from loaded pages...")
result = await page.evaluate(r'''
(function() {
return new Promise((resolve, reject) => {
let script = document.createElement("script");
script.onload = function () {
try {
let pdf = new jsPDF();
let imgs = document.getElementsByTagName("img");
let validImages = [];
// First collect all valid blob images
for (let i = 0; i < imgs.length; i++) {
let img = imgs[i];
if (!/^blob:/.test(img.src)) continue;
if (img.width < 100 || img.height < 100) continue;
validImages.push(img);
}
// Sort by position in the document
validImages.sort((a, b) => {
const rectA = a.getBoundingClientRect();
const rectB = b.getBoundingClientRect();
return rectA.top - rectB.top;
});
console.log(`Found ${validImages.length} valid page images to add to PDF`);
let added = 0;
// Process each image as a page
for (let i = 0; i < validImages.length; i++) {
let img = validImages[i];
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
let imgData = canvas.toDataURL("image/jpeg", 1.0);
if (added > 0) {
pdf.addPage();
}
pdf.addImage(imgData, 'JPEG', 0, 0);
added++;
}
pdf.save("download.pdf");
resolve({success: true, pageCount: added});
} catch (error) {
reject({success: false, error: error.toString()});
}
};
script.onerror = function() {
reject({success: false, error: "Failed to load jsPDF library"});
};
// Use a reliable CDN
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
document.body.appendChild(script);
});
})();
''')
if not result.get('success'):
logger.error(f"Error in PDF generation: {result.get('error')}")
return False
logger.info(f"PDF generation triggered with {result.get('pageCount')} pages")
# Step 7: Wait for the download to complete and save the file
download = await download_promise
# Step 8: Save the downloaded file to the specified path
await download.save_as(save_path)
logger.info(f"Successfully saved PDF to {save_path}")
return os.path.exists(save_path) and os.path.getsize(save_path) > 1000
finally:
await browser.close()
except Exception as e:
logger.error(f"Error in viewonly PDF download process: {e}")
return False
async def download_viewonly_with_screenshots(self, file_id, save_path, file_type):
"""Download any view-only file by taking screenshots"""
try:
async with self.context.new_page() as page:
# Set high-resolution viewport
await page.set_viewport_size({"width": 1600, "height": 1200})
# Navigate to the file
await page.goto(f"https://drive.google.com/file/d/{file_id}/view", wait_until='networkidle', timeout=60000)
# Make sure the file is loaded
await page.wait_for_load_state('networkidle')
await page.wait_for_timeout(3000) # Extra time for rendering
# Create directory for screenshots if multiple pages
base_dir = os.path.dirname(save_path)
base_name = os.path.splitext(os.path.basename(save_path))[0]
screenshots_dir = os.path.join(base_dir, f"{base_name}_screenshots")
os.makedirs(screenshots_dir, exist_ok=True)
# Check if it's a multi-page document
is_multi_page = await page.evaluate("""
() => {
const pages = document.querySelectorAll('.drive-viewer-paginated-page');
return pages.length > 1;
}
""")
if is_multi_page and file_type == 'pdf':
# For multi-page PDFs, take screenshots of each page
page_count = await page.evaluate("""
async () => {
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const pages = document.querySelectorAll('.drive-viewer-paginated-page');
const container = document.querySelector('.drive-viewer-paginated-scrollable');
if (!container || pages.length === 0) return 0;
// Scroll through to make sure all pages are loaded
const scrollHeight = container.scrollHeight;
const viewportHeight = container.clientHeight;
const scrollStep = viewportHeight;
for (let scrollPos = 0; scrollPos < scrollHeight; scrollPos += scrollStep) {
container.scrollTo(0, scrollPos);
await delay(300);
}
// Scroll back to top
container.scrollTo(0, 0);
await delay(300);
return pages.length;
}
""")
logger.info(f"Found {page_count} pages in document")
# Take screenshots of each page
screenshots = []
for i in range(page_count):
# Scroll to page
await page.evaluate(f"""
async () => {{
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const pages = document.querySelectorAll('.drive-viewer-paginated-page');
if (pages.length <= {i}) return false;
pages[{i}].scrollIntoView();
await delay(500);
return true;
}}
""")
# Take screenshot
screenshot_path = os.path.join(screenshots_dir, f"page_{i+1}.png")
await page.screenshot(path=screenshot_path, clip={
'x': 0,
'y': 0,
'width': 1600,
'height': 1200
})
screenshots.append(screenshot_path)
# Combine screenshots into PDF
c = canvas.Canvas(save_path)
for screenshot in screenshots:
img = Image.open(screenshot)
width, height = img.size
# Add page to PDF
c.setPageSize((width, height))
c.drawImage(screenshot, 0, 0, width, height)
c.showPage()
c.save()
# Clean up screenshots
for screenshot in screenshots:
os.remove(screenshot)
os.rmdir(screenshots_dir)
return os.path.exists(save_path) and os.path.getsize(save_path) > 0
else:
# For single-page or non-PDF files, just take one screenshot
screenshot_path = os.path.join(screenshots_dir, "screenshot.png")
await page.screenshot(path=screenshot_path, fullPage=True)
# Convert to requested format if needed
if file_type == 'pdf':
# Create PDF from screenshot
img = Image.open(screenshot_path)
width, height = img.size
c = canvas.Canvas(save_path, pagesize=(width, height))
c.drawImage(screenshot_path, 0, 0, width, height)
c.save()
else:
# Just copy the screenshot to the destination with proper extension
shutil.copy(screenshot_path, save_path)
# Clean up
os.remove(screenshot_path)
os.rmdir(screenshots_dir)
return os.path.exists(save_path) and os.path.getsize(save_path) > 0
except Exception as e:
logger.error(f"Error taking screenshots: {e}")
return False
async def export_google_doc(self, file_id, file_type, save_path):
"""Export Google Docs/Sheets/Slides to downloadable formats"""
try:
# Map file types to export formats
export_formats = {
'doc': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', # docx
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'sheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', # xlsx
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'ppt': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', # pptx
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'pdf': 'application/pdf',
}
export_format = export_formats.get(file_type, 'application/pdf')
export_url = f"https://docs.google.com/document/d/{file_id}/export?format={file_type}"
if 'sheet' in file_type or 'xlsx' in file_type:
export_url = f"https://docs.google.com/spreadsheets/d/{file_id}/export?format=xlsx"
elif 'ppt' in file_type or 'presentation' in file_type:
export_url = f"https://docs.google.com/presentation/d/{file_id}/export/pptx"
elif file_type == 'pdf':
export_url = f"https://docs.google.com/document/d/{file_id}/export?format=pdf"
async with self.context.new_page() as page:
# Get cookies from the main view page first
await page.goto(f"https://drive.google.com/file/d/{file_id}/view", wait_until='networkidle')
# Now try the export
response = await page.goto(export_url, wait_until='networkidle')
if response.status == 200:
content = await response.body()
with open(save_path, 'wb') as f:
f.write(content)
return os.path.exists(save_path) and os.path.getsize(save_path) > 0
else:
logger.warning(f"Export failed with status {response.status}")
return False
except Exception as e:
logger.error(f"Error exporting Google Doc: {e}")
return False
async def get_google_drive_file_info(self, file_id):
"""Get file type and view-only status from Google Drive"""
file_type = None
is_view_only = False
try:
async with self.context.new_page() as page:
await page.goto(f"https://drive.google.com/file/d/{file_id}/view", timeout=30000)
# Check if view-only
view_only_text = await page.query_selector('text="the owner has not granted you permission to download this file"')
is_view_only = view_only_text is not None
# Check for Google Docs viewer
gdocs_viewer = await page.query_selector('iframe[src*="docs.google.com/document"]')
gsheets_viewer = await page.query_selector('iframe[src*="docs.google.com/spreadsheets"]')
gslides_viewer = await page.query_selector('iframe[src*="docs.google.com/presentation"]')
if gdocs_viewer:
file_type = 'docx'
elif gsheets_viewer:
file_type = 'xlsx'
elif gslides_viewer:
file_type = 'pptx'
else:
# Check for PDF viewer
pdf_viewer = await page.query_selector('embed[type="application/pdf"]')
if pdf_viewer:
file_type = 'pdf'
else:
# Check for image viewer
img_viewer = await page.query_selector('img[src*="googleusercontent.com"]')
if img_viewer:
# Get image type from src
img_src = await img_viewer.get_attribute('src')
if 'jpg' in img_src or 'jpeg' in img_src:
file_type = 'jpg'
elif 'png' in img_src:
file_type = 'png'
else:
file_type = 'jpg' # Default to jpg
else:
# Generic file type fallback
file_type = 'pdf' # Default to PDF
# If still no type, check filename
if not file_type:
title_element = await page.query_selector('div[role="heading"]')
if title_element:
title = await title_element.text_content()
if title:
ext_match = re.search(r'\.([a-zA-Z0-9]+)$', title)
if ext_match:
file_type = ext_match.group(1).lower()
except Exception as e:
logger.error(f"Error getting Google Drive file info: {e}")
file_type = 'pdf' # Default to PDF if we can't determine
return file_type, is_view_only
# IMPROVED: Enhanced sublink extraction method
async def get_sublinks(self, url, limit=10000):
"""Enhanced method to extract sublinks from a website, including dynamic content and interactive elements"""
links = set()
try:
logger.info(f"Fetching sublinks from: {url}")
# Check if this is a direct download link
if is_download_link(url):
logger.info(f"URL appears to be a direct download link: {url}")
links.add(url)
return list(links)[:limit]
# Skip if we've already visited this URL
normalized_url = normalize_download_url(url)
if normalized_url in self.visited_urls:
logger.info(f"Skipping already visited URL for sublink extraction: {normalized_url}")
return list(links)[:limit]
# Add to visited URLs
self.visited_urls.add(normalized_url)
# Special handling for educational sites like phsms.cloud.ncnu.edu.tw
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 exam site sublink extraction")
edu_links = await self.get_edu_exam_links(url)
for link in edu_links:
links.add(link)
# If we found a good number of links with the specialized method, return them
if len(links) > 5:
logger.info(f"Found {len(links)} sublinks with specialized method")
return list(links)[:limit]
# Rotate proxy if needed
await self.rotate_proxy_if_needed()
# Standard sublink extraction for all sites
try:
await self.page.goto(url, timeout=30000, wait_until='networkidle')
except Exception as e:
logger.warning(f"Error navigating to URL for sublink extraction: {e}")
# Continue with what we have, we'll try to extract links anyway
# 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)
# Perform initial scrolling to load lazy content
await self.page.evaluate("""
async () => {
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const height = document.body.scrollHeight;
const step = Math.floor(window.innerHeight / 2);
for (let i = 0; i < height; i += step) {
window.scrollTo(0, i);
await delay(150);
}
window.scrollTo(0, 0);
}
""")
await self.page.wait_for_timeout(1000)
# Check if page has ASP.NET elements which might need special handling
is_aspnet = await self.page.evaluate('''
() => {
return document.querySelector('form#aspnetForm') !== null ||
document.querySelector('input[name="__VIEWSTATE"]') !== null;
}
''')
if is_aspnet:
logger.info("Detected ASP.NET page, using enhanced extraction method")
# Try to interact with ASP.NET controls that might reveal more links
# Look for dropdowns, buttons, and grid elements
dropdowns = await self.page.query_selector_all('select')
buttons = await self.page.query_selector_all('input[type="button"], input[type="submit"], button')
# Try interacting with dropdowns first
for dropdown in dropdowns:
try:
# Get all options
options = await self.page.evaluate('''
(dropdown) => {
return Array.from(dropdown.options).map(o => o.value);
}
''', dropdown)
# Try selecting each option
for option in options:
if option:
await dropdown.select_option(value=option)
await self.page.wait_for_timeout(1000)
await self.page.wait_for_load_state('networkidle', timeout=5000)
# Extract any new links that appeared
await self.extract_all_link_types(links, base_url, path_base)
except Exception as e:
logger.warning(f"Error interacting with dropdown: {e}")
# Try clicking buttons (but avoid dangerous ones like "delete")
safe_buttons = []
for button in buttons:
button_text = await button.text_content() or ""
button_value = await button.get_attribute("value") or ""
button_id = await button.get_attribute("id") or ""
combined_text = (button_text + button_value + button_id).lower()
# Skip potentially destructive buttons
if any(keyword in combined_text for keyword in ["delete", "remove", "cancel", "close", "logout"]):
continue
# Prioritize buttons that might show more content
if any(keyword in combined_text for keyword in ["view", "show", "search", "browse", "list", "go", "display"]):
safe_buttons.append(button)
# Click the safe buttons
for button in safe_buttons[:5]: # Limit to first 5 to avoid too many clicks
try:
await button.click()
await self.page.wait_for_timeout(1000)
await self.page.wait_for_load_state('networkidle', timeout=5000)
# Extract any new links that appeared
await self.extract_all_link_types(links, base_url, path_base)
except Exception as e:
logger.warning(f"Error clicking button: {e}")
# Extract links from the initial page state
await self.extract_all_link_types(links, base_url, path_base)
# Look specifically for links inside grid/table views which are common in ASP.NET applications
grid_cells = await self.page.query_selector_all('td a, tr.rgRow a, tr.rgAltRow a, .grid a, .table a')
for cell in grid_cells:
try:
href = await cell.get_attribute('href')
if href:
full_url = href if href.startswith('http') else self.resolve_relative_url(href, base_url, path_base)
links.add(full_url)
except Exception as e:
logger.warning(f"Error extracting grid link: {e}")
# Extract links from onclick attributes and javascript:__doPostBack calls
postback_links = await self.page.evaluate('''
() => {
const results = [];
// Find elements with onclick containing __doPostBack
const elements = document.querySelectorAll('*[onclick*="__doPostBack"]');
for (const el of elements) {
// Extract the postback target
const onclick = el.getAttribute('onclick') || '';
const match = onclick.match(/__doPostBack\\('([^']+)'.*?\\)/);
if (match && match[1]) {
// Get the visible text to use as description
const text = el.innerText || el.textContent || 'Link';
results.push({
id: match[1],
text: text.trim()
});
}
}
return results;
}
''')
# Try interacting with some of the postback links
for postback in postback_links[:10]: # Limit to first 10 to avoid too many interactions
try:
logger.info(f"Trying postback link: {postback['text']} ({postback['id']})")
await self.page.evaluate(f'''
() => {{
if (typeof __doPostBack === 'function') {{
__doPostBack('{postback["id"]}', '');
}}
}}
''')
await self.page.wait_for_timeout(1500)
await self.page.wait_for_load_state('networkidle', timeout=5000)
# Extract any new links that appeared
await self.extract_all_link_types(links, base_url, path_base)
except Exception as e:
logger.warning(f"Error with postback: {e}")
# Look for pagination controls and try to navigate through them
pagination_elements = await self.page.query_selector_all(
'a[href*="page"], .pagination a, .pager a, [onclick*="page"], [aria-label*="Next"]'
)
# Try clicking on pagination links (limit to max 5 pages to avoid infinite loops)
for i in range(min(5, len(pagination_elements))):
try:
# Focus on elements that look like "next page" buttons
el = pagination_elements[i]
el_text = await el.text_content() or ""
# Only click if this looks like a pagination control
if "next" in el_text.lower() or ">" == el_text.strip() or "→" == el_text.strip():
logger.info(f"Clicking pagination control: {el_text}")
await el.click()
await self.page.wait_for_timeout(2000)
await self.page.wait_for_load_state('networkidle', timeout=5000)
# Get new links from this page
await self.extract_all_link_types(links, base_url, path_base)
except Exception as e:
logger.warning(f"Error clicking pagination: {e}")
# Check for hidden links that might be revealed by JavaScript
hidden_links = await self.page.evaluate("""
() => {
// Try to execute common JavaScript patterns that reveal hidden content
try {
// Common patterns used in websites to initially hide content
const hiddenContainers = document.querySelectorAll(
'.hidden, .hide, [style*="display: none"], [style*="visibility: hidden"]'
);
// Attempt to make them visible
hiddenContainers.forEach(el => {
el.style.display = 'block';
el.style.visibility = 'visible';
el.classList.remove('hidden', 'hide');
});
// Return any newly visible links
return Array.from(document.querySelectorAll('a[href]')).map(a => a.href);
} catch (e) {
return [];
}
}
""")
# Add any newly discovered links
for href in hidden_links:
if href and not href.startswith('javascript:'):
links.add(href)
# Find all download links
download_links = await self.page.evaluate("""
() => {
return Array.from(document.querySelectorAll('a[href]'))
.filter(a => {
const href = a.href.toLowerCase();
return href.includes('download') ||
href.includes('file') ||
href.includes('get') ||
href.includes('view.php') ||
href.includes('action=') ||
href.includes('fname=');
})
.map(a => a.href);
}
""")
for download_link in download_links:
links.add(download_link)
# Also check for hidden links in JavaScript, iframes, or dynamic content
js_links = await self.discover_hidden_links(self.page)
for link in js_links:
links.add(link)
logger.info(f"Found {len(links)} sublinks")
# Prioritize download links
prioritized_links = []
normal_links = []
for link in links:
if is_download_link(link):
prioritized_links.append(link)
else:
normal_links.append(link)
# Return prioritized links first, then normal links, up to the limit
result = prioritized_links + normal_links
return result[:limit]
except Exception as e:
logger.error(f"Error getting sublinks from {url}: {e}")
return list(links)[:limit] # Return what we have so far
async def extract_all_link_types(self, links_set, base_url, path_base):
"""Extract all types of links from the current page"""
# Get all <a> tag links
a_links = await self.page.query_selector_all('a[href]')
for a in a_links:
try:
href = await a.get_attribute('href')
if href and not href.startswith('javascript:') and not href.startswith('#'):
full_url = href if href.startswith('http') else self.resolve_relative_url(href, base_url, path_base)
links_set.add(full_url)
except Exception:
pass
# Get iframe sources
iframes = await self.page.query_selector_all('iframe[src]')
for iframe in iframes:
try:
src = await iframe.get_attribute('src')
if src and not src.startswith('javascript:') and not src.startswith('about:'):
full_url = src if src.startswith('http') else self.resolve_relative_url(src, base_url, path_base)
links_set.add(full_url)
except Exception:
pass
# Get links from onclick attributes that reference URLs
onclick_elements = await self.page.query_selector_all('*[onclick*="window.location"], *[onclick*="document.location"]')
for el in onclick_elements:
try:
onclick = await el.get_attribute('onclick')
urls = re.findall(r'(https?://[^\'"]+)', onclick)
for url in urls:
links_set.add(url)
except Exception:
pass
# Look for URLs in data-* attributes
data_elements = await self.page.query_selector_all('*[data-url], *[data-href], *[data-src]')
for el in data_elements:
for attr in ['data-url', 'data-href', 'data-src']:
try:
value = await el.get_attribute(attr)
if value and not value.startswith('javascript:'):
full_url = value if value.startswith('http') else self.resolve_relative_url(value, base_url, path_base)
links_set.add(full_url)
except Exception:
pass
# Look for special anchor links that might not have href attributes
special_anchors = await self.page.query_selector_all('.rgMasterTable a, .grid a, #GridView1 a, #gvResults a')
for anchor in special_anchors:
try:
href = await anchor.get_attribute('href')
if href and not href.startswith('javascript:') and not href.startswith('#'):
full_url = href if href.startswith('http') else self.resolve_relative_url(href, base_url, path_base)
links_set.add(full_url)
except Exception:
pass
# Extract links from JSON data embedded in the page
script_elements = await self.page.query_selector_all('script[type="application/json"], script[type="text/json"]')
for script in script_elements:
try:
script_content = await script.text_content()
if script_content:
# Look for URLs in the JSON content
urls = re.findall(r'(https?://[^\'"]+)', script_content)
for url in urls:
links_set.add(url)
except Exception:
pass
def resolve_relative_url(self, relative_url, base_url, path_base):
"""Properly resolve relative URLs considering multiple formats"""
if relative_url.startswith('/'):
# Absolute path relative to domain
return f"{base_url}{relative_url}"
elif relative_url.startswith('./'):
# Explicit relative path
return f"{base_url}{path_base}/{relative_url[2:]}"
elif relative_url.startswith('../'):
# Parent directory
parent_path = '/'.join(path_base.split('/')[:-1])
return f"{base_url}{parent_path}/{relative_url[3:]}"
else:
# Regular relative path
return f"{base_url}{path_base}/{relative_url}"
async def deep_search(self, url, custom_ext_list=None, sublink_limit=10000, timeout=60):
if not custom_ext_list:
custom_ext_list = []
progress_text = st.empty()
progress_bar = st.progress(0)
file_count_text = st.empty()
try:
# Reset the visited URLs for a fresh deep search
self.visited_urls = set()
progress_text.text("Analyzing main page...")
# Special handling for ASP.NET pages
is_aspnet = False
try:
await self.page.goto(url, timeout=30000, wait_until='networkidle')
is_aspnet = await self.page.evaluate('''
() => {
return document.querySelector('form#aspnetForm') !== null ||
document.querySelector('input[name="__VIEWSTATE"]') !== null;
}
''')
except Exception:
pass
# Check if this URL is a direct download
if is_download_link(url):
progress_text.text("URL appears to be a direct download. Analyzing...")
# Try to extract file directly
normalized_url = normalize_download_url(url)
file_info = {
'url': normalized_url,
'download_url': normalized_url,
'filename': os.path.basename(urlparse(normalized_url).path) or 'download',
'size': 'Unknown Size',
'metadata': {}
}
# Add to visited URLs
self.visited_urls.add(normalized_url)
progress_bar.progress(1.0)
return [file_info]
# Extract files from main page
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 with enhanced method
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")
# Always include files from the main page, regardless of sublinks
all_files = main_files
if not sublinks:
progress_bar.progress(1.0)
return all_files
# Process each sublink
for i, sublink in enumerate(sublinks, 1):
progress = i / total_links
progress_text.text(f"Processing sublink {i}/{total_links}: {sublink}")
progress_bar.progress(progress)
try:
# Check if this is a direct download link
if is_download_link(sublink):
# For download links, just add the link directly
normalized_url = normalize_download_url(sublink)
# Skip if already visited
if normalized_url in self.visited_urls:
continue
# Mark as visited
self.visited_urls.add(normalized_url)
# Get file size if possible
size_str = await self.get_file_size(normalized_url)
# Get filename, with fallback to domain-based name
filename = os.path.basename(urlparse(normalized_url).path)
if not filename or filename == '/' or '?' in filename:
domain = get_domain(normalized_url)
ext = '.pdf' # Default extension
for common_ext in ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.txt', '.zip']:
if common_ext in normalized_url.lower():
ext = common_ext
break
filename = f"file_from_{domain}{ext}"
# Add file to results
all_files.append({
'url': normalized_url,
'download_url': normalized_url,
'filename': filename,
'size': size_str,
'metadata': {}
})
file_count_text.text(f"Found {len(all_files)} total files")
continue
# For regular links, use a longer timeout for ASP.NET pages which can be slower
sub_timeout = timeout * 2 if is_aspnet else timeout
# Skip already visited URLs
if sublink in self.visited_urls:
continue
# 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)
final_count = len(unique_files)
progress_text.text(f"Deep search complete!")
file_count_text.text(f"Found {final_count} 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():
# Custom CSS for better appearance
st.markdown("""
<style>
.stTabs [data-baseweb="tab-list"] {
gap: 10px;
}
.stTabs [data-baseweb="tab"] {
height: 50px;
white-space: pre-wrap;
border-radius: 4px 4px 0px 0px;
padding: 10px 16px;
background-color: #f0f2f6;
}
.stTabs [aria-selected="true"] {
background-color: #ffffff !important;
border-bottom: 2px solid #4c78a8;
}
.stFileUploader > div > div > button {
width: 100%;
}
.main-header {
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 10px;
}
.section-subheader {
font-size: 1.3rem;
font-weight: 600;
margin-top: 20px;
margin-bottom: 10px;
}
.info-text {
color: #6c757d;
font-size: 0.9rem;
}
.stButton>button {
width: 100%;
}
.result-card {
background-color: #f8f9fa;
border-radius: 6px;
padding: 16px;
margin-bottom: 12px;
border-left: 4px solid #4c78a8;
}
.sidebar-header {
font-size: 1.2rem;
font-weight: 600;
margin-bottom: 10px;
}
.sidebar-section {
margin-bottom: 20px;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state for storing files
if 'files' not in st.session_state:
st.session_state.files = []
if 'downloaded_paths' not in st.session_state:
st.session_state.downloaded_paths = []
if 'download_complete' not in st.session_state:
st.session_state.download_complete = False
if 'selected_tab' not in st.session_state:
st.session_state.selected_tab = 0
if 'rag_search' not in st.session_state:
st.session_state.rag_search = EnhancedRAGSearch()
if 'keep_progress' not in st.session_state:
st.session_state.keep_progress = False
if 'google_credentials' not in st.session_state:
st.session_state.google_credentials = None
if 'mode' not in st.session_state:
st.session_state.mode = "Standard"
if 'use_proxy' not in st.session_state:
st.session_state.use_proxy = False
if 'proxy_string' not in st.session_state:
st.session_state.proxy_string = None
if 'stealth_mode' not in st.session_state:
st.session_state.stealth_mode = True
# ============================
# SIDEBAR
# ============================
with st.sidebar:
st.image("https://img.icons8.com/color/96/000000/download--v1.png", width=50)
st.markdown("<p class='sidebar-header'>Advanced File Downloader</p>", unsafe_allow_html=True)
# Mode Selection
st.markdown("<div class='sidebar-section'>", unsafe_allow_html=True)
st.markdown("<p class='sidebar-header'>Mode</p>", unsafe_allow_html=True)
mode = st.radio(
"Select Mode",
["Standard", "Education Mode", "Research Mode", "Media Mode"],
label_visibility="collapsed",
index=["Standard", "Education Mode", "Research Mode", "Media Mode"].index(st.session_state.mode),
horizontal=False
)
if mode != st.session_state.mode:
st.session_state.mode = mode
# Update mode-specific settings
if mode == "Education Mode":
st.session_state.custom_extensions = ".pdf,.doc,.docx,.ppt,.pptx"
st.session_state.prioritize_pdfs = True
elif mode == "Research Mode":
st.session_state.custom_extensions = ".pdf,.txt,.csv,.json,.xlsx"
st.session_state.prioritize_pdfs = True
elif mode == "Media Mode":
st.session_state.custom_extensions = ".jpg,.png,.mp3,.mp4,.avi,.mov"
st.session_state.prioritize_pdfs = False
st.markdown(f"<div class='info-text'>Current: <b>{st.session_state.mode}</b></div>", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
# Quick Settings
st.markdown("<div class='sidebar-section'>", unsafe_allow_html=True)
st.markdown("<p class='sidebar-header'>Quick Settings</p>", unsafe_allow_html=True)
stealth_mode = st.checkbox("Stealth Mode", value=st.session_state.stealth_mode)
if stealth_mode != st.session_state.stealth_mode:
st.session_state.stealth_mode = stealth_mode
use_proxy = st.checkbox("Use Proxy", value=st.session_state.use_proxy)
if use_proxy != st.session_state.use_proxy:
st.session_state.use_proxy = use_proxy
if use_proxy:
proxy_string = st.text_input("Proxy Address",
placeholder="e.g., http://user:pass@host:port",
value=st.session_state.proxy_string or "")
if proxy_string != st.session_state.proxy_string:
st.session_state.proxy_string = proxy_string
st.markdown("</div>", unsafe_allow_html=True)
# Google Drive Integration
st.markdown("<div class='sidebar-section'>", unsafe_allow_html=True)
st.markdown("<p class='sidebar-header'>Google Drive</p>", unsafe_allow_html=True)
if st.session_state.google_credentials:
st.success("✅ Connected")
drive_folder = st.text_input("Drive Folder",
value="File Downloader" if 'drive_folder' not in st.session_state else st.session_state.drive_folder)
if 'drive_folder' not in st.session_state or drive_folder != st.session_state.drive_folder:
st.session_state.drive_folder = drive_folder
if st.button("Disconnect Drive"):
st.session_state.google_credentials = None
st.rerun()
else:
st.warning("⚠️ Not Connected")
if st.button("Connect Google Drive"):
auth_url = get_google_auth_url()
st.markdown(f"[Click here to authorize]({auth_url})")
auth_code = st.text_input("Enter authorization code:")
if auth_code:
with st.spinner("Connecting to Google Drive..."):
credentials, status_msg = exchange_code_for_credentials(auth_code)
if credentials:
st.session_state.google_credentials = credentials
st.success(status_msg)
st.rerun()
else:
st.error(status_msg)
st.markdown("</div>", unsafe_allow_html=True)
# Preset buttons for common EDU sites
if st.session_state.mode == "Education Mode":
st.markdown("<div class='sidebar-section'>", unsafe_allow_html=True)
st.markdown("<p class='sidebar-header'>Quick Access</p>", unsafe_allow_html=True)
st.markdown("<div class='info-text'>Common Educational Sites</div>", unsafe_allow_html=True)
if st.button("Past Exam Papers"):
st.session_state.preset_url = "https://pastpapers.example.edu"
st.session_state.search_method = "Exam Site Mode"
st.rerun()
if st.button("Open Course Materials"):
st.session_state.preset_url = "https://opencourseware.example.edu"
st.session_state.search_method = "Deep Search"
st.rerun()
if st.button("Research Papers"):
st.session_state.preset_url = "https://papers.example.org"
st.session_state.search_method = "Deep Search"
st.rerun()
st.markdown("</div>", unsafe_allow_html=True)
# Tool status
st.markdown("<div class='sidebar-section'>", unsafe_allow_html=True)
st.markdown("<p class='sidebar-header'>System Status</p>", unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
st.markdown("<div class='info-text'>Search</div>", unsafe_allow_html=True)
st.markdown("<div style='color: green; font-weight: bold;'>Active</div>", unsafe_allow_html=True)
with col2:
st.markdown("<div class='info-text'>Browser</div>", unsafe_allow_html=True)
st.markdown("<div style='color: green; font-weight: bold;'>Ready</div>", unsafe_allow_html=True)
if st.button("Install Dependencies"):
with st.spinner("Installing Playwright dependencies..."):
install_playwright_dependencies()
st.markdown("</div>", unsafe_allow_html=True)
# App info
st.markdown("<div class='sidebar-section' style='position: absolute; bottom: 20px; width: 90%;'>", unsafe_allow_html=True)
st.markdown("<div class='info-text' style='text-align: center;'>Version 2.0 • March 2025</div>", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
# ============================
# MAIN CONTENT AREA
# ============================
# Header section
col1, col2 = st.columns([5, 1])
with col1:
st.markdown("<h1 class='main-header'>Advanced File Downloader</h1>", unsafe_allow_html=True)
with col2:
st.image("https://img.icons8.com/color/96/000000/download--v1.png", width=70)
mode_descriptions = {
"Standard": "A versatile tool for discovering and downloading files from any website.",
"Education Mode": "Optimized for educational resources, exams, and academic materials.",
"Research Mode": "Focused on research papers, datasets, and academic publications.",
"Media Mode": "Enhanced for finding and downloading images, videos, and audio files."
}
st.markdown(f"<p class='info-text'>{mode_descriptions[st.session_state.mode]}</p>", unsafe_allow_html=True)
# Main tabs
tabs = st.tabs(["Search & Download", "Local File Search", "Advanced Configuration", "Help"])
# Tab 1: Search & Download
with tabs[0]:
st.markdown("<h2 class='section-subheader'>Find and Download Files</h2>", unsafe_allow_html=True)
col1, col2 = st.columns([3, 1])
with col1:
url = st.text_input("Enter a URL to search for downloadable files:",
placeholder="e.g., https://example.com/resources",
value=st.session_state.get('preset_url', ''))
with col2:
# Initialize search_method with either session state or default value
initial_search_method = st.session_state.get('search_method', "Deep Search")
search_method = st.selectbox("Search Method",
["Deep Search", "Quick Search", "Exam Site Mode"],
index=["Deep Search", "Quick Search", "Exam Site Mode"].index(initial_search_method))
# Update session state when changed
if search_method != st.session_state.get('search_method'):
st.session_state.search_method = search_method
# Advanced options in an expander
with st.expander("Search Options", expanded=False):
col1, col2, col3 = st.columns(3)
with col1:
depth = st.slider("Search Depth", min_value=1, max_value=5, value=2,
help="Higher values will search more links but take longer")
prioritize_pdfs = st.checkbox("Prioritize PDFs",
value=st.session_state.get('prioritize_pdfs', True),
help="Focus on finding PDF files first")
with col2:
timeout = st.slider("Timeout (seconds)", min_value=10, max_value=120, value=60)
follow_subdomains = st.checkbox("Follow Subdomains", value=True,
help="Include links from subdomains in the search")
with col3:
# Default extensions based on mode
default_extensions = {
"Standard": ".pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.zip",
"Education Mode": ".pdf,.doc,.docx,.ppt,.pptx",
"Research Mode": ".pdf,.txt,.csv,.json,.xlsx",
"Media Mode": ".jpg,.png,.mp3,.mp4,.avi,.mov"
}
custom_extensions = st.text_area(
"Custom File Extensions",
value=st.session_state.get('custom_extensions', default_extensions[st.session_state.mode]),
help="Comma-separated list of file extensions to look for"
)
# Update session state when extensions changed
if 'custom_extensions' not in st.session_state or custom_extensions != st.session_state.custom_extensions:
st.session_state.custom_extensions = custom_extensions
search_col1, search_col2 = st.columns([4, 1])
with search_col1:
search_button = st.button("🔍 Start Search", use_container_width=True)
with search_col2:
clear_button = st.button("🧹 Clear Results", use_container_width=True)
# File results section
if st.session_state.files:
st.markdown("<h3 class='section-subheader'>Found Files</h3>", unsafe_allow_html=True)
# File filtering options
filter_col1, filter_col2, filter_col3 = st.columns([2, 2, 1])
with filter_col1:
file_filter = st.text_input("Filter files by name:", placeholder="e.g., exam, 2023, etc.")
with filter_col2:
sort_option = st.selectbox("Sort by:", ["Relevance", "Name", "Size (Largest)", "Size (Smallest)"])
with filter_col3:
show_only_pdfs = st.checkbox("PDFs Only", value=False)
# Sort files based on selection
sorted_files = list(st.session_state.files)
if sort_option == "Name":
sorted_files.sort(key=lambda x: x['filename'])
elif sort_option == "Size (Largest)":
# Convert size strings to comparable values
def parse_size(size_str):
if 'Unknown' in size_str:
return 0
try:
value = float(size_str.split(' ')[0])
unit = size_str.split(' ')[1]
multipliers = {'bytes': 1, 'KB': 1024, 'MB': 1024**2, 'GB': 1024**3, 'TB': 1024**4}
return value * multipliers.get(unit, 0)
except:
return 0
sorted_files.sort(key=lambda x: parse_size(x['size']), reverse=True)
elif sort_option == "Size (Smallest)":
def parse_size(size_str):
if 'Unknown' in size_str:
return float('inf')
try:
value = float(size_str.split(' ')[0])
unit = size_str.split(' ')[1]
multipliers = {'bytes': 1, 'KB': 1024, 'MB': 1024**2, 'GB': 1024**3, 'TB': 1024**4}
return value * multipliers.get(unit, 0)
except:
return float('inf')
sorted_files.sort(key=lambda x: parse_size(x['size']))
# File list with selection
file_container = st.container()
with file_container:
selected_files = []
displayed_files = []
for i, file in enumerate(sorted_files):
# Apply filters
if file_filter and file_filter.lower() not in file['filename'].lower():
continue
if show_only_pdfs and not file['filename'].lower().endswith('.pdf'):
continue
displayed_files.append(i)
with st.container():
col1, col2, col3, col4 = st.columns([0.5, 3, 1, 1])
with col1:
selected = st.checkbox("", key=f"select_{i}", value=True)
if selected:
selected_files.append(i)
with col2:
file_icon = "📄"
if file['filename'].lower().endswith('.pdf'):
file_icon = "📝"
elif file['filename'].lower().endswith(('.doc', '.docx')):
file_icon = "📋"
elif file['filename'].lower().endswith(('.xls', '.xlsx')):
file_icon = "📊"
elif file['filename'].lower().endswith(('.ppt', '.pptx')):
file_icon = "🖼️"
elif file['filename'].lower().endswith(('.jpg', '.png', '.gif')):
file_icon = "🖼️"
elif file['filename'].lower().endswith(('.mp3', '.wav')):
file_icon = "🔊"
elif file['filename'].lower().endswith(('.mp4', '.avi', '.mov')):
file_icon = "🎬"
st.markdown(f"**{file_icon} {file['filename']}**")
st.markdown(f"<span class='info-text'>{file['url'][:60]}...</span>", unsafe_allow_html=True)
with col3:
st.markdown(f"**Size:** {file['size']}")
with col4:
st.button("Preview", key=f"preview_{i}")
st.divider()
if not displayed_files:
st.info("No files match your current filters. Try adjusting your search criteria.")
# Download options
if selected_files:
col1, col2 = st.columns(2)
with col1:
download_dir = st.text_input("Download Directory", value="downloads")
with col2:
download_option = st.radio("Download as", ["Individual Files", "ZIP Archive"], horizontal=True)
download_col1, download_col2, download_col3 = st.columns([3, 1, 1])
with download_col1:
download_button = st.button("⬇️ Download Selected Files", use_container_width=True)
with download_col2:
google_drive_button = st.button("📤 Upload to Drive",
use_container_width=True,
disabled=not st.session_state.google_credentials)
with download_col3:
select_all = st.button("Select All Files", use_container_width=True)
# Handle select all button
if select_all:
for i in displayed_files:
st.session_state[f"select_{i}"] = True
st.rerun()
# Download progress/results
if st.session_state.download_complete:
st.success(f"✅ Downloaded {len(st.session_state.downloaded_paths)} files successfully!")
download_links = []
for path in st.session_state.downloaded_paths:
with open(path, "rb") as f:
file_content = f.read()
file_name = os.path.basename(path)
download_links.append((file_name, file_content))
if len(download_links) > 0:
if download_option == "ZIP Archive":
# Create ZIP archive for download
zip_path = create_zip_file(st.session_state.downloaded_paths, download_dir)
with open(zip_path, "rb") as f:
zip_content = f.read()
st.download_button("📦 Download ZIP Archive",
zip_content,
file_name=os.path.basename(zip_path),
mime="application/zip")
else:
# Show individual file download links
st.markdown("<h4>Download Files</h4>", unsafe_allow_html=True)
# Create a grid of download buttons
cols = st.columns(3)
for idx, (name, content) in enumerate(download_links):
mime_type = mimetypes.guess_type(name)[0] or 'application/octet-stream'
with cols[idx % 3]:
st.download_button(
f"📄 {name}",
content,
file_name=name,
mime=mime_type,
key=f"dl_{name}",
use_container_width=True
)
# Tab 2: Local File Search
with tabs[1]:
st.markdown("<h2 class='section-subheader'>Search Downloaded Files</h2>", unsafe_allow_html=True)
st.write("Upload files to search through their content with AI-powered semantic search.")
# File upload
uploaded_files = st.file_uploader("Upload documents for search",
accept_multiple_files=True,
type=['pdf', 'docx', 'txt', 'csv', 'json'])
if uploaded_files:
# Build search index on upload
col1, col2 = st.columns([4, 1])
with col1:
use_transformer = st.checkbox("Use AI Transformer Model", value=HAVE_TRANSFORMERS,
help="Uses advanced AI for more accurate semantic search (if available)")
with col2:
if st.button("Build Search Index", use_container_width=True):
with st.spinner("Processing files and building search index..."):
files_added = 0
for uploaded_file in uploaded_files:
file_info = {
'filename': uploaded_file.name,
'url': f'local://{uploaded_file.name}',
'size': humanize_file_size(uploaded_file.size)
}
success = st.session_state.rag_search.add_file(uploaded_file.getvalue(), file_info)
if success:
files_added += 1
if files_added > 0:
index_built = st.session_state.rag_search.build_index()
if index_built:
st.success(f"✅ Successfully indexed {files_added} files!")
else:
st.error("Failed to build search index.")
else:
st.warning("No valid text could be extracted from the files.")
# Search interface
st.markdown("<h3 class='section-subheader'>Search Files</h3>", unsafe_allow_html=True)
col1, col2 = st.columns([4, 1])
with col1:
query = st.text_input("Enter search query:", placeholder="e.g., neural networks, climate change")
with col2:
expand_query = st.checkbox("Auto-expand query", value=True,
help="Automatically add related terms to your search")
col1, col2 = st.columns([4, 1])
with col1:
if st.button("🔍 Search Documents", use_container_width=True):
if not query:
st.warning("Please enter a search query")
else:
with st.spinner("Searching..."):
results = st.session_state.rag_search.search(query, top_k=5, search_chunks=True)
if results:
st.markdown(f"**Found {len(results)} relevant documents:**")
for i, result in enumerate(results):
with st.container():
st.markdown(f"<div class='result-card'>", unsafe_allow_html=True)
st.markdown(f"**{i+1}. {result['file_info']['filename']}** (Score: {result['score']:.2f})")
if result.get('chunk_preview'):
st.markdown("**Matching content:**")
st.text(result['chunk_preview'])
st.markdown("</div>", unsafe_allow_html=True)
else:
st.info("No matching documents found. Try a different query.")
with col2:
num_results = st.number_input("Max results", min_value=1, max_value=20, value=5)
# Quick search tips
with st.expander("Search Tips", expanded=False):
st.markdown("""
### Effective Search Tips
- **Be specific** with your queries for more accurate results
- **Try different phrasings** if you don't get the results you expect
- Use **quotation marks** for exact phrase matching
- For **complex topics**, break down your search into multiple queries
- **Combine related terms** to improve recall
The search engine uses advanced algorithms to understand the semantic meaning of your query,
not just keyword matching.
""")
# Tab 3: Advanced Configuration
with tabs[2]:
st.markdown("<h2 class='section-subheader'>Advanced Settings</h2>", unsafe_allow_html=True)
config_tabs = st.tabs(["Browser Settings", "Proxy Configuration", "Download Options", "System"])
# Browser Settings tab
with config_tabs[0]:
col1, col2 = st.columns(2)
with col1:
use_stealth = st.checkbox("Use Stealth Mode", value=st.session_state.stealth_mode,
help="Makes browser harder to detect as automated, but may be slower")
handle_captchas = st.checkbox("Handle Captchas Automatically", value=False,
help="Attempt to solve simple captchas automatically")
download_timeout = st.slider("Download Timeout (seconds)",
min_value=30, max_value=600, value=300,
help="Maximum time to wait for downloads to complete")
with col2:
user_agent = st.selectbox("User Agent", USER_AGENTS, index=0,
help="Browser identity to use when accessing websites")
save_screenshots = st.checkbox("Save Browser Screenshots", value=False,
help="Save screenshots when errors occur for debugging")
browser_lang = st.selectbox("Browser Language",
["English (US)", "English (UK)", "Spanish", "French", "German", "Chinese"],
index=0)
if st.button("Update Browser Settings"):
st.session_state.stealth_mode = use_stealth
st.success("Browser settings updated!")
# Dependency installation section
st.markdown("<h4 class='section-subheader'>Dependencies</h4>", unsafe_allow_html=True)
if st.button("Install Playwright Dependencies"):
with st.spinner("Installing dependencies..."):
install_playwright_dependencies()
# Proxy Configuration tab
with config_tabs[1]:
proxy_enabled = st.checkbox("Enable Proxy", value=st.session_state.use_proxy,
help="Route requests through a proxy server for anonymity or bypassing restrictions")
if proxy_enabled:
proxy_col1, proxy_col2 = st.columns(2)
with proxy_col1:
proxy_type = st.selectbox("Proxy Type", ["HTTP", "SOCKS5", "HTTPS"])
proxy_host = st.text_input("Proxy Host", placeholder="e.g., 127.0.0.1")
with proxy_col2:
proxy_port = st.text_input("Proxy Port", placeholder="e.g., 8080")
proxy_auth = st.text_input("Proxy Authentication (optional)",
placeholder="username:password", type="password")
st.markdown("<h4 class='section-subheader'>Proxy Rotation</h4>", unsafe_allow_html=True)
use_proxy_rotation = st.checkbox("Enable Proxy Rotation", value=False,
help="Automatically rotate between multiple proxies for better anonymity")
if use_proxy_rotation:
proxy_list = st.text_area("Proxy List (one per line)",
placeholder="http://proxy1.example.com:8080\nhttp://proxy2.example.com:8080")
rotation_interval = st.slider("Rotation Interval (requests)",
min_value=1, max_value=50, value=10,
help="How often to switch proxies")
if st.button("Save Proxy Configuration"):
# Construct the proxy string
proxy_string = None
if proxy_enabled and proxy_host and proxy_port:
proxy_prefix = f"{proxy_type.lower()}://"
proxy_auth_str = f"{proxy_auth}@" if proxy_auth else ""
proxy_string = f"{proxy_prefix}{proxy_auth_str}{proxy_host}:{proxy_port}"
# Update session state
st.session_state.use_proxy = proxy_enabled
st.session_state.proxy_string = proxy_string
# Configure proxy rotation if enabled
if use_proxy_rotation and proxy_list:
PROXY_ROTATION_CONFIG["enabled"] = True
PROXY_ROTATION_CONFIG["rotation_interval"] = rotation_interval
PROXY_ROTATION_CONFIG["proxies"] = [p.strip() for p in proxy_list.splitlines() if p.strip()]
st.success("Proxy configuration updated!")
# Download Options tab
with config_tabs[2]:
col1, col2 = st.columns(2)
with col1:
st.markdown("<h4 class='section-subheader'>Download Behavior</h4>", unsafe_allow_html=True)
skip_existing = st.checkbox("Skip Existing Files", value=True,
help="Don't download files that already exist locally")
auto_rename = st.checkbox("Auto-Rename Duplicates", value=True,
help="Automatically rename files instead of overwriting")
verify_downloads = st.checkbox("Verify Downloads", value=True,
help="Check file integrity after download")
max_retries = st.slider("Max Retries", min_value=0, max_value=10, value=3,
help="Number of times to retry failed downloads")
with col2:
st.markdown("<h4 class='section-subheader'>File Organization</h4>", unsafe_allow_html=True)
auto_organize = st.checkbox("Auto-Organize Files", value=True,
help="Automatically organize files by type")
default_dir = st.text_input("Default Download Directory", value="downloads",
help="Default location to save downloaded files")
org_by_domain = st.checkbox("Organize by Domain", value=False,
help="Create subdirectories based on source domains")
org_by_type = st.checkbox("Organize by File Type", value=False,
help="Create subdirectories based on file types")
if st.button("Save Download Settings"):
st.session_state.download_settings = {
"skip_existing": skip_existing,
"auto_rename": auto_rename,
"verify_downloads": verify_downloads,
"max_retries": max_retries,
"auto_organize": auto_organize,
"default_dir": default_dir,
"org_by_domain": org_by_domain,
"org_by_type": org_by_type
}
st.success("Download settings saved!")
# System tab
with config_tabs[3]:
col1, col2 = st.columns(2)
with col1:
st.markdown("<h4 class='section-subheader'>Memory & Performance</h4>", unsafe_allow_html=True)
max_concurrent = st.slider("Max Concurrent Downloads", min_value=1, max_value=10, value=3,
help="Maximum number of simultaneous downloads")
memory_limit = st.slider("Memory Limit (MB)", min_value=256, max_value=4096, value=1024,
help="Maximum memory to use for file processing")
processing_threads = st.slider("Processing Threads", min_value=1, max_value=8, value=2,
help="Number of threads to use for file processing")
with col2:
st.markdown("<h4 class='section-subheader'>Logs & Diagnostics</h4>", unsafe_allow_html=True)
log_level = st.selectbox("Log Level", ["DEBUG", "INFO", "WARNING", "ERROR"], index=1,
help="Detail level for application logs")
save_debug_info = st.checkbox("Save Debug Information", value=False,
help="Save detailed information about program execution")
log_dir = st.text_input("Log Directory", value="logs",
help="Directory to save log files")
if st.button("Apply System Settings"):
st.session_state.system_settings = {
"max_concurrent": max_concurrent,
"memory_limit": memory_limit,
"processing_threads": processing_threads,
"log_level": log_level,
"save_debug_info": save_debug_info,
"log_dir": log_dir
}
# Update logging configuration
log_level_num = getattr(logging, log_level)
logging.getLogger().setLevel(log_level_num)
st.success("System settings applied!")
# Reset application button
st.markdown("<h4 class='section-subheader'>Application Control</h4>", unsafe_allow_html=True)
reset_col1, reset_col2 = st.columns([1, 3])
with reset_col1:
if st.button("Reset Application", use_container_width=True):
for key in list(st.session_state.keys()):
if key != 'google_credentials': # Preserve Google auth
del st.session_state[key]
st.success("Application has been reset!")
st.rerun()
with reset_col2:
st.info("This will clear all search results, downloaded files, and reset settings to defaults.")
# Advanced cleanup options
st.markdown("<h4 class='section-subheader'>Advanced Options</h4>", unsafe_allow_html=True)
adv_col1, adv_col2 = st.columns(2)
with adv_col1:
clear_cache = st.button("Clear Cache", use_container_width=True)
if clear_cache:
# Clear cached files and temporary data
temp_dir = tempfile.gettempdir()
try:
for f in os.listdir(temp_dir):
if f.startswith("playwright") or f.startswith("download"):
try:
os.remove(os.path.join(temp_dir, f))
except:
pass
st.success("Cache cleared successfully!")
except Exception as e:
st.error(f"Error clearing cache: {e}")
with adv_col2:
export_settings = st.button("Export Settings", use_container_width=True)
if export_settings:
# Export current settings to JSON
settings = {
"mode": st.session_state.mode,
"stealth_mode": st.session_state.stealth_mode,
"use_proxy": st.session_state.use_proxy,
"proxy_string": st.session_state.proxy_string,
"custom_extensions": st.session_state.get("custom_extensions", ""),
"prioritize_pdfs": st.session_state.get("prioritize_pdfs", True),
"system_settings": st.session_state.get("system_settings", {}),
"download_settings": st.session_state.get("download_settings", {})
}
settings_json = json.dumps(settings, indent=2)
b64 = base64.b64encode(settings_json.encode()).decode()
href = f'data:application/json;base64,{b64}'
st.markdown(f'<a href="{href}" download="file_downloader_settings.json">Download Settings File</a>', unsafe_allow_html=True)
# Tab 4: Help
with tabs[3]:
st.markdown("<h2 class='section-subheader'>Help & Documentation</h2>", unsafe_allow_html=True)
help_tabs = st.tabs(["Quick Start", "Advanced Features", "Troubleshooting", "About"])
with help_tabs[0]:
st.markdown("""
### Getting Started
1. **Enter a URL** on the Search & Download tab
2. Select a **Search Method**:
- **Deep Search**: Thorough but slower
- **Quick Search**: Fast but may miss some files
- **Exam Site Mode**: Optimized for educational resource sites
3. Click **Start Search** to find downloadable files
4. Select files you want to download
5. Click **Download Selected Files**
#### Using Different Modes
Select a mode from the sidebar to optimize the tool for different use cases:
- **Standard Mode**: Balanced for general use
- **Education Mode**: Optimized for finding academic materials
- **Research Mode**: Better for research papers and datasets
- **Media Mode**: Enhanced for finding images, videos, and audio
For best results with educational materials, use the **Exam Site Mode** with websites that contain past exams, papers, or course materials.
""")
with help_tabs[1]:
st.markdown("""
### Advanced Features
- **Local File Search**: Upload files and search through their content using the enhanced RAG search
- **Custom Extensions**: Specify additional file types to look for beyond the default set
- **Stealth Mode**: Makes the browser harder to detect as automated, useful for sites that block scrapers
- **Proxy Support**: Use proxies to access region-restricted content or improve anonymity
- **Google Drive Integration**: Upload downloaded files directly to your Google Drive
#### Search Tips
- For educational sites, include specific terms like "exam", "test", "paper" in the URL
- When using Local File Search, try different variations of your query for better results
- Use filtering and sorting options to find the most relevant files quickly
#### File Organization
You can configure automatic file organization in the Advanced Configuration tab:
- **Organize by Domain**: Creates folders based on the source website
- **Organize by File Type**: Separates files into folders by their extension
- **Auto-Rename**: Prevents overwriting existing files with same names
""")
with help_tabs[2]:
st.markdown("""
### Troubleshooting
#### Common Issues
- **No files found**: Try using Deep Search with higher depth value, or add more specific file extensions
- **Downloads failing**: Check if the site requires authentication or uses captchas
- **Slow performance**: Reduce search depth or disable stealth mode for faster results
- **Browser errors**: Click "Install Playwright Dependencies" in Advanced Settings
#### Captcha Issues
Some websites use captchas to prevent automated access. If you encounter captchas:
1. Try using a different proxy
2. Enable "Handle Captchas Automatically" for simple captchas
3. For complex captchas, you may need to manually access the site first
#### Proxy Problems
If you're having issues with proxies:
1. Verify your proxy is working with an external tool
2. Check that you've entered the correct format (http://host:port)
3. Some websites may block known proxy IPs
#### Memory Usage
If the application is using too much memory:
1. Reduce the "Memory Limit" in System settings
2. Process fewer files at once
3. Use lower search depth values
""")
with help_tabs[3]:
st.markdown("""
### About This Tool
**Advanced File Downloader** is a sophisticated tool designed to discover and download files from websites with enhanced capabilities for educational resources.
#### Key Features
- **Smart Discovery**: Finds downloadable files even when they're not directly linked
- **Enhanced RAG Search**: Search through downloaded documents using advanced AI techniques
- **Educational Focus**: Specialized detection for exam papers and academic resources
- **Stealth Capabilities**: Avoids detection by anti-scraping measures
#### Technical Details
This tool uses:
- **Playwright**: For browser automation and stealth capabilities
- **Sentence Transformers**: For AI-powered semantic search
- **Streamlit**: For the user interface
- **Google Drive API**: For cloud integration
#### Credits
Created with Python, Streamlit, Playwright, and various AI libraries.
For issues or suggestions, please contact the developer.
Version 2.0 - March 2025
""")
# Handle search and download actions
if search_button and url:
# Reset files and downloaded paths
st.session_state.files = []
st.session_state.downloaded_paths = []
st.session_state.download_complete = False
# Clear the preset URL if it was used
if 'preset_url' in st.session_state:
st.session_state.preset_url = ''
# Prepare custom extensions
custom_ext_list = [ext.strip() for ext in custom_extensions.split(",") if ext.strip()]
# Configure proxy from session state
proxy_string = st.session_state.proxy_string if st.session_state.use_proxy else None
# Set up proxy rotation if enabled
if 'use_proxy_rotation' in locals() and use_proxy_rotation and proxy_list:
PROXY_ROTATION_CONFIG["enabled"] = True
PROXY_ROTATION_CONFIG["rotation_interval"] = rotation_interval
PROXY_ROTATION_CONFIG["proxies"] = [p.strip() for p in proxy_list.splitlines() if p.strip()]
# Configure search parameters based on method
sublink_limit = 5000 if search_method == "Deep Search" else 1000
search_depth = depth if search_method == "Deep Search" else 1
is_exam_site = search_method == "Exam Site Mode"
# Execute the search asynchronously
async def run_search():
async with DownloadManager(
use_proxy=st.session_state.use_proxy,
proxy=proxy_string,
use_stealth=st.session_state.stealth_mode,
proxy_rotation=PROXY_ROTATION_CONFIG["enabled"]
) as manager:
# For exam sites, use specialized approach
if is_exam_site:
st.session_state.keep_progress = True
edu_links = await manager.get_edu_exam_links(url)
all_files = []
progress_text = st.empty()
progress_bar = st.progress(0)
# Process each exam link
for i, link in enumerate(edu_links):
progress = (i+1) / max(1, len(edu_links))
progress_text.text(f"Processing exam link {i+1}/{len(edu_links)}: {link}")
progress_bar.progress(progress)
files = await manager.extract_downloadable_files(link, custom_ext_list)
all_files.extend(files)
st.session_state.files = all_files
progress_text.empty()
progress_bar.empty()
st.session_state.keep_progress = False
else:
# Use general search method
files = await manager.deep_search(url, custom_ext_list, sublink_limit, timeout)
st.session_state.files = files
# Run the search
asyncio.run(run_search())
st.rerun()
# Handle download button
if 'download_button' in locals() and download_button and selected_files:
# Create download directory
os.makedirs(download_dir, exist_ok=True)
# Reset download state
st.session_state.downloaded_paths = []
st.session_state.download_complete = False
# Get selected files
files_to_download = [st.session_state.files[i] for i in selected_files]
# Execute the download asynchronously
async def run_download():
async with DownloadManager(
use_proxy=st.session_state.use_proxy,
proxy=st.session_state.proxy_string,
use_stealth=st.session_state.stealth_mode
) as manager:
download_progress = st.progress(0)
status_text = st.empty()
for i, file_info in enumerate(files_to_download):
progress = (i) / len(files_to_download)
status_text.text(f"Downloading {i+1}/{len(files_to_download)}: {file_info['filename']}")
download_progress.progress(progress)
downloaded_path = await manager.download_file(
file_info,
download_dir,
get_domain(file_info['url'])
)
if downloaded_path:
st.session_state.downloaded_paths.append(downloaded_path)
download_progress.progress(1.0)
status_text.text(f"Downloaded {len(st.session_state.downloaded_paths)}/{len(files_to_download)} files successfully!")
st.session_state.download_complete = True
# Run the download
asyncio.run(run_download())
st.rerun()
# Handle Google Drive upload
if 'google_drive_button' in locals() and google_drive_button and st.session_state.google_credentials and st.session_state.downloaded_paths:
with st.spinner("Uploading to Google Drive..."):
drive_service = googleapiclient.discovery.build("drive", "v3", credentials=st.session_state.google_credentials)
# Create folder if it doesn't exist
folder_id = None
folder_name = st.session_state.drive_folder if 'drive_folder' in st.session_state else "File Downloader"
# Check if folder exists
query = f"name='{folder_name}' and mimeType='application/vnd.google-apps.folder' and trashed=false"
results = drive_service.files().list(q=query, spaces='drive', fields='files(id)').execute()
items = results.get('files', [])
if not items:
# Create folder
folder_id = create_drive_folder(drive_service, folder_name)
else:
folder_id = items[0]['id']
# Upload each file
upload_progress = st.progress(0)
status_text = st.empty()
uploaded_count = 0
for i, path in enumerate(st.session_state.downloaded_paths):
progress = i / len(st.session_state.downloaded_paths)
status_text.text(f"Uploading {i+1}/{len(st.session_state.downloaded_paths)}: {os.path.basename(path)}")
upload_progress.progress(progress)
result = google_drive_upload(path, st.session_state.google_credentials, folder_id)
if isinstance(result, str) and not result.startswith("Error"):
uploaded_count += 1
upload_progress.progress(1.0)
status_text.text(f"Uploaded {uploaded_count}/{len(st.session_state.downloaded_paths)} files to Google Drive folder '{folder_name}'")
st.success(f"✅ Files uploaded to Google Drive successfully!")
# Handle clear button
if clear_button:
st.session_state.files = []
st.session_state.downloaded_paths = []
st.session_state.download_complete = False
if 'preset_url' in st.session_state:
st.session_state.preset_url = ''
st.rerun()
if __name__ == "__main__":
main() |