File size: 108,858 Bytes
efcb36e 5134f3b efcb36e 5134f3b efcb36e 5134f3b efcb36e 5134f3b efcb36e 5134f3b efcb36e 5134f3b efcb36e 5134f3b efcb36e 5134f3b efcb36e 5134f3b efcb36e | 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 | """
Backend logic for Offliner.
All download, search, and media-resolution logic is encapsulated inside the
``OfflinerCore`` class. A module-level singleton (``_core``) is created on
import so that existing callers (routes.py) can keep using the same public
function names via thin backward-compatible wrappers defined at the bottom of
this file.
"""
from __future__ import annotations
import concurrent.futures
import json as _json
import logging
import os
import re
import shutil
import tempfile
import threading
import time
import unicodedata
import urllib.parse
import uuid
import zipfile
from pathlib import Path
from typing import Any, Callable
import redis as _redis
import requests
import spotipy
import yt_dlp
from spotipy.oauth2 import SpotifyClientCredentials
from ytmusicapi import YTMusic
# Proxy configuration is handled by the ProxyRotator class (see below).
# Set PROXY_URL to one or more comma-separated proxy URLs.
# Optional: rapidfuzz for faster fuzzy matching
try:
from rapidfuzz import fuzz as _rfuzz
_RAPIDFUZZ_AVAILABLE = True
except ImportError:
from difflib import SequenceMatcher as _SequenceMatcher
_RAPIDFUZZ_AVAILABLE = False
logger = logging.getLogger(__name__)
# ============================================
# Custom exceptions
# ============================================
class _ProxyBlockedError(Exception):
"""Raised when a download fails due to a blocked / non-working proxy."""
# ============================================
# Thread-safe proxy rotator
# ============================================
class ProxyRotator:
"""Thread-safe rotating proxy manager.
Parses the ``PROXY_URL`` environment variable (one or more proxy URLs
separated by commas) and exposes a ``current`` property plus a ``rotate``
method that advances to the next proxy in the list.
When a proxy is detected as blocked by YouTube (or otherwise unusable),
callers invoke ``rotate()`` and retry the operation transparently.
"""
_PROXY_ERROR_PATTERNS: list[str] = [
r"Sign in to confirm",
r"HTTP Error 403",
r"HTTP Error 429",
r"blocked",
r"proxy.*error",
r"Unable to download webpage",
r"urlopen error",
r"Connection refused",
r"Connection reset",
r"Tunnel connection failed",
r"ProxyError",
r"ConnectTimeoutError",
r"SOCKSHTTPSConnectionPool",
r"Read timed out",
r"IncompleteRead",
r"RemoteDisconnected",
r"No such file or directory.*cookie",
]
def __init__(self) -> None:
self._lock = threading.Lock()
raw = os.getenv("PROXY_URL", "")
self._proxies: list[str] = [p.strip() for p in raw.split(",") if p.strip()]
self._current_index: int = 0
self._apply_env_vars()
if self._proxies:
logger.info(
f"ProxyRotator initialised with {len(self._proxies)} proxy(ies). "
f"Active: {self._proxies[0]}"
)
else:
logger.info("ProxyRotator: no proxies configured")
# -- Public API --
@property
def current(self) -> str | None:
"""Return the currently active proxy URL, or *None* if none configured."""
with self._lock:
if not self._proxies:
return None
return self._proxies[self._current_index]
@property
def count(self) -> int:
"""Number of configured proxies."""
return len(self._proxies)
def rotate(self) -> str | None:
"""Advance to the next proxy in the list and return it.
Wraps around to the first proxy after the last one.
Thread-safe.
"""
with self._lock:
if not self._proxies:
return None
old_idx = self._current_index
self._current_index = (self._current_index + 1) % len(self._proxies)
new_proxy = self._proxies[self._current_index]
self._apply_env_vars_unlocked()
logger.warning(
f"ProxyRotator: rotating from proxy #{old_idx} "
f"to #{self._current_index} → {new_proxy}"
)
return new_proxy
def is_proxy_error(self, error: BaseException) -> bool:
"""Return *True* when *error* looks like a proxy / IP-block issue."""
msg = str(error)
for pattern in self._PROXY_ERROR_PATTERNS:
if re.search(pattern, msg, re.IGNORECASE):
return True
return False
# -- Internal helpers --
def _apply_env_vars(self) -> None:
with self._lock:
self._apply_env_vars_unlocked()
def _apply_env_vars_unlocked(self) -> None:
"""Set ``http_proxy`` / ``https_proxy`` env vars. Caller must hold *_lock*."""
if self._proxies:
url = self._proxies[self._current_index]
os.environ["http_proxy"] = url
os.environ["https_proxy"] = url
os.environ["HTTP_PROXY"] = url
os.environ["HTTPS_PROXY"] = url
else:
for key in ("http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"):
os.environ.pop(key, None)
# Module-level singleton created at import time.
_proxy_rotator = ProxyRotator()
# ============================================
# Thread-safe TTL cache
# ============================================
_CACHE_MISS = object()
class _TTLCache:
"""Bounded, thread-safe cache with per-entry TTL expiry."""
def __init__(self, maxsize: int = 256, ttl: float = 600.0) -> None:
self._data: dict[Any, tuple[float, Any]] = {}
self._lock = threading.Lock()
self._maxsize = maxsize
self._ttl = ttl
def get(self, key: Any) -> Any:
"""Return cached value or ``_CACHE_MISS``."""
with self._lock:
entry = self._data.get(key)
if entry is not None:
ts, val = entry
if time.time() - ts < self._ttl:
return val
del self._data[key]
return _CACHE_MISS
def put(self, key: Any, value: Any) -> None:
with self._lock:
now = time.time()
# Purge expired
expired = [k for k, (ts, _) in self._data.items() if now - ts >= self._ttl]
for k in expired:
del self._data[k]
# Evict oldest if at capacity
while len(self._data) >= self._maxsize:
oldest_key = min(self._data, key=lambda k: self._data[k][0])
del self._data[oldest_key]
self._data[key] = (now, value)
_yt_search_cache = _TTLCache(maxsize=512, ttl=600.0)
_ytm_search_cache = _TTLCache(maxsize=256, ttl=600.0)
# ============================================
# Global Download Progress Store (SSE support) — Redis-backed
# ============================================
# Module-level Redis connection pool. Initialised lazily the first time any
# DownloadProgressStore method is called, or eagerly via `init_redis()`.
_redis_client: _redis.Redis | None = None
# Default TTL for progress entries (seconds). Entries auto-expire so that
# stale sessions never accumulate in Redis.
_PROGRESS_TTL: int = 3600 # 1 hour
def init_redis(redis_url: str | None = None) -> _redis.Redis:
"""Initialise (or re-initialise) the module-level Redis client.
Called once during application startup from ``app.py``. If *redis_url* is
``None``, the ``REDIS_URL`` environment variable is used (falling back to
``redis://localhost:6379/0``).
"""
global _redis_client # noqa: PLW0603
url = redis_url or os.getenv("REDIS_URL", "redis://localhost:6379/0")
_redis_client = _redis.Redis.from_url(url, decode_responses=True)
logger.info(f"Redis client initialised ({url})")
return _redis_client
def get_redis() -> _redis.Redis:
"""Return the module-level Redis client, initialising lazily if needed."""
global _redis_client # noqa: PLW0603
if _redis_client is None:
init_redis()
assert _redis_client is not None
return _redis_client
class DownloadProgressStore:
"""Redis-backed global store for real-time download progress per request_id.
Used by SSE endpoints in routes.py to stream live progress to the frontend.
Updated by yt-dlp progress hooks during downloads.
Each request_id maps to a Redis key ``progress:{request_id}`` holding a
JSON-serialised dict. Keys expire after ``_PROGRESS_TTL`` seconds.
"""
_KEY_PREFIX: str = "progress:"
# ------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------
@classmethod
def _key(cls, request_id: str) -> str:
return f"{cls._KEY_PREFIX}{request_id}"
# ------------------------------------------------------------------
# Public API (same signatures as the old in-memory implementation)
# ------------------------------------------------------------------
@classmethod
def create(cls, request_id: str, total_items: int = 1) -> None:
r = get_redis()
data = {
"percent": 0,
"status": "Preparing...",
"detail": "",
"speed": "",
"eta": "",
"current_file": "",
"completed_items": 0,
"total_items": total_items,
"phase": "preparing",
"complete": False,
"error": None,
"file_path": None,
"temp_dir": None,
# Flag that indicates a client requested cancellation (disconnect)
"cancel_requested": False,
}
r.set(cls._key(request_id), _json.dumps(data), ex=_PROGRESS_TTL)
@classmethod
def request_cancel(cls, request_id: str) -> None:
"""Mark a running request as cancelled so worker threads can abort."""
r = get_redis()
raw = r.get(cls._key(request_id))
if raw is None:
return
data = _json.loads(raw)
data["cancel_requested"] = True
r.set(cls._key(request_id), _json.dumps(data), keepttl=True)
@classmethod
def is_cancelled(cls, request_id: str) -> bool:
r = get_redis()
raw = r.get(cls._key(request_id))
if raw is None:
return False
data = _json.loads(raw)
return bool(data.get("cancel_requested"))
@classmethod
def update(cls, request_id: str, **kwargs: Any) -> None:
r = get_redis()
raw = r.get(cls._key(request_id))
if raw is None:
return
data = _json.loads(raw)
data.update(kwargs)
r.set(cls._key(request_id), _json.dumps(data), keepttl=True)
@classmethod
def get(cls, request_id: str) -> dict:
r = get_redis()
raw = r.get(cls._key(request_id))
if raw is None:
return {
"percent": 0,
"status": "Unknown",
"detail": "",
"speed": "",
"eta": "",
"complete": False,
"error": "Session not found",
}
return _json.loads(raw)
@classmethod
def remove(cls, request_id: str) -> None:
r = get_redis()
r.delete(cls._key(request_id))
# ============================================
# Download Result (thread-safe bookkeeping)
# ============================================
class _DownloadResult:
"""Thread-safe accumulator for per-session download statistics."""
__slots__ = (
"_lock",
"audios_exito",
"audios_error",
"videos_exito",
"videos_error",
"canciones_descargadas",
"progress_callback",
"total_items",
"completed_items",
"request_id",
)
def __init__(
self,
progress_callback: Callable | None = None,
request_id: str | None = None,
) -> None:
self._lock = threading.Lock()
self.audios_exito: int = 0
self.audios_error: int = 0
self.videos_exito: int = 0
self.videos_error: int = 0
self.canciones_descargadas: list[str] = []
self.progress_callback = progress_callback
self.total_items: int = 0
self.completed_items: int = 0
self.request_id: str | None = request_id
# -- Thread-safe mutators --
def inc_success(self, mode: str) -> None:
with self._lock:
if mode == "audio":
self.audios_exito += 1
else:
self.videos_exito += 1
def inc_error(self, mode: str) -> None:
with self._lock:
if mode == "audio":
self.audios_error += 1
else:
self.videos_error += 1
def add_file(self, path: str) -> None:
with self._lock:
self.canciones_descargadas.append(path)
def inc_completed(self) -> None:
with self._lock:
self.completed_items += 1
def get_progress_pct(self) -> int:
with self._lock:
if self.total_items <= 0:
return 15
return 15 + int((self.completed_items / self.total_items) * 70)
# ============================================
# OfflinerCore
# ============================================
class OfflinerCore:
"""Central class encapsulating all Offliner download / search logic.
Designed as a **singleton** for long-lived API clients (Spotify, YTMusic).
All request-specific state (paths, cookies, results) is either passed as
method arguments or scoped locally — never stored on ``self``.
"""
# --- Class-level constants ------------------------------------------------
SPONSORBLOCK_CATEGORIES: dict[str, str] = {
"sponsor": "Sponsors (promociones pagadas)",
"intro": "Intros/Animaciones de entrada",
"outro": "Outros/Créditos finales",
"selfpromo": "Auto-promoción del creador",
"preview": "Previews/Avances",
"filler": "Relleno/Contenido no musical",
"interaction": "Recordatorios de suscripción/interacción",
"music_offtopic": "Partes sin música en videos musicales",
}
_SIDECAR_EXTENSIONS = (".jpg", ".png", ".webp", ".vtt", ".srt", ".ass")
_VIDEO_TAG_PATTERNS: list[str] = [
r"\(Official\s*(Music\s*)?Video\)",
r"\(Official\s*Audio\)",
r"\(Official\s*Lyric\s*Video\)",
r"\(Video\s*Oficial\)",
r"\(Audio\s*Oficial\)",
r"\(Visualizer\)",
r"\[Official\s*(Music\s*)?Video\]",
r"\[Official\s*Audio\]",
r"\(HD\)",
r"\(HQ\)",
r"\(4K\)",
r"\(1080p\)",
r"\(720p\)",
]
_DEFAULT_MAX_WORKERS: int = 4
# ------------------------------------------------------------------
# Initialization
# ------------------------------------------------------------------
def __init__(self) -> None:
self._base_dir: Path = Path(__file__).resolve().parent
# One-time ffmpeg check at startup
self._ffmpeg_available: bool = shutil.which("ffmpeg") is not None
if not self._ffmpeg_available:
logger.warning("ffmpeg not found in PATH; post-processing will fail")
# API clients (graceful init — never raise)
self.ytmusic: YTMusic | None = self._init_ytmusic()
self._spotify_client_id: str = os.getenv("SPOTIFY_CLIENT_ID", "")
self._spotify_client_secret: str = os.getenv("SPOTIFY_CLIENT_SECRET", "")
self._spotify_default: spotipy.Spotify | None = self._init_spotify(
self._spotify_client_id, self._spotify_client_secret
)
@staticmethod
def _init_ytmusic() -> YTMusic | None:
try:
client = YTMusic()
logger.info("YTMusic client initialized")
return client
except Exception as e:
logger.warning(f"Could not initialize YTMusic: {e}")
return None
@staticmethod
def _init_spotify(client_id: str, client_secret: str) -> spotipy.Spotify | None:
if not (client_id and client_secret):
logger.warning("Spotify credentials not configured")
return None
try:
ccm = SpotifyClientCredentials(client_id, client_secret)
client = spotipy.Spotify(
client_credentials_manager=ccm, requests_timeout=10
)
logger.info("Spotify client initialized successfully")
return client
except Exception as e:
logger.warning(f"Could not initialize Spotify client: {e}")
return None
# ------------------------------------------------------------------
# Fuzzy matching (rapidfuzz preferred, SequenceMatcher fallback)
# ------------------------------------------------------------------
@staticmethod
def _normalize_text(text: str) -> str:
"""Lower-case, strip parenthetical/bracket tags, collapse whitespace."""
text = text.lower()
text = re.sub(r"\([^)]*\)|\[[^\]]*\]", "", text)
text = re.sub(r"[^\w\s]", " ", text)
return re.sub(r"\s+", " ", text).strip()
def _is_match(self, query: str, candidate: str, threshold: float = 0.6) -> bool:
"""Return *True* when *query* and *candidate* are sufficiently similar."""
return self._match_score(query, candidate) >= threshold
def _match_score(self, query: str, candidate: str) -> float:
"""Return a similarity ratio in [0, 1]."""
q = self._normalize_text(query)
c = self._normalize_text(candidate)
if _RAPIDFUZZ_AVAILABLE:
return _rfuzz.ratio(q, c) / 100.0
return _SequenceMatcher(None, q, c).ratio()
# ------------------------------------------------------------------
# Per-session cookie management
# ------------------------------------------------------------------
@staticmethod
def _setup_cookies(config: dict, session_dir: Path) -> Path | None:
"""Provision a session-local cookie file from *config* and return its
path, or ``None`` when no cookies are configured.
Supported config keys
---------------------
* ``cookies_content`` – raw Netscape cookie-jar text.
* ``cookies_filepath`` – path to an existing cookie file on disk.
The resulting file is written *inside* ``session_dir`` so it is
automatically destroyed during session cleanup. Cookie content is
**never logged** for security.
"""
cookies_content = config.get("cookies_content")
cookies_filepath = config.get("cookies_filepath")
if cookies_content:
cookie_path = session_dir / "cookies.txt"
try:
cookie_path.write_text(cookies_content, encoding="utf-8")
logger.info("Per-session cookie file created (from content)")
return cookie_path
except Exception as e:
logger.error(f"Failed to write cookie file: {e}")
return None
if cookies_filepath:
src = Path(cookies_filepath)
if src.is_file():
cookie_path = session_dir / "cookies.txt"
try:
shutil.copy2(str(src), str(cookie_path))
logger.info("Per-session cookie file created (from filepath)")
return cookie_path
except Exception as e:
logger.error(f"Failed to copy cookie file: {e}")
return None
else:
logger.warning("Cookie filepath provided but file not found")
return None
return None
# ------------------------------------------------------------------
# Path / filename utilities (pathlib-based)
# ------------------------------------------------------------------
def _ensure_dir(self, path: Path | str) -> Path:
"""Create *path* (and parents) if it doesn't exist; return it."""
p = Path(path) if not isinstance(path, Path) else path
if not p.is_absolute():
p = self._base_dir / p
p.mkdir(parents=True, exist_ok=True)
return p
@staticmethod
def _sanitize_filename(title: str) -> str:
"""Remove illegal Windows characters and normalize to ASCII."""
name = title.strip()
name = re.sub(r'[<>:"/\\|?*]', "", name)
name = re.sub(r"\.+$", "", name.strip())
name = re.sub(r"\s+", " ", name).strip()
if len(name) > 200:
name = name[:200].strip()
try:
ascii_name = (
unicodedata.normalize("NFKD", name)
.encode("ascii", "ignore")
.decode("ascii")
)
ascii_name = re.sub(r"\s+", " ", ascii_name).strip()
if ascii_name:
name = ascii_name
except Exception:
pass
return name
# ------------------------------------------------------------------
# yt-dlp option builders
# ------------------------------------------------------------------
@staticmethod
def _base_ytdlp_opts(cookie_file: Path | None = None) -> dict[str, Any]:
"""Base options shared by every yt-dlp invocation.
When *cookie_file* is provided, ``cookiefile`` is added to the dict
so yt-dlp authenticates with those cookies.
"""
opts: dict[str, Any] = {
"quiet": True,
"no_warnings": True,
"extractor_retries": 10,
"fragment_retries": 10,
"file_access_retries": 5,
"retry_sleep_functions": {"http": lambda n: min(2**n, 30)},
"socket_timeout": 60,
"http_chunk_size": 10485760,
# Use the web client when user cookies are present so yt-dlp sends
# them to the same surface that issued the cookies. Android client
# plus web cookies can trigger 400 responses from YouTube.
"extractor_args": {
"youtube": {
"player_client": ["web"] if cookie_file else ["android_music"]
},
},
"http_headers": {
"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-Language": "en-US,en;q=0.9",
},
"nocheckcertificate": True,
"check_formats": "selected",
"force_ipv4": True,
"continuedl": False,
"overwrites": True,
"cachedir": False,
"encoding": "utf-8",
}
# Proxy configuration via the global rotating proxy manager.
proxy = _proxy_rotator.current
if proxy:
opts["proxy"] = proxy
if cookie_file:
opts["cookiefile"] = str(cookie_file)
return opts
def _sponsorblock_postprocessors(self, config: dict) -> list[dict]:
"""Build SponsorBlock postprocessor chain from user config."""
if not config.get("SponsorBlock_enabled"):
return []
categories = config.get("SponsorBlock_categories", [])
if not categories:
return []
# SponsorBlockPP expects only `categories` and optional `api`.
# The previous code passed an `action` argument (e.g. 'remove'/'chapter')
# which is not accepted and caused failures. We instead always request
# SponsorBlock chapters for the relevant categories and then use
# `ModifyChapters` to remove the selected categories when needed.
all_cats = list(self.SPONSORBLOCK_CATEGORIES.keys())
pp: list[dict] = [
{
"key": "SponsorBlock",
"api": "https://sponsor.ajay.app",
"categories": all_cats,
},
{
"key": "ModifyChapters",
"remove_sponsor_segments": categories,
"force_keyframes": False,
},
]
logger.info(f"SponsorBlock enabled. Removing: {', '.join(categories)}")
return pp
# ------------------------------------------------------------------
# URL detection (static — no instance state needed)
# ------------------------------------------------------------------
@staticmethod
def detect_url_source(url: str) -> str | None:
"""Return ``'spotify'``, ``'youtube_music'``, ``'youtube'``, or *None*."""
if not url:
return None
u = url.lower()
if "spotify.com" in u:
return "spotify"
if "music.youtube.com" in u:
return "youtube_music"
if "youtube.com" in u or "youtu.be" in u:
return "youtube"
return None
@staticmethod
def is_playlist_url(url: str) -> bool:
"""Detect YouTube / YouTube Music / Spotify playlist or album URLs."""
if not url:
return False
u = url.lower()
patterns = [
"youtube.com/playlist?list=",
"youtube.com/watch?v=.*&list=",
"youtube.com/watch?.*list=",
"music.youtube.com/playlist?list=",
"music.youtube.com/watch?v=.*&list=",
"youtu.be/.*[?&]list=",
]
for p in patterns:
if p.replace(".*", "") in u or (
".*" in p and all(part in u for part in p.split(".*"))
):
return True
if "spotify.com" in u and ("/playlist/" in u or "/album/" in u):
return True
return False
@staticmethod
def extract_youtube_video_id(url: str) -> str | None:
"""Extract the 11-character YouTube video ID from *url*."""
if not url:
return None
m = re.search(
r"(?:youtube\.com/watch\?v=|youtu\.be/|youtube\.com/embed/"
r"|youtube\.com/v/|music\.youtube\.com/watch\?v=)"
r"([a-zA-Z0-9_-]{11})",
url,
)
if m:
return m.group(1)
if re.match(r"^[a-zA-Z0-9_-]{11}$", url):
return url
return None
# ------------------------------------------------------------------
# SponsorBlock API
# ------------------------------------------------------------------
def get_sponsorblock_segments(
self, video_id: str, categories: list[str] | None = None
) -> dict:
"""Query the SponsorBlock API for skip-segments."""
empty: dict = {
"has_segments": False,
"segments": [],
"total_duration_removed": 0,
"categories_found": [],
}
cats = categories or list(self.SPONSORBLOCK_CATEGORIES.keys())
try:
resp = requests.get(
f"https://sponsor.ajay.app/api/skipSegments?videoID={video_id}",
timeout=5,
)
if resp.status_code == 404:
return empty
if resp.status_code != 200:
logger.warning(f"SponsorBlock API returned status {resp.status_code}")
return empty
filtered = [s for s in resp.json() if s.get("category") in cats]
if not filtered:
return empty
total = sum(s["segment"][1] - s["segment"][0] for s in filtered)
return {
"has_segments": True,
"segments": filtered,
"total_duration_removed": total,
"categories_found": list({s["category"] for s in filtered}),
}
except requests.RequestException as e:
logger.error(f"SponsorBlock request error: {e}")
return empty
except Exception as e:
logger.error(f"SponsorBlock unexpected error: {e}")
return empty
# ------------------------------------------------------------------
# Spotify helpers
# ------------------------------------------------------------------
def _get_spotify_client(self, config: dict) -> spotipy.Spotify | None:
"""Return a Spotify client — custom credentials override the default."""
custom_id = config.get("Client_ID", "")
custom_secret = config.get("Secret_ID", "")
if custom_id and custom_id != self._spotify_client_id:
try:
ccm = SpotifyClientCredentials(custom_id, custom_secret)
return spotipy.Spotify(
client_credentials_manager=ccm, requests_timeout=10
)
except Exception as e:
logger.error(f"Custom Spotify client failed: {e}")
return self._spotify_default
def _resolve_spotify_track(self, config: dict, url: str) -> str:
"""Convert a Spotify track URL into a YouTube URL via search."""
try:
client = self._get_spotify_client(config)
if not client:
logger.error("No Spotify client available")
return ""
if "spotify.com" not in url or "/track/" not in url:
logger.warning(f"Invalid Spotify URL: {url}")
return ""
track_id = url.split("/track/")[1].split("?")[0].split("/")[0]
logger.info(f"Extracting Spotify track info: {track_id}")
info = client.track(track_id)
query = f"{info['name']} {info['artists'][0]['name']}"
logger.info(f"Searching YouTube for: {query}")
return self.search_youtube(query)
except Exception as e:
logger.error(f"Error resolving Spotify track: {e}")
return ""
# ------------------------------------------------------------------
# Search (with TTL caching)
# ------------------------------------------------------------------
def search_youtube(self, query: str) -> str:
"""Return the URL of the first YouTube result for *query*, or ``""``.
Results are cached for 10 minutes to save API quota.
"""
cached = _yt_search_cache.get(query)
if cached is not _CACHE_MISS:
logger.info(f"YouTube search cache hit: '{query}'")
return cached
result = self._search_youtube_impl(query)
_yt_search_cache.put(query, result)
return result
def _search_youtube_impl(self, query: str) -> str:
"""Actual YouTube search via yt-dlp (uncached), with proxy rotation."""
max_attempts = max(_proxy_rotator.count, 1)
for attempt in range(max_attempts):
try:
logger.info(f"Searching YouTube: {query}")
opts = self._base_ytdlp_opts()
opts.update({"extract_flat": True, "default_search": "ytsearch1"})
with yt_dlp.YoutubeDL(opts) as ydl:
info = ydl.extract_info(f"ytsearch1:{query}", download=False)
if info and info.get("entries"):
entry = info["entries"][0]
vid = (entry.get("id") or "") if entry else ""
if vid:
link = f"https://www.youtube.com/watch?v={vid}"
logger.info(f"Video found: {link}")
return link
logger.warning(f"No results for: {query}")
return ""
except Exception as e:
if (
_proxy_rotator.count > 1
and _proxy_rotator.is_proxy_error(e)
and attempt < max_attempts - 1
):
_proxy_rotator.rotate()
continue
logger.error(f"Error searching YouTube: {e}")
return ""
def search_youtube_music(
self, title: str, artist: str | None = None
) -> tuple[str | None, str | None, str | None]:
"""Search YouTube Music and return ``(url, title, artist)`` or three *None*s.
Results are cached for 10 minutes.
"""
cache_key = (title.strip().lower(), (artist or "").strip().lower())
cached = _ytm_search_cache.get(cache_key)
if cached is not _CACHE_MISS:
logger.info(f"YouTube Music cache hit: '{title}'")
return cached
result = self._search_youtube_music_impl(title, artist)
_ytm_search_cache.put(cache_key, result)
return result
def _search_youtube_music_impl(
self, title: str, artist: str | None = None
) -> tuple[str | None, str | None, str | None]:
"""Actual YouTube Music search (uncached)."""
if not self.ytmusic:
logger.warning("YTMusic not available")
return None, None, None
try:
search_q = title.strip()
for pat in self._VIDEO_TAG_PATTERNS:
search_q = re.sub(pat, "", search_q, flags=re.IGNORECASE)
search_q = search_q.replace("||", " ").replace("|", " ").replace("#", " ")
search_q = re.sub(r"\s+", " ", search_q).strip()
logger.info(f"Searching on YouTube Music: '{search_q}'")
results = self.ytmusic.search(search_q, filter="songs", limit=15)
if not results:
results = self.ytmusic.search(search_q, limit=15)
if not results:
logger.warning(f"No results found for '{search_q}'")
return None, None, None
# Build a combined query string for fuzzy comparison
query_combined = f"{title} {artist or ''}".strip()
best: dict | None = None
best_score: float = 0.0
for r in results:
if not r.get("videoId"):
continue
r_title = r.get("title", "")
r_artists = r.get("artists", [])
r_artist = r_artists[0].get("name", "") if r_artists else ""
result_combined = f"{r_title} {r_artist}".strip()
score = self._match_score(query_combined, result_combined)
if score > best_score:
best_score = score
best = r
if best and best_score >= 0.5:
vid = best["videoId"]
t = best.get("title", search_q)
arts = best.get("artists", [])
a = arts[0].get("name", "Unknown") if arts else "Unknown"
url = f"https://music.youtube.com/watch?v={vid}"
logger.info(f"Match found ({best_score:.0%}): '{t}' by '{a}'")
return url, t, a
logger.warning(
f"No sufficient match for '{title}' (best: {best_score:.0%})"
)
return None, None, None
except Exception as e:
logger.error(f"Error searching YouTube Music: {e}")
return None, None, None
# ------------------------------------------------------------------
# Media info
# ------------------------------------------------------------------
def get_media_info(self, url: str, config: dict | None = None) -> dict | None:
"""Return basic info (title, thumbnail, author, duration) for a single item."""
if not url:
return None
probe_dir: Path | None = None
cookie_file: Path | None = None
try:
if config:
probe_dir = Path(tempfile.mkdtemp(prefix="offliner-probe-"))
cookie_file = self._setup_cookies(config, probe_dir)
source = self.detect_url_source(url)
if source == "spotify":
return self._get_spotify_info(url)
return self._get_youtube_info(url, source or "youtube", cookie_file)
except Exception as e:
logger.error(f"Error getting media info: {e}")
return None
finally:
if probe_dir and probe_dir.exists():
shutil.rmtree(probe_dir, ignore_errors=True)
def _get_youtube_info(
self, url: str, source: str = "youtube", cookie_file: Path | None = None
) -> dict | None:
max_attempts = max(_proxy_rotator.count, 1)
for attempt in range(max_attempts):
try:
opts = self._base_ytdlp_opts(cookie_file)
if cookie_file:
# For metadata probes with authenticated cookies, let yt-dlp
# auto-select the client surface; forcing a specific client can
# return 400/sign-in errors in some accounts.
opts.pop("extractor_args", None)
opts.update(
{
"extract_flat": False,
"skip_download": True,
# Probing metadata should not fail just because a selected
# format is unavailable for the current auth context.
"check_formats": None,
}
)
with yt_dlp.YoutubeDL(opts) as ydl:
info = ydl.extract_info(url, download=False)
if not info:
return None
dur_s = info.get("duration", 0) or 0
dur_str = f"{dur_s // 60}:{dur_s % 60:02d}" if dur_s else "0:00"
thumb = ""
for t in reversed(info.get("thumbnails", [])):
if t.get("url"):
thumb = t["url"]
break
thumb = thumb or info.get("thumbnail", "")
return {
"titulo": info.get("title", "Sin título"),
"thumbnail": thumb,
"autor": info.get(
"uploader", info.get("channel", "Desconocido")
),
"duracion": dur_str,
"duracion_segundos": dur_s,
"fuente": source,
}
except Exception as e:
if (
_proxy_rotator.count > 1
and _proxy_rotator.is_proxy_error(e)
and attempt < max_attempts - 1
):
_proxy_rotator.rotate()
continue
logger.error(f"Error getting YouTube info: {e}")
return None
return None
def _get_spotify_info(self, url: str) -> dict | None:
client = self._get_spotify_client({})
if not client:
logger.warning("Spotify not configured")
return None
try:
if "/track/" in url:
tid = url.split("/track/")[1].split("?")[0].split("/")[0]
track = client.track(tid)
if not track:
return None
artists = (
", ".join(
a["name"] for a in track.get("artists", []) if a.get("name")
)
or "Desconocido"
)
imgs = track.get("album", {}).get("images", [])
dur_ms = track.get("duration_ms", 0)
dur_s = dur_ms // 1000
return {
"titulo": track.get("name", "Sin título"),
"thumbnail": imgs[0]["url"] if imgs else "",
"autor": artists,
"duracion": f"{dur_s // 60}:{dur_s % 60:02d}",
"duracion_segundos": dur_s,
"fuente": "spotify",
}
elif "/album/" in url:
aid = url.split("/album/")[1].split("?")[0].split("/")[0]
album = client.album(aid)
if not album:
return None
artists = (
", ".join(
a["name"] for a in album.get("artists", []) if a.get("name")
)
or "Desconocido"
)
imgs = album.get("images", [])
total = album.get("total_tracks", 0)
return {
"titulo": album.get("name", "Sin título"),
"thumbnail": imgs[0]["url"] if imgs else "",
"autor": artists,
"duracion": f"{total} tracks",
"duracion_segundos": 0,
"fuente": "spotify",
"es_playlist": True,
}
return None
except Exception as e:
logger.error(f"Error getting Spotify info: {e}")
return None
# ------------------------------------------------------------------
# Playlist info
# ------------------------------------------------------------------
def get_playlist_info(
self,
url: str,
config: dict | None = None,
max_items: int | None = None,
) -> dict | None:
"""Full playlist metadata + item list (YouTube / YTM / Spotify)."""
probe_dir: Path | None = None
cookie_file: Path | None = None
try:
if config:
probe_dir = Path(tempfile.mkdtemp(prefix="offliner-probe-"))
cookie_file = self._setup_cookies(config, probe_dir)
if "spotify.com" in url:
if "/playlist/" in url:
return self._spotify_playlist_info(url, max_items=max_items)
if "/album/" in url:
return self._spotify_album_info(url, max_items=max_items)
is_ytm = "music.youtube.com" in url
playlist_id: str | None = None
if "list=" in url:
parsed = urllib.parse.urlparse(url)
params = urllib.parse.parse_qs(parsed.query)
playlist_id = params.get("list", [None])[0]
if is_ytm and self.ytmusic and playlist_id:
result = self._ytmusic_playlist_info(playlist_id, max_items=max_items)
if result:
return result
normalized = (
url.replace("music.youtube.com", "www.youtube.com") if is_ytm else url
)
return self._ytdlp_playlist_info(
normalized,
cookie_file,
max_items=max_items,
)
except Exception as e:
logger.error(f"Error getting playlist info: {e}")
return None
finally:
if probe_dir and probe_dir.exists():
shutil.rmtree(probe_dir, ignore_errors=True)
# --- Spotify playlist / album info ----------------------------------------
def _spotify_playlist_info(
self,
url: str,
max_items: int | None = None,
) -> dict | None:
client = self._get_spotify_client({})
if not client:
logger.error("Spotify client not available")
return None
try:
pid = url.split("/playlist/")[1].split("?")[0].split("/")[0]
logger.info(f"Getting Spotify playlist info: {pid}")
playlist = client.playlist(pid)
if not playlist:
return None
info: dict[str, Any] = {
"titulo": playlist.get("name", "Playlist sin título"),
"descripcion": playlist.get("description", ""),
"autor": playlist.get("owner", {}).get("display_name", "Desconocido"),
"total": playlist.get("tracks", {}).get("total", 0),
"thumbnail": (
playlist.get("images", [{}])[0].get("url", "")
if playlist.get("images")
else ""
),
"items": [],
}
offset, limit = 0, 100
while True:
page = client.playlist_tracks(
pid,
offset=offset,
limit=limit,
fields="items(track(id,name,artists,duration_ms,album(images))),next,total",
)
if not page or not page.get("items"):
break
for item in page["items"]:
track = item.get("track")
if not track or not track.get("id"):
continue
info["items"].append(self._spotify_track_to_item(track))
if max_items and len(info["items"]) > max_items:
info["total"] = len(info["items"])
return info
if not page.get("next"):
break
offset += limit
info["total"] = len(info["items"])
logger.info(
f"Spotify playlist obtained: '{info['titulo']}' "
f"with {info['total']} tracks"
)
return info
except Exception as e:
logger.error(f"Error getting Spotify playlist: {e}")
return None
def _spotify_album_info(
self,
url: str,
max_items: int | None = None,
) -> dict | None:
client = self._get_spotify_client({})
if not client:
logger.error("Spotify client not available")
return None
try:
aid = url.split("/album/")[1].split("?")[0].split("/")[0]
logger.info(f"Getting Spotify album info: {aid}")
album = client.album(aid)
if not album:
return None
album_artist = (
", ".join(a["name"] for a in album.get("artists", [])) or "Desconocido"
)
thumb = (
album.get("images", [{}])[0].get("url", "")
if album.get("images")
else ""
)
info: dict[str, Any] = {
"titulo": album.get("name", "Album sin título"),
"descripcion": "",
"autor": album_artist,
"total": album.get("total_tracks", 0),
"thumbnail": thumb,
"items": [],
}
offset, limit = 0, 50
while True:
page = client.album_tracks(aid, offset=offset, limit=limit)
if not page or not page.get("items"):
break
for track in page["items"]:
if not track or not track.get("id"):
continue
item = self._spotify_track_to_item(track)
# Album tracks don't carry album images; reuse the album thumb
item["thumbnail"] = thumb
info["items"].append(item)
if max_items and len(info["items"]) > max_items:
info["total"] = len(info["items"])
return info
if not page.get("next"):
break
offset += limit
info["total"] = len(info["items"])
logger.info(
f"Spotify album obtained: '{info['titulo']}' "
f"with {info['total']} tracks"
)
return info
except Exception as e:
logger.error(f"Error getting Spotify album: {e}")
return None
@staticmethod
def _spotify_track_to_item(track: dict) -> dict:
"""Convert a Spotify track dict into a standard playlist item dict."""
artists = ", ".join(
a.get("name", "") for a in track.get("artists", []) if a.get("name")
)
dur_s = (track.get("duration_ms", 0) or 0) // 1000
album_imgs = track.get("album", {}).get("images", [])
return {
"id": track["id"],
"titulo": track.get("name", "Sin título"),
"url": f"https://open.spotify.com/track/{track['id']}",
"duracion": f"{dur_s // 60}:{dur_s % 60:02d}",
"duracion_segundos": dur_s,
"thumbnail": album_imgs[0].get("url", "") if album_imgs else "",
"autor": artists,
}
# --- YouTube Music playlist info ------------------------------------------
def _ytmusic_playlist_info(
self,
playlist_id: str,
max_items: int | None = None,
) -> dict | None:
try:
logger.info(
f"Getting YouTube Music playlist using ytmusicapi: {playlist_id}"
)
probe_limit = (max_items + 1) if max_items else None
pl = self.ytmusic.get_playlist(playlist_id, limit=probe_limit) # type: ignore[union-attr]
if not pl or "tracks" not in pl:
return None
info: dict[str, Any] = {
"titulo": pl.get("title", "Playlist sin título"),
"descripcion": pl.get("description", ""),
"autor": pl.get("author", {}).get("name", "Desconocido"),
"total": len(pl.get("tracks", [])),
"thumbnail": (
pl.get("thumbnails", [{}])[-1].get("url", "")
if pl.get("thumbnails")
else ""
),
"items": [],
}
for t in pl.get("tracks", []):
vid = t.get("videoId", "")
if not vid:
continue
dur_txt = t.get("duration", "")
dur_s = self._parse_duration_str(dur_txt)
artists = ", ".join(
a.get("name", "") for a in t.get("artists", []) if a.get("name")
)
thumbs = t.get("thumbnails", [])
thumb = (
thumbs[-1].get("url", "")
if thumbs
else f"https://i.ytimg.com/vi/{vid}/mqdefault.jpg"
)
info["items"].append(
{
"id": vid,
"video_id": vid,
"titulo": t.get("title", "Sin título"),
"url": f"https://www.youtube.com/watch?v={vid}",
"duracion": dur_txt or "--:--",
"duracion_segundos": dur_s,
"thumbnail": thumb,
"autor": artists,
}
)
info["total"] = len(info["items"])
logger.info(
f"Playlist obtained via ytmusicapi: '{info['titulo']}' "
f"with {info['total']} items"
)
return info
except Exception as e:
logger.warning(f"Error with ytmusicapi, using yt-dlp: {e}")
return None
# --- yt-dlp playlist info -------------------------------------------------
def _ytdlp_playlist_info(
self,
url: str,
cookie_file: Path | None = None,
max_items: int | None = None,
) -> dict | None:
max_proxy_attempts = max(_proxy_rotator.count, 1)
for _proxy_attempt in range(max_proxy_attempts):
try:
return self._ytdlp_playlist_info_inner(url, cookie_file, max_items)
except Exception as e:
if (
_proxy_rotator.count > 1
and _proxy_rotator.is_proxy_error(e)
and _proxy_attempt < max_proxy_attempts - 1
):
_proxy_rotator.rotate()
continue
logger.error(f"Error in yt-dlp playlist info: {e}")
return None
return None
def _ytdlp_playlist_info_inner(
self,
url: str,
cookie_file: Path | None = None,
max_items: int | None = None,
) -> dict | None:
opts = self._base_ytdlp_opts(cookie_file)
if cookie_file:
opts.pop("extractor_args", None)
opts.update(
{
"extract_flat": "in_playlist",
"skip_download": True,
"ignoreerrors": True,
"playlistend": (max_items + 1) if max_items else None,
"extractor_retries": 3,
"socket_timeout": 30,
"check_formats": None,
}
)
with yt_dlp.YoutubeDL(opts) as ydl:
result = ydl.extract_info(url, download=False)
if not result:
return None
if result.get("_type") != "playlist" and "entries" not in result:
return None
info: dict[str, Any] = {
"titulo": result.get("title", "Playlist sin título"),
"descripcion": result.get("description", ""),
"autor": result.get("uploader", result.get("channel", "Desconocido")),
"total": result.get("playlist_count", 0),
"thumbnail": result.get("thumbnail", ""),
"items": [],
}
entries = result.get("entries", [])
if entries and not isinstance(entries, list):
entries = list(entries)
for entry in entries or []:
if entry is None:
continue
dur_s = entry.get("duration", 0) or 0
dur_str = (
f"{int(dur_s // 60)}:{int(dur_s % 60):02d}" if dur_s else "--:--"
)
vid = entry.get("id", entry.get("url", ""))
if vid and not vid.startswith("http"):
v_url = f"https://www.youtube.com/watch?v={vid}"
else:
v_url = entry.get("url", "")
thumb = entry.get("thumbnail", "") or (
f"https://i.ytimg.com/vi/{vid}/mqdefault.jpg" if vid else ""
)
info["items"].append(
{
"id": vid,
"video_id": vid,
"titulo": entry.get("title", "Sin título"),
"url": v_url,
"duracion": dur_str,
"duracion_segundos": dur_s,
"thumbnail": thumb,
"autor": entry.get("uploader", entry.get("channel", "")),
}
)
info["total"] = len(info["items"])
logger.info(
f"Playlist obtained: '{info['titulo']}' " f"with {info['total']} items"
)
return info
# ------------------------------------------------------------------
# Playlist URL resolution (download-time)
# ------------------------------------------------------------------
def _resolve_playlist_urls(
self, config: dict, platform: str, url: str
) -> list[str]:
"""Return a flat list of downloadable URLs from a playlist."""
try:
logger.info(f"Getting songs from {platform} playlist...")
urls: list[str] = []
if platform == "YouTube":
max_proxy_attempts = max(_proxy_rotator.count, 1)
for _pa in range(max_proxy_attempts):
try:
opts = self._base_ytdlp_opts()
opts.update(
{"extract_flat": True, "force_generic_extractor": True}
)
with yt_dlp.YoutubeDL(opts) as ydl:
result = ydl.extract_info(url, download=False)
if result and "entries" in result:
urls = [e["url"] for e in result["entries"]]
break
except Exception as _pe:
if (
_proxy_rotator.count > 1
and _proxy_rotator.is_proxy_error(_pe)
and _pa < max_proxy_attempts - 1
):
_proxy_rotator.rotate()
continue
raise
elif platform == "Spotify":
client = self._get_spotify_client(config)
if not client:
logger.error("Spotify client not available")
return []
tracks = self._collect_spotify_tracks(client, url)
if tracks:
queries = [f"{n} {a}" for n, a in tracks]
with concurrent.futures.ThreadPoolExecutor() as pool:
resolved = list(pool.map(self.search_youtube, queries))
urls.extend(u for u in resolved if u)
logger.info(f"Got {len(urls)} video(s) from {platform} playlist")
return urls
except Exception as e:
logger.error(f"Error getting playlist from {platform}: {e}")
return []
@staticmethod
def _collect_spotify_tracks(
client: spotipy.Spotify, url: str
) -> list[tuple[str, str]]:
"""Return ``[(track_name, artist_name), ...]`` from a Spotify URL."""
tracks: list[tuple[str, str]] = []
if "/playlist/" in url:
pid = url.split("/playlist/")[1].split("?")[0].split("/")[0]
offset = 0
while True:
page = client.playlist_items(pid, offset=offset)
if not page or not page.get("items"):
break
for item in page["items"]:
t = item.get("track")
if t and t.get("name") and t.get("artists"):
tracks.append((t["name"], t["artists"][0]["name"]))
offset += len(page["items"])
if offset >= page.get("total", 0):
break
elif "/album/" in url:
aid = url.split("/album/")[1].split("?")[0].split("/")[0]
album = client.album(aid)
if not album:
return tracks
fallback = ([a["name"] for a in album.get("artists", [])] or [""])[0]
offset = 0
while True:
page = client.album_tracks(aid, offset=offset, limit=50)
if not page or not page.get("items"):
break
for t in page["items"]:
if t and t.get("name"):
a = t["artists"][0]["name"] if t.get("artists") else fallback
tracks.append((t["name"], a))
if not page.get("next"):
break
offset += len(page["items"])
return tracks
# ------------------------------------------------------------------
# yt-dlp progress hooks (for SSE real-time updates)
# ------------------------------------------------------------------
def _make_progress_hook(self, request_id: str) -> Callable:
"""Create a yt-dlp progress_hook that writes to DownloadProgressStore."""
def hook(d: dict) -> None:
# If cancellation has been requested (client disconnected), raise to abort yt-dlp
try:
if DownloadProgressStore.is_cancelled(request_id):
raise yt_dlp.utils.DownloadError("Cancelled by client disconnect")
except Exception:
# In case yt_dlp references aren't available yet or other errors,
# just raise a generic exception to abort.
raise
status = d.get("status", "")
if status == "downloading":
downloaded = d.get("downloaded_bytes", 0) or 0
total = d.get("total_bytes") or d.get("total_bytes_estimate") or 0
item_pct = (downloaded / total * 100) if total > 0 else 0
store = DownloadProgressStore.get(request_id)
completed = store.get("completed_items", 0)
total_items = max(store.get("total_items", 1), 1)
# Map to 15-90% range
overall = 15 + ((completed + item_pct / 100) / total_items) * 75
speed = d.get("speed")
speed_str = self._format_speed(speed) if speed else ""
eta = d.get("eta")
eta_str = self._format_eta(eta) if eta else ""
filename = Path(d.get("filename", "")).stem
DownloadProgressStore.update(
request_id,
percent=min(int(overall), 90),
status="Downloading...",
detail=filename[:60] if filename else "",
speed=speed_str,
eta=eta_str,
current_file=filename,
phase="downloading",
)
elif status == "finished":
filename = Path(d.get("filename", "")).stem
DownloadProgressStore.update(
request_id,
status="Converting...",
detail=f"Processing {filename[:50]}",
phase="converting",
speed="",
eta="",
)
return hook
def _make_postprocessor_hook(self, request_id: str) -> Callable:
"""Create a yt-dlp postprocessor_hook for SSE status updates."""
def hook(d: dict) -> None:
status = d.get("status", "")
pp = d.get("postprocessor", "")
if status == "started" and pp:
label = (
pp.replace("FFmpeg", "")
.replace("Extract", "Extracting ")
.replace("Embed", "Embedding ")
.replace("Metadata", "metadata")
.strip()
or pp
)
DownloadProgressStore.update(
request_id,
status="Processing...",
detail=label,
phase="converting",
)
return hook
@staticmethod
def _format_speed(speed_bps: float | None) -> str:
"""Format bytes/sec into a human-readable string."""
if not speed_bps:
return ""
if speed_bps >= 1_048_576:
return f"{speed_bps / 1_048_576:.1f} MB/s"
if speed_bps >= 1024:
return f"{speed_bps / 1024:.0f} KB/s"
return f"{int(speed_bps)} B/s"
@staticmethod
def _format_eta(seconds: float | int | None) -> str:
"""Format ETA seconds into a human-readable string."""
if not seconds or seconds < 0:
return ""
seconds = int(seconds)
if seconds >= 3600:
return f"{seconds // 3600}h {(seconds % 3600) // 60}m"
if seconds >= 60:
return f"{seconds // 60}m {seconds % 60}s"
return f"{seconds}s"
# ------------------------------------------------------------------
# Unified media download (DRY merge of audio + video)
# ------------------------------------------------------------------
def _download_media(
self,
url: str,
format_mode: str, # "audio" | "video"
config: dict,
result: _DownloadResult,
session_dir: Path,
cookie_file: Path | None = None,
) -> None:
"""Download a single item as audio or video into *session_dir*.
Handles Spotify conversion, ffmpeg check, quality selection,
SponsorBlock, metadata embedding, and sidecar cleanup.
All files are written strictly inside ``session_dir``.
"""
is_audio = format_mode == "audio"
try:
# --- Spotify URL conversion ---
if "spotify.com" in url and "/track/" in url:
logger.info("Detected Spotify URL, converting to YouTube")
url = self._resolve_spotify_track(config, url)
if not url:
result.inc_error(format_mode)
return
# --- ffmpeg check (warn if missing, but don't abort) ---
if not self._ffmpeg_available:
logger.warning(
"ffmpeg is not installed or not in PATH; some post-processing may fail."
)
# --- Pre-flight metadata extraction ---
try:
with yt_dlp.YoutubeDL(self._base_ytdlp_opts(cookie_file)) as ydl:
info = ydl.extract_info(url, download=False)
except Exception:
if not cookie_file:
raise
# Retry once without forced extractor_args for cookie-auth flows.
fb_opts = self._base_ytdlp_opts(cookie_file)
fb_opts.pop("extractor_args", None)
with yt_dlp.YoutubeDL(fb_opts) as ydl:
info = ydl.extract_info(url, download=False)
def _has_playable_formats(data: dict) -> bool:
fmts = data.get("formats", []) or []
return any(
(f.get("vcodec") and f.get("vcodec") != "none")
or (f.get("acodec") and f.get("acodec") != "none")
for f in fmts
)
if not _has_playable_formats(info):
# Try one fallback without forced player_client in case cookies are valid
# but the selected client surface returned only storyboards.
if cookie_file:
fb_opts = self._base_ytdlp_opts(cookie_file)
fb_opts.pop("extractor_args", None)
with yt_dlp.YoutubeDL(fb_opts) as ydl:
info = ydl.extract_info(url, download=False)
if not _has_playable_formats(info):
logger.error(
"No playable formats were returned (video likely needs valid logged-in cookies)."
)
result.inc_error(format_mode)
if result.request_id:
DownloadProgressStore.update(
result.request_id,
status="Error",
detail="No playable formats. Check your cookies.",
phase="error",
)
return
title = info.get("title", "Unknown Title")
uploader = info.get("uploader", "Unknown Uploader")
download_url = url
# --- YouTube Music preference (audio only) ---
if is_audio and config.get("Preferir_YouTube_Music", False):
logger.info(f"Searching for pure audio on YouTube Music: '{title}'")
ytm_url, _, _ = self.search_youtube_music(title, uploader)
if ytm_url:
download_url = ytm_url
# --- Clean title for filename ---
clean_title = self._sanitize_filename(
title if is_audio else f"{title} - {uploader}"
)
# --- Output directory — always session_dir ---
out_dir = self._ensure_dir(session_dir)
# --- Format / quality ---
quality_key = config.get("Calidad_audio_video", "avg")
if is_audio:
_AUDIO_QUALITY: dict[str, tuple[str, str]] = {
"min": ("worstaudio[abr<=96]/worstaudio/worst", "64"),
"avg": (
"bestaudio[abr<=160]/bestaudio[abr<=192]/bestaudio/best",
"128",
),
"max": ("bestaudio/best", "320"),
}
fmt_str, quality_val = _AUDIO_QUALITY.get(
quality_key, _AUDIO_QUALITY["avg"]
)
file_format = config.get("Formato_audio", "mp3")
else:
# Prefer audio formats compatible with MP4 (e.g. m4a/AAC)
# when the requested container is mp4; otherwise allow
# broader selection (webm/opus etc.). This avoids producing
# MP4 files with Opus audio which some players (Windows)
# cannot play.
file_format = config.get("Formato_video", "mp4")
if file_format == "mp4":
_VIDEO_QUALITY: dict[str, str] = {
"min": "worstvideo[ext=mp4]+worstaudio[ext=m4a]/worst[ext=mp4]/worst",
"avg": "bestvideo[height<=1080]+bestaudio[ext=m4a]/bestaudio[height<=1080]/best[height<=1080]",
"max": "bestvideo+bestaudio[ext=m4a]/bestaudio/best",
}
else:
_VIDEO_QUALITY = {
"min": "worstvideo[ext=mp4]+worstaudio[ext=m4a]/worst[ext=mp4]/worst",
"avg": "bestvideo[height<=1080]+bestaudio/best[height<=1080]",
"max": "bestvideo+bestaudio/best",
}
fmt_str = _VIDEO_QUALITY.get(quality_key, _VIDEO_QUALITY["avg"])
quality_val = "" # unused for video
logger.info(f"Downloading {format_mode}: '{title}'")
# --- Output template ---
# Use yt-dlp template macros so its internal postprocessors
# manage final filenames and thumbnail embedding.
outtmpl = str(out_dir / "%(title)s - %(uploader)s.%(ext)s")
# --- Postprocessors ---
postprocessors = list(self._sponsorblock_postprocessors(config))
if is_audio:
postprocessors.append(
{
"key": "FFmpegExtractAudio",
"preferredcodec": file_format,
"preferredquality": quality_val,
}
)
# Common: metadata + thumbnail conversion. EmbedThumbnail is only
# added when the final container/codec supports embedded cover art
# (yt-dlp/ffmpeg will error otherwise — e.g. WAV does not support it).
common_pp = [
{
"key": "FFmpegMetadata",
"add_chapters": True,
"add_metadata": True,
},
{"key": "FFmpegThumbnailsConvertor", "format": "jpg"},
]
# Supported targets for thumbnail embedding (yt-dlp/ffmpeg)
fmt = (file_format or "").lower()
if is_audio:
embed_supported = fmt in ("mp3", "ogg", "opus", "flac", "m4a")
else:
embed_supported = fmt in ("mp4", "m4v", "mov", "mkv", "mka")
if embed_supported:
common_pp.append({"key": "EmbedThumbnail"})
postprocessors.extend(common_pp)
# --- yt-dlp options (with cookie support) ---
ydl_opts = self._base_ytdlp_opts(cookie_file)
ydl_opts.update(
{
"format": fmt_str,
"postprocessors": postprocessors,
"outtmpl": outtmpl,
"trim_file_name_length": 184,
"updatetime": False,
"writethumbnail": True,
"parse_metadata": [
"%(artist,uploader)s:%(artist)s",
"%(album,title)s:%(album)s",
"%(title)s:%(title)s",
],
}
)
# Improve compatibility for MP3 cover-art tags on Windows players.
# These ffmpeg options are applied to yt-dlp postprocessing steps
# and help ensure ID3v2 artwork is recognized reliably.
if is_audio and file_format.lower() == "mp3":
ydl_opts["postprocessor_args"] = [
"-id3v2_version",
"3",
"-write_id3v1",
"1",
]
if not is_audio:
video_opts: dict = {
"format_sort": ["vext:mp4", "aext:m4a", "aext:mp3"],
"merge_output_format": file_format,
"concurrent_fragment_downloads": 3,
"retries": 3,
}
ydl_opts.update(video_opts)
# --- yt-dlp progress hooks (SSE real-time updates) ---
if result.request_id:
ydl_opts["progress_hooks"] = [
self._make_progress_hook(result.request_id)
]
ydl_opts["postprocessor_hooks"] = [
self._make_postprocessor_hook(result.request_id)
]
# --- Download ---
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
dl_info = ydl.extract_info(download_url, download=True)
except Exception as e:
msg = str(e)
if (
"SponsorBlock" in msg
or "SponsorBlockPP" in msg
or "unexpected keyword argument 'action'" in msg
):
logger.warning(
f"SponsorBlock postprocessor failed: {e}. Retrying without SponsorBlock."
)
if result.request_id:
DownloadProgressStore.update(
result.request_id,
detail="SponsorBlock falló; continuando sin SponsorBlock.",
)
# Remove SponsorBlock postprocessors and retry once
filtered_pp = [
p
for p in postprocessors
if not (isinstance(p, dict) and p.get("key") == "SponsorBlock")
]
ydl_opts["postprocessors"] = filtered_pp
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
dl_info = ydl.extract_info(download_url, download=True)
except Exception as e2:
logger.error(
f"Download failed after disabling SponsorBlock: {e2}"
)
raise
else:
raise
# --- Locate output file (robust detection) ---
final_path: Path | None = None
# 1) Prefer explicit paths reported by yt-dlp (requested_downloads)
if dl_info:
requested = dl_info.get("requested_downloads")
if requested and isinstance(requested, list):
for r in requested:
fp = r.get("filepath") or r.get("filename")
if not fp:
continue
candidate = Path(fp)
if candidate.exists():
# If a postprocessor converted audio, check for the
# converted extension next to the originally
# downloaded file (common with FFmpegExtractAudio).
if is_audio and file_format:
alt = candidate.with_suffix(f".{file_format}")
if alt.exists():
final_path = alt
break
final_path = candidate
break
# 2) Fallback: scan output directory for matching basename
if final_path is None:
candidates = list(out_dir.glob(f"{clean_title}.*"))
# Exclude known sidecar extensions
candidates = [
p
for p in candidates
if p.suffix.lower() not in self._SIDECAR_EXTENSIONS
]
if is_audio and file_format:
# Prefer exact extension match
for p in candidates:
if p.suffix.lower() == f".{file_format}".lower():
final_path = p
break
if final_path is None and candidates:
# Pick the largest candidate (likely the media file)
candidates.sort(
key=lambda p: p.stat().st_size if p.exists() else 0,
reverse=True,
)
final_path = candidates[0]
# 3) Last-resort: reconstruct expected path
if final_path is None:
if is_audio:
final_path = out_dir / f"{clean_title}.{file_format}"
else:
final_path = self._resolve_video_output(
dl_info, out_dir, clean_title, file_format
)
if final_path and final_path.exists():
# Rely on yt-dlp's postprocessors (EmbedThumbnail) to have
# embedded the cover art. Clean sidecars and record result.
self._cleanup_sidecars(final_path)
result.inc_success(format_mode)
result.add_file(str(final_path))
logger.info(f"{format_mode.capitalize()} downloaded: '{title}'")
else:
logger.error(f"Output file not found after download: {final_path}")
result.inc_error(format_mode)
except Exception as e:
# If the error looks proxy-related and we have multiple proxies,
# signal the caller to rotate and retry.
if _proxy_rotator.count > 1 and _proxy_rotator.is_proxy_error(e):
raise _ProxyBlockedError(str(e)) from e
logger.error(f"Error downloading {format_mode}: {e}")
result.inc_error(format_mode)
@staticmethod
def _resolve_video_output(
dl_info: dict | None,
out_dir: Path,
clean_title: str,
file_format: str,
) -> Path:
"""Determine the actual video file path produced by yt-dlp."""
if dl_info:
requested = dl_info.get("requested_downloads")
if requested and isinstance(requested, list) and requested:
fp = requested[0].get("filepath") or requested[0].get("filename")
if fp:
return Path(fp)
return out_dir / f"{clean_title}.{file_format}"
def _cleanup_sidecars(self, filepath: Path) -> None:
"""Remove leftover thumbnail / subtitle sidecar files.
Handles both plain sidecars (``video.jpg``) and language-suffixed
subtitle sidecars produced by yt-dlp (``video.en.srt``,
``video.es-orig.vtt``, etc.).
"""
stem = filepath.stem
parent = filepath.parent
for ext in self._SIDECAR_EXTENSIONS:
# Plain sidecar: video.srt
sidecar = filepath.with_suffix(ext)
if sidecar.exists():
try:
sidecar.unlink()
except OSError:
pass
# Language-suffixed sidecars: video.en.srt, video.es-orig.vtt
for lang_file in parent.glob(f"{stem}.*{ext}"):
if lang_file != filepath:
try:
lang_file.unlink()
except OSError:
pass
# ------------------------------------------------------------------
# Compression
# ------------------------------------------------------------------
def compress_files(
self,
zip_name: str,
files: list[str],
output_folder: Path | str | None = None,
) -> str | None:
"""Compress *files* into a ZIP, delete originals, return ZIP path."""
try:
out = self._ensure_dir(
output_folder or self._base_dir / "Downloads" / "Zip"
)
clean_name = self._sanitize_filename(zip_name)
if clean_name.lower().endswith(".zip"):
clean_name = clean_name[:-4]
zip_path = out / f"{clean_name}.zip"
with zipfile.ZipFile(zip_path, "w") as zf:
for f in files:
zf.write(f, self._sanitize_filename(Path(f).name))
for f in files:
Path(f).unlink(missing_ok=True)
logger.info(f"Files compressed to '{zip_path.name}'")
return str(zip_path.resolve())
except Exception as e:
logger.error(f"Error compressing files: {e}")
return None
# ------------------------------------------------------------------
# Parallel task execution
# ------------------------------------------------------------------
def _run_download_tasks(
self,
tasks: list[tuple[str, str, dict]],
result: _DownloadResult,
session_dir: Path,
cookie_file: Path | None,
max_workers: int,
) -> None:
"""Fan out ``(url, mode, config)`` tasks across a thread pool.
Progress is reported via ``result.progress_callback`` in a
thread-safe manner.
"""
if not tasks:
return
result.total_items = max(len(tasks), 1)
if result.request_id:
DownloadProgressStore.update(
result.request_id, total_items=result.total_items
)
callback = result.progress_callback
def _worker(task: tuple[str, str, dict]) -> None:
url, mode, cfg = task
if callback:
callback(
result.get_progress_pct(),
f"Downloading {mode}...",
f"Processing...",
)
# Check cancellation before starting this task
if result.request_id and DownloadProgressStore.is_cancelled(
result.request_id
):
logger.info(f"Skipping task due to cancellation: {url}")
return
# Attempt download with automatic proxy rotation on failure.
max_proxy_attempts = max(_proxy_rotator.count, 1)
for _proxy_attempt in range(max_proxy_attempts):
try:
self._download_media(
url, mode, cfg, result, session_dir, cookie_file
)
break # success (or handled error inside _download_media)
except _ProxyBlockedError as proxy_err:
if _proxy_attempt < max_proxy_attempts - 1:
new_proxy = _proxy_rotator.rotate()
logger.warning(
f"Proxy blocked for '{url}', rotating to "
f"{new_proxy} (attempt "
f"{_proxy_attempt + 2}/{max_proxy_attempts})"
)
if result.request_id:
DownloadProgressStore.update(
result.request_id,
detail="Proxy blocked, switching to next proxy…",
)
continue
# All proxies exhausted
logger.error(f"All proxies exhausted for '{url}': {proxy_err}")
result.inc_error(mode)
result.inc_completed()
if result.request_id:
DownloadProgressStore.update(
result.request_id,
completed_items=result.completed_items,
)
effective_workers = min(max_workers, len(tasks))
with concurrent.futures.ThreadPoolExecutor(
max_workers=effective_workers
) as executor:
futures = [executor.submit(_worker, t) for t in tasks]
# Wait and propagate worker-level exceptions as log entries
for fut in concurrent.futures.as_completed(futures):
# If cancellation was requested globally, attempt to cancel remaining futures
if result.request_id and DownloadProgressStore.is_cancelled(
result.request_id
):
logger.info("Cancellation requested - cancelling remaining tasks")
for f in futures:
if not f.done():
try:
f.cancel()
except Exception:
pass
# Update progress store to reflect cancellation
if result.request_id:
DownloadProgressStore.update(
result.request_id,
status="Cancelled",
detail="Cancelled by client disconnect",
complete=True,
error="Cancelled by client disconnect",
percent=100,
phase="cancelled",
)
break
try:
fut.result()
except Exception as exc:
logger.error(f"Download worker exception: {exc}")
# ------------------------------------------------------------------
# Entry points (UUID isolation + cookie + parallel)
# ------------------------------------------------------------------
def download_with_progress(
self,
config: dict,
data: str,
filename: str = "archivos.zip",
progress_callback: Callable | None = None,
base_folder: str | None = None,
request_id: str | None = None,
) -> str | None:
"""Auto-detect URL type and download with progress reporting.
When *base_folder* is supplied (e.g. by ``routes.py``), it is used
directly and the **caller** is responsible for cleanup. Otherwise a
UUID-scoped temporary directory is created and cleaned automatically
via ``try … finally``.
"""
# --- Session directory & ownership ---
if base_folder:
session_dir = Path(base_folder)
owns_session = False
else:
session_id = uuid.uuid4().hex
session_dir = self._base_dir / "Downloads" / "Temp" / session_id
owns_session = True
try:
self._ensure_dir(session_dir)
# --- Per-session cookie provisioning ---
cookie_file = self._setup_cookies(config, session_dir)
result = _DownloadResult(progress_callback, request_id=request_id)
urls: list[str] = []
if progress_callback:
progress_callback(5, "Preparing...", "Analyzing request")
# --- URL resolution (unchanged logic) ---
data_lower = (data or "").lower()
is_yt = any(
p in data_lower
for p in ("youtube.com", "youtu.be", "music.youtube.com")
)
is_sp = "spotify.com" in data_lower
if is_sp:
if self.is_playlist_url(data):
if progress_callback:
progress_callback(
10, "Getting playlist...", "Connecting to Spotify"
)
urls.extend(self._resolve_playlist_urls(config, "Spotify", data))
else:
if progress_callback:
progress_callback(
10, "Processing link...", "Spotify URL detected"
)
yt_url = self._resolve_spotify_track(config, data)
if yt_url:
urls.append(yt_url)
elif is_yt:
if self.is_playlist_url(data):
if progress_callback:
progress_callback(
10,
"Getting playlist...",
"Connecting to YouTube",
)
urls.extend(self._resolve_playlist_urls(config, "YouTube", data))
else:
if progress_callback:
progress_callback(
10, "Processing link...", "YouTube URL detected"
)
urls.append(data)
elif data:
if progress_callback:
progress_callback(
10, "Searching YouTube...", f"Searching: {data[:50]}"
)
if config.get("Preferir_YouTube_Music"):
ytm_url, _, _ = self.search_youtube_music(data, data)
if ytm_url:
urls.append(ytm_url)
else:
link = self.search_youtube(data)
if link:
urls.append(link)
else:
link = self.search_youtube(data)
if link:
urls.append(link)
if not urls:
if progress_callback:
progress_callback(100, "No results", "No URLs to download found")
return None
if progress_callback:
progress_callback(
15, "Starting downloads...", f"{len(urls)} item(s) found"
)
# --- Build task list & run in parallel ---
tasks: list[tuple[str, str, dict]] = []
for url in urls:
if not url:
continue
if config.get("Descargar_video"):
tasks.append((url, "video", config))
if config.get("Descargar_audio"):
tasks.append((url, "audio", config))
max_workers = config.get("max_download_workers", self._DEFAULT_MAX_WORKERS)
start = time.time()
self._run_download_tasks(
tasks, result, session_dir, cookie_file, max_workers
)
logger.info(f"Execution time: {time.time() - start:.2f} seconds")
# If cancellation was requested during task execution, abort early
if result.request_id and DownloadProgressStore.is_cancelled(
result.request_id
):
logger.info("Download cancelled by client - aborting before finalize")
if progress_callback:
progress_callback(100, "Cancelled", "Cancelled by client")
return None
if progress_callback:
progress_callback(90, "Finishing...", "Processing downloaded files")
return self._finalize(
result, filename, session_dir, progress_callback, owns_session
)
except Exception as e:
logger.error(f"Error in download process: {e}")
if progress_callback:
progress_callback(100, "Error", str(e)[:100])
return None
finally:
if owns_session:
shutil.rmtree(session_dir, ignore_errors=True)
logger.debug(f"Session directory cleaned: {session_dir.name}")
def download_selective(
self,
config: dict,
selected_urls: list[str],
filename: str = "archivos.zip",
progress_callback: Callable | None = None,
base_folder: str | None = None,
item_configs: dict | None = None,
request_id: str | None = None,
) -> str | None:
"""Download selected URLs with optional per-item format overrides.
Same UUID-isolation and cookie semantics as
``download_with_progress``.
"""
item_configs = item_configs or {}
# --- Session directory & ownership ---
if base_folder:
session_dir = Path(base_folder)
owns_session = False
else:
session_id = uuid.uuid4().hex
session_dir = self._base_dir / "Downloads" / "Temp" / session_id
owns_session = True
try:
self._ensure_dir(session_dir)
# --- Per-session cookie provisioning ---
cookie_file = self._setup_cookies(config, session_dir)
result = _DownloadResult(progress_callback, request_id=request_id)
if progress_callback:
progress_callback(5, "Preparing...", "Analyzing request")
if not selected_urls:
if progress_callback:
progress_callback(100, "No selection", "No URLs selected")
return None
if progress_callback:
progress_callback(
15,
"Starting downloads...",
f"{len(selected_urls)} item(s) selected",
)
# --- Build task list with per-item overrides ---
tasks: list[tuple[str, str, dict]] = []
for url in selected_urls:
if not url:
continue
ic = item_configs.get(url, {})
item_fmt = ic.get("format")
# Build effective config with per-item format override
effective_cfg = config.copy()
file_fmt = ic.get("fileFormat")
if file_fmt:
_AUDIO_EXTS = ("mp3", "m4a", "flac", "wav")
_VIDEO_EXTS = ("mp4", "mkv", "webm", "mov")
if file_fmt in _AUDIO_EXTS:
effective_cfg["Formato_audio"] = file_fmt
elif file_fmt in _VIDEO_EXTS:
effective_cfg["Formato_video"] = file_fmt
# Determine modes to download
modes: list[str] = []
if item_fmt:
modes.append(item_fmt)
else:
if config.get("Descargar_video"):
modes.append("video")
if config.get("Descargar_audio"):
modes.append("audio")
for mode in modes:
tasks.append((url, mode, effective_cfg))
max_workers = config.get("max_download_workers", self._DEFAULT_MAX_WORKERS)
start = time.time()
self._run_download_tasks(
tasks, result, session_dir, cookie_file, max_workers
)
logger.info(f"Execution time: {time.time() - start:.2f} seconds")
# If cancellation was requested during task execution, abort early
if result.request_id and DownloadProgressStore.is_cancelled(
result.request_id
):
logger.info("Download cancelled by client - aborting before finalize")
if progress_callback:
progress_callback(100, "Cancelled", "Cancelled by client")
return None
if progress_callback:
progress_callback(90, "Finishing...", "Processing downloaded files")
return self._finalize(
result, filename, session_dir, progress_callback, owns_session
)
except Exception as e:
logger.error(f"Error in selective download: {e}")
if progress_callback:
progress_callback(100, "Error", str(e)[:100])
return None
finally:
if owns_session:
shutil.rmtree(session_dir, ignore_errors=True)
logger.debug(f"Session directory cleaned: {session_dir.name}")
# ------------------------------------------------------------------
# Shared helpers for entry points
# ------------------------------------------------------------------
def _finalize(
self,
result: _DownloadResult,
filename: str,
session_dir: Path,
progress_callback: Callable | None,
owns_session: bool = False,
) -> str | None:
"""Compress (if many files), log summary, return final path.
When *owns_session* is ``True``, the final deliverable is copied to a
permanent output directory **before** the session directory is removed
by the caller's ``finally`` block.
"""
files = result.canciones_descargadas
if len(files) > 1:
if progress_callback:
progress_callback(92, "Compressing...", "Creating ZIP file")
path = self.compress_files(filename, files, str(session_dir))
elif len(files) == 1:
path = str(Path(files[0]).resolve())
else:
path = None
# When we own the session dir, move the deliverable out before cleanup
if owns_session and path and Path(path).exists():
output_dir = self._ensure_dir(self._base_dir / "Downloads" / "Output")
dest = output_dir / Path(path).name
# Avoid name collision
if dest.exists():
stem = dest.stem
suffix = dest.suffix
dest = output_dir / f"{stem}_{uuid.uuid4().hex[:8]}{suffix}"
shutil.copy2(str(path), str(dest))
path = str(dest)
if progress_callback:
progress_callback(98, "Done!", "Preparing download")
self._log_summary(result)
logger.info(f"File path: {path}")
return path
@staticmethod
def _log_summary(result: _DownloadResult) -> None:
a, v = result.audios_exito, result.videos_exito
if a and v:
logger.info(f"Downloaded {a} audios and {v} videos")
elif a:
logger.info(f"Downloaded {a} audios")
elif v:
logger.info(f"Downloaded {v} videos")
@staticmethod
def _parse_duration_str(text: str) -> int:
"""Parse ``"M:SS"`` or ``"H:MM:SS"`` into total seconds."""
if not text:
return 0
parts = text.split(":")
try:
if len(parts) == 2:
return int(parts[0]) * 60 + int(parts[1])
if len(parts) == 3:
return int(parts[0]) * 3600 + int(parts[1]) * 60 + int(parts[2])
except ValueError:
pass
return 0
# ============================================
# RQ Task — standalone picklable function for the worker process
# ============================================
def execute_download_task(
user_config: dict,
input_url: str,
task_id: str,
nombre_archivo: str,
temp_dir: str,
is_playlist_mode: bool = False,
selected_urls: list | None = None,
item_configs: dict | None = None,
redis_url: str | None = None,
) -> None:
"""Top-level task executed by an RQ worker.
This function is intentionally defined at module level so that it is
*picklable* by RQ. It re-initialises the Redis connection inside the
worker process (workers do NOT share memory with the Flask app) and
delegates to the :class:`OfflinerCore` singleton for the actual download.
Args:
user_config: Validated user configuration dict.
input_url: The URL or search query submitted by the user.
task_id: Unique request_id (used as the Redis progress key).
nombre_archivo: Desired output filename (ZIP name).
temp_dir: Absolute path to the session-scoped temp directory.
is_playlist_mode: Whether the request is a playlist selection.
selected_urls: List of selected URLs (playlist mode only).
item_configs: Per-item format overrides (playlist mode only).
redis_url: Redis connection URL so the worker can write progress.
"""
# Ensure the worker process has a valid Redis connection.
if redis_url:
init_redis(redis_url)
try:
def progress_callback(percent: int, status: str, detail: str = "") -> None:
DownloadProgressStore.update(
task_id,
percent=percent,
status=status,
detail=detail,
)
if is_playlist_mode and selected_urls:
result_path = _core.download_selective(
user_config,
selected_urls,
nombre_archivo,
progress_callback,
base_folder=temp_dir,
item_configs=item_configs,
request_id=task_id,
)
else:
result_path = _core.download_with_progress(
user_config,
input_url,
nombre_archivo,
progress_callback,
base_folder=temp_dir,
request_id=task_id,
)
if result_path and os.path.exists(result_path):
logger.info(f"RQ task {task_id}: download completed → {result_path}")
DownloadProgressStore.update(
task_id,
file_path=result_path,
complete=True,
percent=100,
status="Done!",
detail="Ready to download",
phase="done",
speed="",
eta="",
)
else:
if DownloadProgressStore.is_cancelled(task_id):
DownloadProgressStore.update(
task_id,
error="Cancelled by client disconnect",
complete=True,
percent=100,
status="Cancelled",
detail="Cancelled by client disconnect",
phase="cancelled",
)
else:
DownloadProgressStore.update(
task_id,
error="Could not download the file.",
complete=True,
percent=100,
status="Error",
phase="error",
)
except Exception as exc:
logger.error(f"RQ task {task_id} failed: {exc}")
DownloadProgressStore.update(
task_id,
error=str(exc)[:200],
complete=True,
percent=100,
status="Error",
phase="error",
)
# ============================================
# Module-level singleton + backward-compatible API
# ============================================
#
# routes.py imports individual names from this module. The wrappers below
# delegate to the singleton so that existing call-sites keep working without
# any changes.
_core = OfflinerCore()
# -- Re-exported constants / objects --
SPONSORBLOCK_CATEGORIES = OfflinerCore.SPONSORBLOCK_CATEGORIES
ytmusic = _core.ytmusic
sp = _core._spotify_default # noqa: SLF001 — kept for backward compat
# -- Thin function wrappers (preserve original signatures) --
def obtener_info_playlist(url, config=None, max_items=None):
"""Backward-compatible wrapper for ``OfflinerCore.get_playlist_info``."""
return _core.get_playlist_info(url, config, max_items=max_items)
def es_url_playlist(url):
"""Backward-compatible wrapper for ``OfflinerCore.is_playlist_url``."""
return OfflinerCore.is_playlist_url(url)
def obtener_info_media(url, config=None):
"""Backward-compatible wrapper for ``OfflinerCore.get_media_info``."""
return _core.get_media_info(url, config)
def detectar_fuente_url(url):
"""Backward-compatible wrapper for ``OfflinerCore.detect_url_source``."""
return OfflinerCore.detect_url_source(url)
def extraer_video_id_youtube(url):
"""Backward-compatible wrapper for ``OfflinerCore.extract_youtube_video_id``."""
return OfflinerCore.extract_youtube_video_id(url)
def obtener_segmentos_sponsorblock(video_id, categories=None):
"""Backward-compatible wrapper for ``OfflinerCore.get_sponsorblock_segments``."""
return _core.get_sponsorblock_segments(video_id, categories)
def iniciar_con_progreso(
config,
dato,
nombre_archivo_final="archivos.zip",
progress_callback=None,
base_folder=None,
request_id=None,
):
"""Backward-compatible wrapper for ``OfflinerCore.download_with_progress``."""
return _core.download_with_progress(
config,
dato,
nombre_archivo_final,
progress_callback,
base_folder,
request_id=request_id,
)
def iniciar_descarga_selectiva(
config,
urls_seleccionadas,
nombre_archivo_final="archivos.zip",
progress_callback=None,
base_folder=None,
item_configs=None,
request_id=None,
):
"""Backward-compatible wrapper for ``OfflinerCore.download_selective``."""
return _core.download_selective(
config,
urls_seleccionadas,
nombre_archivo_final,
progress_callback,
base_folder,
item_configs,
request_id=request_id,
)
def buscar_cancion_youtube(query):
"""Backward-compatible wrapper for ``OfflinerCore.search_youtube``."""
return _core.search_youtube(query)
def buscar_en_youtube_music(titulo_video, artista=None):
"""Backward-compatible wrapper for ``OfflinerCore.search_youtube_music``."""
return _core.search_youtube_music(titulo_video, artista)
def obtener_cancion_Spotify(config, link_spotify):
"""Backward-compatible wrapper for ``OfflinerCore._resolve_spotify_track``."""
return _core._resolve_spotify_track(config, link_spotify) # noqa: SLF001
def obtener_playlist(config, plataforma, playlist_url):
"""Backward-compatible wrapper for ``OfflinerCore._resolve_playlist_urls``."""
return _core._resolve_playlist_urls(
config, plataforma, playlist_url
) # noqa: SLF001
|