Spaces:
Configuration error
Configuration error
File size: 86,251 Bytes
a92232f | 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 | import os
import sys
# Enforce zero shard/chunk disk cache on HuggingFace Xet to prevent 10GB+ cache accumulation
os.environ.setdefault("HF_XET_SHARD_CACHE_SIZE_LIMIT", "0")
os.environ.setdefault("HF_XET_CHUNK_CACHE_SIZE_BYTES", "0")
os.environ.setdefault("HF_XET_RECONSTRUCTION_DOWNLOAD_BUFFER_LIMIT", "33554432")
os.environ.setdefault("HF_XET_LOG_DIR_MAX_SIZE", "10000000")
os.environ.setdefault("HF_HUB_DISABLE_DISK_INTEGRITY_CHECK", "1")
import logging
import urllib.parse
import httpx
import time
import collections
import threading
import re
import zipfile
import shutil
import json
from typing import Optional, Any
from fastapi import FastAPI, HTTPException, Query, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse, HTMLResponse
# Store last 100 log lines in a thread-safe list/deque
RECENT_LOGS = collections.deque(maxlen=100)
FATAL_ERRORS = collections.deque(maxlen=10)
class InMemoryLogHandler(logging.Handler):
def emit(self, record):
try:
log_entry = self.format(record)
RECENT_LOGS.append(log_entry)
except Exception:
self.handleError(record)
# Setup logging
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper()
# Create handlers
stdout_handler = logging.StreamHandler(sys.stdout)
file_handler = logging.FileHandler("stream_service.log", encoding="utf-8")
log_handler = InMemoryLogHandler()
# Set formatters
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(name)s: %(message)s")
stdout_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
log_handler.setFormatter(formatter)
# Configure root logger
root_logger = logging.getLogger()
root_logger.setLevel(LOG_LEVEL)
root_logger.addHandler(stdout_handler)
root_logger.addHandler(file_handler)
root_logger.addHandler(log_handler)
log = logging.getLogger("stream-service")
# Config
DB_URL = os.environ.get("DB_URL", "")
HF_TOKEN = os.environ.get("HFTOKEN")
BUCKET_ID = "shashwatIDR/stream"
import ipaddress
def is_safe_domain_url(url: str) -> bool:
"""Strict security check: ensures 0 direct IP access. Returns False if target is an IPv4 or IPv6."""
try:
hostname = urllib.parse.urlparse(url).hostname or ""
if not hostname:
return False
ipaddress.ip_address(hostname)
return False # Target is a raw IP address! Block it.
except ValueError:
return True # Target is a valid domain name.
# Zero Direct IP Access: Invidious queries & streams are routed strictly through Antideploy proxy layer
INVIDIOUS_PROXY_URL = os.environ.get("INVIDIOUS_PROXY_URL", "https://noipaccess.antideploy.app").rstrip("/")
raw_instances = [inst.strip() for inst in os.environ.get("INVIDIOUS_INSTANCES", "").split(",") if inst.strip()]
if not raw_instances:
raw_instances = [INVIDIOUS_PROXY_URL]
# Filter out any raw IP addresses with zero tolerance
INVIDIOUS_INSTANCES = [inst for inst in raw_instances if is_safe_domain_url(inst)]
if not INVIDIOUS_INSTANCES:
INVIDIOUS_INSTANCES = [INVIDIOUS_PROXY_URL]
# Track stats in session
STATS = {
"total": 0,
"done": 0,
"progressing": 0,
"failed": 0
}
STREAM_JOBS = {} # video_id -> {"status": "fetching"|"progressing"|"done"|"failed", "error": str, "timestamp": float}
UPLOAD_TIMESTAMPS = collections.deque()
NETWORK_STATS = {
"last_download_speed_kbps": 0.0,
"last_upload_speed_kbps": 0.0,
"total_downloaded_mb": 0.0,
"total_uploaded_mb": 0.0
}
# Base URL cache to avoid redundant database reads
BASE_URL_CACHE = {}
BASE_URL_CACHE_LOCK = threading.Lock()
# In-memory database cache
DB_CACHE = {}
DB_CACHE_LOCK = threading.Lock()
def load_db_cache():
"""Load all records from SQL database or fallback HF db_backup.json."""
global DB_CACHE, BASE_URL_CACHE
sql_loaded = False
try:
log.info("Syncing cache from MySQL database on startup...")
conn = get_db_connection()
with conn.cursor() as cursor:
cursor.execute("SELECT id, url FROM base_urls")
url_rows = cursor.fetchall()
cursor.execute("SELECT id, name, artists, lyrics_sources, canvas_sources, stream_sources FROM songs")
song_rows = cursor.fetchall()
conn.close()
with DB_CACHE_LOCK:
DB_CACHE = {row["id"]: row for row in song_rows}
with BASE_URL_CACHE_LOCK:
BASE_URL_CACHE = {row["id"]: row["url"] for row in url_rows}
BASE_URL_CACHE[2] = "https://velamhere-img.hf.space"
log.info(f"Successfully synced {len(song_rows)} songs and {len(url_rows)} base URLs from MySQL.")
sql_loaded = True
except Exception as sql_ex:
log.warning(f"Failed to sync from MySQL database: {sql_ex}")
if not sql_loaded:
try:
url = f"https://huggingface.co/buckets/{BUCKET_ID}/resolve/db_backup.json"
headers = {}
if HF_TOKEN:
headers["Authorization"] = f"Bearer {HF_TOKEN}"
log.info(f"Fallback: loading database cache from HF backup ({url})...")
with httpx.Client(follow_redirects=True) as client:
resp = client.get(url, headers=headers)
if resp.status_code == 200:
data = resp.json()
songs_list = data.get("songs", [])
if not songs_list:
raise ValueError("HF backup JSON contains 0 songs")
has_lyrics_metadata = any("lyrics_sources" in s for s in songs_list)
if not has_lyrics_metadata:
raise ValueError("HF backup JSON is missing lyrics_sources metadata (old schema)")
with DB_CACHE_LOCK:
DB_CACHE = {s["id"]: s for s in songs_list}
base_urls_list = data.get("base_urls", [])
with BASE_URL_CACHE_LOCK:
BASE_URL_CACHE = {}
for item in base_urls_list:
if isinstance(item, dict):
BASE_URL_CACHE[int(item["id"])] = item["url"]
elif isinstance(item, (list, tuple)) and len(item) >= 2:
BASE_URL_CACHE[int(item[0])] = item[1]
BASE_URL_CACHE[2] = "https://velamhere-img.hf.space"
log.info(f"Successfully loaded {len(DB_CACHE)} songs and {len(BASE_URL_CACHE)} base URLs from HF backup JSON.")
else:
log.warning(f"HF backup JSON not found (HTTP {resp.status_code}).")
except Exception as e:
log.error(f"Failed to load cache from HF backup fallback: {e}")
# Regular expression to mask URLs
URL_PATTERN = re.compile(r'https?://[^\s]+')
def mask_urls(text: str) -> str:
return URL_PATTERN.sub('[URL]', text)
def get_avg_uploads_per_min() -> float:
now = time.time()
while UPLOAD_TIMESTAMPS and UPLOAD_TIMESTAMPS[0] < now - 120:
UPLOAD_TIMESTAMPS.popleft()
return len(UPLOAD_TIMESTAMPS) / 2.0
def get_ssl_config():
ca_paths = [
"/etc/ssl/certs/ca-certificates.crt",
"/etc/ssl/cert.pem",
"/usr/local/etc/openssl/cert.pem",
"/etc/pki/tls/certs/ca-bundle.crt",
]
for path in ca_paths:
if os.path.exists(path):
return {"ca": path, "check_hostname": True}
return {"check_hostname": True}
def get_db_connection():
parsed = urllib.parse.urlparse(DB_URL)
host = parsed.hostname
port = parsed.port or 4000
user = parsed.username
password = parsed.password
db = parsed.path.lstrip('/')
ssl_config = get_ssl_config()
return pymysql.connect(
host=host,
port=port,
user=user,
password=password,
database=db,
ssl=ssl_config,
cursorclass=pymysql.cursors.DictCursor
)
def get_base_url(url_id: int) -> str:
"""Fetch base URL from cached storage or database lookup."""
with BASE_URL_CACHE_LOCK:
if url_id in BASE_URL_CACHE:
return BASE_URL_CACHE[url_id]
try:
conn = get_db_connection()
with conn.cursor() as cursor:
cursor.execute("SELECT url FROM base_urls WHERE id = %s", (url_id,))
row = cursor.fetchone()
conn.close()
if row:
url = row["url"]
with BASE_URL_CACHE_LOCK:
BASE_URL_CACHE[url_id] = url
return url
except Exception as e:
log.error(f"Failed to fetch base url for ID {url_id}: {e}")
return ""
def load_db_stats():
"""Load stats from DB cache to know how many streams are already done and total songs."""
try:
with DB_CACHE_LOCK:
total_count = len(DB_CACHE)
done_count = sum(1 for song in DB_CACHE.values() if song.get("stream_sources"))
STATS["done"] = done_count
STATS["total"] = total_count
log.info(f"Loaded DB stats from cache: Total Songs = {total_count}, Cached Streams = {done_count}")
except Exception as e:
log.error(f"Failed to load DB stats: {e}")
def init_db():
"""Ensure the songs and base_urls tables exist, run migrations, and seed default URLs."""
try:
conn = get_db_connection()
with conn.cursor() as cursor:
# 1. Create base_urls table
cursor.execute("""
CREATE TABLE IF NOT EXISTS base_urls (
id INT AUTO_INCREMENT PRIMARY KEY,
url VARCHAR(500) UNIQUE NOT NULL
)
""")
conn.commit()
# 2. Seed default base_urls
cursor.execute("""
INSERT INTO base_urls (id, url) VALUES
(1, 'https://huggingface.co/buckets/allnewuser/lyrics/resolve'),
(2, 'https://velamhere-img.hf.space'),
(3, 'https://velamhere-img.hf.space/song'),
(4, 'https://huggingface.co/buckets/shashwatIDR/lossless/resolve')
ON DUPLICATE KEY UPDATE url = VALUES(url)
""")
conn.commit()
# 3. Create songs table if not exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS songs (
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
artists VARCHAR(500),
lyrics_sources VARCHAR(50),
canvas_sources VARCHAR(50),
stream_sources VARCHAR(50),
lossless VARCHAR(50)
)
""")
conn.commit()
# Add stream_sources column if it does not exist
cursor.execute("SHOW COLUMNS FROM songs LIKE 'stream_sources'")
if not cursor.fetchone():
cursor.execute("ALTER TABLE songs ADD COLUMN stream_sources VARCHAR(50)")
conn.commit()
cursor.execute("SHOW COLUMNS FROM songs LIKE 'lossless'")
if not cursor.fetchone():
cursor.execute("ALTER TABLE songs ADD COLUMN lossless VARCHAR(50)")
conn.commit()
log.info("Added lossless column to songs table.")
else:
log.info("Database schema verified.")
conn.close()
# Load database cache and stats
load_db_cache()
load_db_stats()
except Exception as e:
log.error(f"Database initialization failed: {e}")
DB_BACKUP_INTERVAL = int(os.environ.get("DB_BACKUP_INTERVAL", "180"))
def db_backup_loop():
log.info(f"Starting database backup daemon thread (interval: {DB_BACKUP_INTERVAL}s, dirty-checked)...")
while True:
try:
time.sleep(DB_BACKUP_INTERVAL)
try:
import db_service
if not getattr(db_service, "DB_CACHE_DIRTY", True):
continue
except Exception:
pass
# Read from memory cache instead of DB
with DB_CACHE_LOCK:
songs = list(DB_CACHE.values())
# SAFEGUARD: Never overwrite backup with empty data (prevents clearing backup if DB is down)
if not songs:
log.warning("DB backup skipped: DB_CACHE is empty. Refusing to overwrite db_backup.json.")
continue
with BASE_URL_CACHE_LOCK:
base_urls = [{"id": k, "url": v} for k, v in BASE_URL_CACHE.items()]
try:
conn = get_db_connection()
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM base_urls")
db_base_urls = cursor.fetchall()
if db_base_urls:
base_urls = db_base_urls
conn.close()
except Exception as conn_err:
log.warning(f"Database connection unavailable during backup, using in-memory base_urls: {conn_err}")
backup_data = {
"songs": songs,
"base_urls": base_urls
}
json_bytes = json.dumps(backup_data, default=str, separators=(',', ':')).encode('utf-8')
from huggingface_hub import batch_bucket_files
batch_bucket_files(
BUCKET_ID,
add=[(json_bytes, "db_backup.json")],
token=HF_TOKEN
)
del json_bytes
try:
import db_service
db_service.cleanup_upload_cache()
db_service.DB_CACHE_DIRTY = False
except Exception:
pass
log.info(f"Database backup ({len(songs)} songs) successfully uploaded to Hugging Face (db_backup.json).")
except Exception as e:
log.error(f"Failed to backup database to Hugging Face: {e}")
# Initialize FastAPI App
app = FastAPI(title="Muzo Stream Service", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
def startup_event():
init_db()
# Start database backup loop in a separate daemon thread
backup_thread = threading.Thread(target=db_backup_loop, daemon=True)
backup_thread.start()
def trigger_startup_sync():
log.info("Automatic startup sync disabled by configuration.")
def db_get_song_stream(video_id: str) -> Optional[dict]:
with DB_CACHE_LOCK:
return DB_CACHE.get(video_id)
def fetch_correct_metadata(video_id: str) -> tuple[str, str]:
"""Fetch track metadata (title, artist) using upstream metadata service API."""
worker_api = os.environ.get("METADATA_API_BASE", os.environ.get("WORKER_API_BASE", "https://beautiful-junie-scoder-53c5187c.koyeb.app"))
url = f"{worker_api.rstrip('/')}/?id={video_id}"
try:
with httpx.Client(timeout=15.0, follow_redirects=True) as client:
resp = client.get(url)
if resp.status_code == 200:
data = resp.json()
matched = data.get("matched_track", {})
yt = data.get("ytmusic", {})
title = matched.get("title") or yt.get("title") or ""
artist = matched.get("artist") or yt.get("artist") or ""
if title:
return title.strip(), artist.strip()
except Exception as e:
log.warning(f"Metadata API fetch failed for {video_id}: {e}")
return "Unknown Song", "Unknown Artist"
def db_save_stream_source(video_id: str, value: str = "m4a"):
# Determine metadata from memory cache
name = ""
artists = ""
with DB_CACHE_LOCK:
existing = DB_CACHE.get(video_id)
if existing:
name = existing.get("name") or ""
artists = existing.get("artists") or ""
# Only fetch metadata if it is missing or invalid
if not name or name == "Unknown Song" or not artists:
name, artists = fetch_correct_metadata(video_id)
# Update in-memory cache first
with DB_CACHE_LOCK:
if video_id in DB_CACHE:
DB_CACHE[video_id]["stream_sources"] = value
if name and name != "Unknown Song":
DB_CACHE[video_id]["name"] = name
if artists and artists != "Unknown Artist":
DB_CACHE[video_id]["artists"] = artists
else:
DB_CACHE[video_id] = {
"id": video_id,
"name": name,
"artists": artists,
"stream_sources": value
}
try:
# Update DB
conn = get_db_connection()
with conn.cursor() as cursor:
# Query for existing metadata
cursor.execute("SELECT id FROM songs WHERE id = %s", (video_id,))
row = cursor.fetchone()
if row:
cursor.execute("""
UPDATE songs SET name = %s, artists = %s, stream_sources = %s WHERE id = %s
""", (name, artists, value, video_id))
else:
cursor.execute("""
INSERT INTO songs (id, name, artists, stream_sources)
VALUES (%s, %s, %s, %s)
""", (video_id, name, artists, value))
conn.commit()
conn.close()
log.info(f"Updated stream_sources to {value} for {video_id}")
except Exception as e:
log.error(f"Failed to save stream source to DB for {video_id} (preserved in memory cache): {e}")
# --------------------------------------------------------------------------
# Automatic Cache Clearance Logic
# --------------------------------------------------------------------------
def check_and_clear_cache():
"""Check if Hugging Face bucket streams size exceeds 20 GB, and if so, purge all streams and clear DB references."""
return
def download_and_upload_loop(video_id: str) -> Optional[str]:
"""Loop through Invidious instances, fetch stream metadata, download (30s timeout), and upload to HF."""
STREAM_JOBS[video_id] = {"status": "progressing", "timestamp": time.time()}
STATS["progressing"] = sum(1 for j in STREAM_JOBS.values() if j["status"] in ["fetching", "progressing"])
for instance in INVIDIOUS_INSTANCES:
try:
log.info(f"Attempting download/upload for {video_id} using {instance}")
# Fetch metadata and download audio in one HTTP client context with 60s timeout
with httpx.Client(timeout=30.0) as client:
url = f"{instance.rstrip('/')}/api/v1/videos/{video_id}"
if not is_safe_domain_url(url):
log.error(f"SECURITY: Blocked attempt to contact direct IP: {url}")
continue
resp = client.get(url)
if resp.status_code != 200:
log.warning(f"Invidious API call to {instance} failed: HTTP {resp.status_code}")
continue
try:
data = resp.json()
except ValueError:
log.warning(f"Failed to decode Invidious API JSON from {instance} for {video_id}")
continue
formats = data.get("adaptiveFormats", [])
# Filter and prioritize m4a formats
m4a_formats = [
f for f in formats
if f.get("type", "").startswith("audio/") and
("mp4" in f.get("type", "") or f.get("container") == "m4a")
]
if m4a_formats:
m4a_formats.sort(key=lambda x: int(x.get("bitrate") or 0), reverse=True)
selected_format = m4a_formats[0]
else:
other_formats = [f for f in formats if f.get("type", "").startswith("audio/")]
if other_formats:
other_formats.sort(key=lambda x: int(x.get("bitrate") or 0), reverse=True)
selected_format = other_formats[0]
else:
selected_format = None
if not selected_format:
log.warning(f"No audio formats found on Invidious instance {instance}")
continue
original_url = selected_format.get("url")
container = selected_format.get("container")
if not container:
mime_type = selected_format.get("type", "")
container = "webm" if "webm" in mime_type else "m4a"
ext = container or "m4a"
# Reconstruct proxied URL using the same instance
parsed = urllib.parse.urlparse(original_url)
original_host = parsed.netloc
proxied_url = f"{instance.rstrip('/')}/videoplayback?{parsed.query}&host={original_host}"
if not is_safe_domain_url(proxied_url):
log.error(f"SECURITY: Blocked attempt to stream from direct IP: {proxied_url}")
continue
t0 = time.time()
log.info(f"Downloading stream via proxy {instance}...")
stream_resp = client.get(proxied_url)
if stream_resp.status_code != 200:
log.warning(f"Stream download failed from proxy {instance}: HTTP {stream_resp.status_code}. Trying direct /stream endpoint...")
stream_resp = client.get(f"{instance.rstrip('/')}/stream/{video_id}")
if stream_resp.status_code != 200:
continue
if stream_resp.headers.get("x-audio-extension"):
ext = stream_resp.headers.get("x-audio-extension")
audio_bytes = stream_resp.content
t1 = time.time()
dl_duration = t1 - t0
if dl_duration > 0:
dl_speed = len(audio_bytes) / dl_duration / 1024.0
NETWORK_STATS["last_download_speed_kbps"] = dl_speed
NETWORK_STATS["total_downloaded_mb"] += len(audio_bytes) / (1024.0 * 1024.0)
# Upload to HF
log.info(f"Uploading {len(audio_bytes)} bytes to HF bucket for {video_id}.{ext}")
t0 = time.time()
from huggingface_hub import batch_bucket_files
batch_bucket_files(
BUCKET_ID,
add=[(audio_bytes, f"{video_id}.{ext}")],
token=HF_TOKEN
)
t1 = time.time()
ul_duration = t1 - t0
if ul_duration > 0:
ul_speed = len(audio_bytes) / ul_duration / 1024.0
NETWORK_STATS["last_upload_speed_kbps"] = ul_speed
NETWORK_STATS["total_uploaded_mb"] += len(audio_bytes) / (1024.0 * 1024.0)
# Update DB with ext
db_save_stream_source(video_id, ext)
STREAM_JOBS[video_id] = {"status": "done", "timestamp": time.time()}
STATS["done"] += 1
UPLOAD_TIMESTAMPS.append(time.time())
log.info(f"Stream upload successfully completed for {video_id}.{ext} using {instance}")
# Post-upload check for cache limits
check_and_clear_cache()
return ext
except Exception as e:
log.warning(f"Exception using instance {instance} for {video_id}: {e}")
if not isinstance(e, (httpx.HTTPError, httpx.NetworkError, httpx.TimeoutException, ValueError, KeyError)):
err_msg = f"{type(e).__name__} during sync for {video_id}: {str(e)}"
if err_msg not in FATAL_ERRORS:
FATAL_ERRORS.append(err_msg)
continue
# All instances failed - Try ultimate fallback
log.info(f"All Invidious instances failed. Attempting ultimate fallback for {video_id}...")
try:
fallback_url = f"https://effective-disco-3nfb.onrender.com/api/stream/{video_id}"
with httpx.Client(timeout=60.0, follow_redirects=True) as client:
t0 = time.time()
resp = client.get(fallback_url)
if resp.status_code == 200:
audio_bytes = resp.content
t1 = time.time()
dl_duration = t1 - t0
if dl_duration > 0:
dl_speed = len(audio_bytes) / dl_duration / 1024.0
NETWORK_STATS["last_download_speed_kbps"] = dl_speed
NETWORK_STATS["total_downloaded_mb"] += len(audio_bytes) / (1024.0 * 1024.0)
ext = "m4a" # default to m4a
content_type = resp.headers.get("content-type", "")
if "webm" in content_type:
ext = "webm"
elif "mp3" in content_type:
ext = "mp3"
log.info(f"Uploading fallback {len(audio_bytes)} bytes to HF bucket for {video_id}.{ext}")
t0_up = time.time()
from huggingface_hub import batch_bucket_files
batch_bucket_files(
BUCKET_ID,
add=[(audio_bytes, f"{video_id}.{ext}")],
token=HF_TOKEN
)
t1_up = time.time()
ul_duration = t1_up - t0_up
if ul_duration > 0:
ul_speed = len(audio_bytes) / ul_duration / 1024.0
NETWORK_STATS["last_upload_speed_kbps"] = ul_speed
NETWORK_STATS["total_uploaded_mb"] += len(audio_bytes) / (1024.0 * 1024.0)
db_save_stream_source(video_id, ext)
STREAM_JOBS[video_id] = {"status": "done", "timestamp": time.time()}
STATS["done"] += 1
UPLOAD_TIMESTAMPS.append(time.time())
log.info(f"Stream upload successfully completed for {video_id}.{ext} via ultimate fallback")
return ext
except Exception as fallback_ex:
log.error(f"Ultimate fallback failed for {video_id}: {fallback_ex}")
STREAM_JOBS[video_id] = {"status": "failed", "error": "All Invidious instances and ultimate fallback failed", "timestamp": time.time()}
STATS["failed"] += 1
STATS["progressing"] = sum(1 for j in STREAM_JOBS.values() if j["status"] in ["fetching", "progressing"])
return None
@app.get("/api/stream")
async def get_stream(id: str = Query(..., description="YouTube videoId"), background_tasks: BackgroundTasks = None):
# 1. Check if stream is already cached
row = db_get_song_stream(id)
name = ""
artists = ""
lossless_val = None
if row:
name = row.get("name") or ""
artists = row.get("artists") or ""
stream_src = row.get("stream_sources") or ""
lossless_src = row.get("lossless") or ""
if lossless_src:
lossless_val = f"https://huggingface.co/buckets/shashwatIDR/lossless/resolve/{id}.flac"
else:
# Enqueue background lossless processing if not already present
lossless_val = "processing in lossless"
try:
from queue_manager import trigger_lossless_backup
if background_tasks:
background_tasks.add_task(trigger_lossless_backup)
except Exception:
pass
if stream_src:
cached_url = f"https://velamhere-img.hf.space/song/{id}"
log.info(f"Cache hit: returning stream URL for {id}")
return {"id": id, "name": name, "artist": artists, "url": cached_url, "lossless": lossless_val}
else:
# Increment total count as this song ID is new to the DB
STATS["total"] += 1
# 2. Cache miss: download and upload using the retry loop
ext = download_and_upload_loop(id)
if not ext:
raise HTTPException(status_code=502, detail="Failed to fetch and cache stream from all Invidious instances")
cached_url = f"https://velamhere-img.hf.space/song/{id}"
row = db_get_song_stream(id) or {}
lossless_src = row.get("lossless") or ""
if lossless_src:
lossless_val = f"https://huggingface.co/buckets/shashwatIDR/lossless/resolve/{id}.flac"
else:
lossless_val = "processing in lossless"
log.info(f"Cache miss resolved: returning stream URL for {id}")
return {"id": id, "name": name, "artist": artists, "url": cached_url, "lossless": lossless_val}
def bulk_sync_worker(song_ids):
from concurrent.futures import ThreadPoolExecutor
log.info(f"Bulk sync worker started for {len(song_ids)} songs with 100 parallel threads")
def sync_single_song(song_id):
try:
# Check if already processed while waiting
row = db_get_song_stream(song_id)
if row:
stream_src = row.get("stream_sources") or ""
if stream_src:
return
log.info(f"Syncing stream for {song_id}")
download_and_upload_loop(song_id)
except Exception as e:
log.error(f"Error syncing {song_id}: {e}")
with ThreadPoolExecutor(max_workers=100) as executor:
executor.map(sync_single_song, song_ids)
log.info("Bulk sync worker completed.")
@app.post("/api/stream/sync")
def sync_streams(background_tasks: BackgroundTasks):
"""Scan all songs in DB and cache streams for those that don't have it yet."""
try:
with DB_CACHE_LOCK:
song_ids = [song_id for song_id, song in DB_CACHE.items() if not song.get("stream_sources")]
log.info(f"Sync triggered: found {len(song_ids)} songs to cache.")
if song_ids:
background_tasks.add_task(bulk_sync_worker, song_ids)
return {"message": f"Sync started in background for {len(song_ids)} songs."}
except Exception as e:
log.error(f"Sync failed to start: {e}")
raise HTTPException(status_code=502, detail=str(e))
@app.post("/api/lossless/sync")
def sync_lossless_backup(background_tasks: BackgroundTasks):
"""Trigger background bulk sync for all songs missing lossless audio."""
try:
from queue_manager import trigger_lossless_backup
count = trigger_lossless_backup()
return {"message": f"Lossless bulk backup started: queued {count} songs missing lossless audio."}
except Exception as e:
log.error(f"Lossless sync failed to start: {e}")
raise HTTPException(status_code=500, detail=str(e))
# Internet Archive endpoints removed
@app.get("/api/stats")
def get_stats():
total, lyrics, canvas, stream, lossless_count = 0, 0, 0, 0, 0
try:
with DB_CACHE_LOCK:
total = len(DB_CACHE)
for song in DB_CACHE.values():
if song.get("lyrics_sources"):
lyrics += 1
if song.get("canvas_sources"):
canvas += 1
if song.get("stream_sources"):
stream += 1
lossless_val = str(song.get("lossless") or "").strip()
if lossless_val and lossless_val != "0":
lossless_count += 1
except Exception as e:
log.error(f"Failed to calculate stats from cache: {e}")
try:
from queue_manager import LOSSLESS_STATS
except ImportError:
LOSSLESS_STATS = {"queued": 0, "progressing": 0, "done": 0, "failed": 0}
return {
"total": total,
"lyrics": lyrics,
"canvas": canvas,
"stream": stream,
"lossless": lossless_count,
"lossless_stats": LOSSLESS_STATS,
"stats": STATS,
"avg_uploads_per_min": get_avg_uploads_per_min(),
"network_stats": NETWORK_STATS,
"fatal_errors": list(FATAL_ERRORS),
"jobs": {jid: {"status": job["status"], "timestamp": job["timestamp"]} for jid, job in STREAM_JOBS.items()},
"logs": [mask_urls(line) for line in RECENT_LOGS]
}
@app.get("/api/search")
def search_songs(q: Optional[str] = None, limit: int = 12):
try:
results = []
q_lower = q.lower() if q else None
with DB_CACHE_LOCK:
songs = list(DB_CACHE.values())
if q_lower:
for s in songs:
s_id = s.get("id") or ""
s_name = s.get("name") or ""
s_artists = s.get("artists") or ""
if (q_lower in s_id.lower() or
q_lower in s_name.lower() or
q_lower in s_artists.lower()):
results.append(s)
if len(results) >= limit:
break
else:
# Return last N songs
results = songs[-limit:]
results.reverse()
return results
except Exception as e:
log.error(f"Search failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/lyrics/{video_id}")
async def get_lyrics(video_id: str):
try:
with DB_CACHE_LOCK:
row = DB_CACHE.get(video_id)
if not row or not row.get("lyrics_sources"):
raise HTTPException(status_code=404, detail="No cached lyrics for this videoId in fallback DB")
lyrics_src = row["lyrics_sources"]
url_id = 1
ext = "json"
if ":" in lyrics_src:
parts = lyrics_src.split(":")
try:
url_id = int(parts[0])
except ValueError:
url_id = 1
if len(parts) > 1:
ext = parts[1]
with BASE_URL_CACHE_LOCK:
url_base = BASE_URL_CACHE.get(url_id)
if url_base:
target_url = f"{url_base}/{video_id}.{ext}"
else:
target_url = f"https://huggingface.co/buckets/allnewuser/lyrics/resolve/{video_id}.{ext}"
headers = {}
if HF_TOKEN:
headers["Authorization"] = f"Bearer {HF_TOKEN}"
async with httpx.AsyncClient(follow_redirects=True) as client:
resp = await client.get(target_url, headers=headers)
if resp.status_code != 200:
raise HTTPException(status_code=resp.status_code, detail="Failed to fetch lyrics from Hugging Face bucket/IA")
try:
return resp.json()
except Exception:
return HTMLResponse(content=resp.text, media_type="application/json")
except HTTPException:
raise
except Exception as e:
log.error(f"Lyrics fetch failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/canvas")
def get_canvas(id: str):
try:
with DB_CACHE_LOCK:
row = DB_CACHE.get(id)
if not row or not row.get("canvas_sources"):
raise HTTPException(status_code=404, detail="No cached canvas for this id in fallback DB")
with BASE_URL_CACHE_LOCK:
url_base = BASE_URL_CACHE.get(2)
if not url_base or "huggingface.co/buckets" in str(url_base):
url_base = "https://velamhere-img.hf.space"
stream_url = f"{url_base}/{id}"
return {
"id": id,
"name": row["name"],
"artist": row["artists"],
"url": stream_url
}
except HTTPException:
raise
except Exception as e:
log.error(f"Canvas fetch failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/canvas/stream/{video_id}")
def stream_canvas(video_id: str):
try:
with BASE_URL_CACHE_LOCK:
url_base = BASE_URL_CACHE.get(2)
if not url_base or "huggingface.co/buckets" in str(url_base):
url_base = "https://velamhere-img.hf.space"
redirect_url = f"{url_base}/{video_id}"
return RedirectResponse(url=redirect_url, status_code=307)
except Exception as e:
log.error(f"Canvas stream redirect failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/stream/listen/{video_id}")
def listen_stream(video_id: str):
try:
row = db_get_song_stream(video_id)
url_id = 3
ext = None
if row and row.get("stream_sources"):
stream_src = row["stream_sources"]
if ":" in stream_src:
parts = stream_src.split(":")
try:
url_id = int(parts[0])
except ValueError:
url_id = 3
if len(parts) > 1:
ext = parts[1]
else:
ext = stream_src
# If not cached yet, download and upload it first
if not ext:
log.info(f"Stream miss in listen_stream for {video_id}, downloading and uploading...")
ext = download_and_upload_loop(video_id)
if not ext:
raise HTTPException(status_code=502, detail="Failed to fetch and cache stream")
with BASE_URL_CACHE_LOCK:
url_base = BASE_URL_CACHE.get(url_id)
if url_base:
redirect_url = f"{url_base}/{video_id}"
else:
redirect_url = f"https://velamhere-img.hf.space/song/{video_id}"
return RedirectResponse(url=redirect_url, status_code=307)
except HTTPException:
raise
except Exception as e:
log.error(f"Stream listen redirect failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.api_route("/api/stream/add", methods=["GET", "POST"])
def add_stream_to_queue(id: str = Query(..., description="YouTube videoId or comma-separated videoIds"), background_tasks: BackgroundTasks = None):
"""Add video ID(s) to the stream processing queue (in-memory execution). Accepts comma-separated IDs."""
try:
raw_ids = [v.strip() for v in id.split(",") if v.strip()]
if not raw_ids:
raise HTTPException(status_code=400, detail="No valid IDs provided")
cached_ids = []
queued_ids = []
for vid in raw_ids:
row = db_get_song_stream(vid)
if row and row.get("stream_sources"):
cached_ids.append(vid)
else:
queued_ids.append(vid)
if background_tasks:
background_tasks.add_task(download_and_upload_loop, vid)
else:
thread = threading.Thread(target=download_and_upload_loop, args=(vid,), daemon=True)
thread.start()
return {
"message": f"Processed {len(raw_ids)} ID(s). {len(queued_ids)} queued, {len(cached_ids)} already cached.",
"queued": queued_ids,
"cached": cached_ids
}
except HTTPException:
raise
except Exception as e:
log.error(f"Failed to add streams to queue: {e}")
@app.get("/status")
@app.get("/api/status")
def get_service_status():
lossless_count = 0
m4a_count = 0
try:
with DB_CACHE_LOCK:
for song in DB_CACHE.values():
lossless_val = str(song.get("lossless") or "").strip()
if lossless_val and lossless_val != "0":
lossless_count += 1
stream_src = str(song.get("stream_sources") or "").strip().lower()
if stream_src:
ext = stream_src.split(":")[-1] if ":" in stream_src else stream_src
if ext == "m4a":
m4a_count += 1
except Exception as e:
log.error(f"Failed to calculate status stats: {e}")
try:
from queue_manager import TASK_QUEUE, LOSSLESS_TASK_QUEUE, QUEUED_SET, LOSSLESS_QUEUED_SET, QUEUED_SET_LOCK, LOSSLESS_QUEUED_LOCK
with QUEUED_SET_LOCK:
stream_queued_ids = set(QUEUED_SET)
with LOSSLESS_QUEUED_LOCK:
lossless_queued_ids = set(LOSSLESS_QUEUED_SET)
combined_queued = len(stream_queued_ids | lossless_queued_ids)
sq_size = TASK_QUEUE.qsize()
lq_size = LOSSLESS_TASK_QUEUE.qsize()
except Exception:
combined_queued = STATS.get("progressing", 0)
sq_size = 0
lq_size = 0
return {
"status": "online",
"lossless": lossless_count,
"m4a": m4a_count,
"in_queue": combined_queued,
"queue_details": {
"stream_queue": sq_size,
"lossless_queue": lq_size,
"total_combined_queued": combined_queued
}
}
@app.get("/admin", response_class=HTMLResponse)
def get_admin_page():
try:
import admin
return HTMLResponse(content=admin.get_admin_html())
except Exception:
import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import admin
return HTMLResponse(content=admin.get_admin_html())
@app.get("/", response_class=HTMLResponse)
def dashboard():
html_content = r"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MLC Playground</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Bungee&family=Space+Mono:wght@400;700&family=Manrope:wght@400;500;600;700;800&display=swap"
rel="stylesheet">
<style>
:root {
/* ── Jukebox Console Palette ─────────────────── */
--bg: #14100c;
--bg-raised: #1c1610;
--metal: #2b241d;
--metal-light: #3c3227;
--glass: #1c1712;
--glass-deep: #110d0a;
--cream: #f4e9d2;
--cream-dim: #c9b78f;
--ink: #241c14;
--amber: #ffb733;
--amber-soft: rgba(255, 183, 51, 0.14);
--amber-glow: rgba(255, 183, 51, 0.55);
--cyan: #63e6ea;
--cyan-soft: rgba(99, 230, 234, 0.14);
--cyan-glow: rgba(99, 230, 234, 0.55);
--red: #c1483c;
--red-soft: rgba(193, 72, 60, 0.16);
--rule: rgba(244, 233, 210, 0.10);
--rule-strong: rgba(244, 233, 210, 0.20);
--font-display: "Bungee", system-ui, sans-serif;
--font-body: "Manrope", -apple-system, sans-serif;
--font-mono: "Space Mono", monospace;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
scrollbar-color: var(--metal-light) var(--bg-raised);
}
body {
background-color: var(--bg);
background-image:
radial-gradient(ellipse 900px 500px at 15% -10%, rgba(255, 183, 51, 0.06), transparent 60%),
radial-gradient(ellipse 900px 500px at 90% 0%, rgba(99, 230, 234, 0.05), transparent 60%);
color: var(--cream);
font-family: var(--font-body);
min-height: 100vh;
display: flex;
flex-direction: column;
overflow-x: hidden;
-webkit-font-smoothing: antialiased;
}
::selection {
background: var(--amber-soft);
color: var(--amber);
}
/* Brushed metal texture overlay */
.metal-bg {
position: fixed;
inset: 0;
z-index: -1;
opacity: 0.5;
background-image: repeating-linear-gradient(180deg, rgba(255, 255, 255, 0.012) 0px, rgba(255, 255, 255, 0.012) 1px, transparent 1px, transparent 3px);
pointer-events: none;
}
header {
border-bottom: 2px solid var(--rule-strong);
background:
repeating-linear-gradient(100deg, rgba(255, 255, 255, 0.015) 0px, rgba(255, 255, 255, 0.015) 1px, transparent 1px, transparent 5px),
var(--metal);
padding: 1.35rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
}
.marquee {
background: var(--glass-deep);
border: 3px solid var(--metal-light);
border-radius: 10px;
padding: 0.6rem 1.4rem;
box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.6), inset 0 0 30px rgba(255, 183, 51, 0.05);
animation: flicker-in 1.4s ease-out;
}
@keyframes flicker-in {
0% {
opacity: 0;
}
8% {
opacity: 1;
}
12% {
opacity: 0.2;
}
20% {
opacity: 1;
}
26% {
opacity: 0.4;
}
34% {
opacity: 1;
}
100% {
opacity: 1;
}
}
.marquee h1 {
font-family: var(--font-display);
font-size: 1.4rem;
letter-spacing: 0.02em;
color: var(--amber);
text-shadow: 0 0 4px var(--amber-glow), 0 0 16px rgba(255, 183, 51, 0.35);
display: inline;
}
.marquee h1 .sep {
color: var(--cream-dim);
font-size: 1rem;
margin: 0 0.35rem;
text-shadow: none;
}
.marquee h1 em {
font-style: normal;
color: var(--cyan);
text-shadow: 0 0 4px var(--cyan-glow), 0 0 16px rgba(99, 230, 234, 0.35);
display: inline;
}
.marquee p {
color: var(--cream-dim);
font-family: var(--font-mono);
font-size: 0.68rem;
letter-spacing: 0.04em;
margin-top: 0.3rem;
text-transform: uppercase;
}
.header-right {
display: flex;
align-items: center;
gap: 0.85rem;
flex-wrap: wrap;
}
.vu-pill {
border: 1px solid var(--rule-strong);
padding: 0.4rem 0.85rem;
border-radius: 6px;
background: var(--glass);
display: flex;
align-items: center;
gap: 0.5rem;
font-family: var(--font-mono);
font-size: 0.72rem;
color: var(--cream-dim);
text-transform: uppercase;
letter-spacing: 0.04em;
box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.5);
}
.vu-pill .led {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 6px var(--amber-glow), 0 0 2px var(--amber);
flex-shrink: 0;
}
.vu-pill.cyan-led .led {
background: var(--cyan);
box-shadow: 0 0 6px var(--cyan-glow), 0 0 2px var(--cyan);
}
.vu-pill strong {
color: var(--cream);
font-weight: 700;
font-size: 0.85rem;
}
.chrome-badge {
font-family: var(--font-mono);
background: linear-gradient(180deg, var(--metal-light), var(--metal));
border: 1px solid var(--rule-strong);
color: var(--cream-dim);
padding: 0.45rem 0.9rem;
border-radius: 99px;
font-size: 0.72rem;
font-weight: 500;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08), 0 1px 3px rgba(0, 0, 0, 0.4);
}
.app-layout {
flex: 1;
display: grid;
grid-template-columns: 1fr;
max-width: 1600px;
margin: 0 auto;
width: 100%;
}
@media (min-width: 1024px) {
.app-layout {
grid-template-columns: 350px 1fr;
}
}
/* Sidebar — Selector Panel */
.sidebar {
border-right: 2px solid var(--rule);
background: var(--bg-raised);
padding: 2rem 1.6rem;
display: flex;
flex-direction: column;
gap: 1.5rem;
overflow-y: auto;
}
.panel-label {
font-family: var(--font-display);
font-size: 0.72rem;
letter-spacing: 0.08em;
color: var(--amber);
margin-bottom: 1rem;
padding-bottom: 0.6rem;
border-bottom: 2px solid var(--red);
display: inline-block;
}
.endpoint-card {
background: var(--metal);
border: 1px solid var(--rule);
border-radius: 8px;
padding: 0.9rem;
margin-bottom: 0.9rem;
display: flex;
gap: 0.75rem;
transition: border-color 0.2s ease, transform 0.15s ease;
}
.endpoint-card:hover {
border-color: var(--amber);
transform: translateX(2px);
}
.endpoint-code {
flex-shrink: 0;
width: 34px;
height: 34px;
border-radius: 50%;
background: var(--glass-deep);
border: 2px solid var(--red);
color: var(--cream);
font-family: var(--font-mono);
font-weight: 700;
font-size: 0.72rem;
display: flex;
align-items: center;
justify-content: center;
}
.endpoint-badge {
display: inline-block;
font-family: var(--font-mono);
font-size: 0.62rem;
font-weight: 700;
padding: 0.1rem 0.4rem;
border-radius: 3px;
text-transform: uppercase;
background: var(--amber-soft);
color: var(--amber);
border: 1px solid rgba(255, 183, 51, 0.3);
margin-bottom: 0.4rem;
}
.endpoint-path {
font-family: var(--font-mono);
font-size: 0.78rem;
font-weight: 700;
color: var(--cream);
word-break: break-all;
}
.endpoint-desc {
font-size: 0.73rem;
color: var(--cream-dim);
margin-top: 0.4rem;
line-height: 1.45;
}
.code-snippet {
background: var(--glass-deep);
border-top: 1px dashed var(--rule-strong);
border-radius: 0 0 4px 4px;
padding: 0.5rem 0;
margin-top: 0.6rem;
font-family: var(--font-mono);
font-size: 0.66rem;
color: var(--cyan);
overflow-x: auto;
white-space: pre;
}
/* Main workspace */
.main-content {
padding: 2.25rem 2.5rem 3rem;
display: flex;
flex-direction: column;
gap: 2.5rem;
overflow-y: auto;
}
/* Search — coin slot */
.search-wrapper {
position: relative;
width: 100%;
}
.search-input {
width: 100%;
padding: 0.9rem 1rem 0.9rem 2.85rem;
background: var(--glass-deep);
border: 2px solid var(--metal-light);
border-radius: 8px;
color: var(--cream);
font-family: var(--font-mono);
font-size: 0.9rem;
outline: none;
box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.55);
transition: border-color 0.2s, box-shadow 0.2s;
}
.search-input::placeholder {
color: var(--cream-dim);
opacity: 0.6;
}
.search-input:focus {
border-color: var(--amber);
box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.55), 0 0 0 3px var(--amber-soft);
}
.search-icon {
position: absolute;
left: 1rem;
top: 50%;
transform: translateY(-50%);
color: var(--cyan);
width: 16px;
height: 16px;
}
/* Section titles with LED marker */
.section-title {
font-family: var(--font-display);
font-size: 0.85rem;
letter-spacing: 0.04em;
color: var(--cream);
margin-bottom: 1rem;
display: flex;
align-items: center;
gap: 0.6rem;
}
.section-title::before {
content: "";
display: inline-block;
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 6px var(--cyan-glow);
animation: pulse-led 2.2s ease-in-out infinite;
}
@keyframes pulse-led {
0%,
100% {
opacity: 1;
}
50% {
opacity: 0.35;
}
}
/* Song grid — title strip cards */
.songs-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
gap: 1.1rem;
}
.song-card {
position: relative;
background: var(--cream);
border-bottom: 5px solid var(--red);
border-radius: 3px;
padding: 1.1rem 1.2rem;
cursor: pointer;
transition: transform 0.18s ease, box-shadow 0.18s ease;
display: flex;
flex-direction: column;
justify-content: space-between;
min-height: 108px;
box-shadow: 0 3px 0 rgba(0, 0, 0, 0.3), 0 6px 14px rgba(0, 0, 0, 0.35);
overflow: hidden;
}
.song-card::after {
content: "";
position: absolute;
inset: 0;
background: linear-gradient(115deg, transparent 40%, rgba(255, 255, 255, 0.35) 48%, transparent 56%);
opacity: 0;
transition: opacity 0.2s ease;
pointer-events: none;
}
.song-card:hover {
transform: translateY(-3px) rotate(-0.6deg);
box-shadow: 0 5px 0 rgba(0, 0, 0, 0.3), 0 12px 22px rgba(0, 0, 0, 0.4);
}
.song-card:hover::after {
opacity: 1;
}
.song-title-text {
font-family: var(--font-display);
font-size: 0.92rem;
letter-spacing: 0.01em;
color: var(--ink);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
line-height: 1.3;
}
.song-artist {
font-family: var(--font-mono);
font-size: 0.72rem;
color: var(--ink);
opacity: 0.62;
text-transform: uppercase;
letter-spacing: 0.03em;
margin-top: 0.3rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.song-meta-tags {
display: flex;
gap: 0.4rem;
margin-top: 0.85rem;
}
.meta-tag {
font-family: var(--font-mono);
font-size: 0.6rem;
font-weight: 700;
padding: 0.18rem 0.45rem;
border-radius: 99px;
text-transform: uppercase;
letter-spacing: 0.03em;
}
.tag-lyrics {
background: var(--ink);
color: var(--cream);
}
.tag-canvas {
background: var(--red);
color: var(--cream);
}
.tag-stream {
background: var(--cyan);
color: var(--glass-deep);
}
/* Sandbox / service panel */
.detail-container {
display: grid;
grid-template-columns: 1fr;
gap: 1.75rem;
background: var(--bg-raised);
border: 2px solid var(--metal-light);
border-radius: 14px;
padding: 1.75rem;
position: relative;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03);
}
@media (min-width: 1200px) {
.detail-container {
grid-template-columns: 1fr 260px;
}
}
.lyrics-panel {
display: flex;
flex-direction: column;
min-height: 380px;
}
.lyrics-header {
border-bottom: 1px solid var(--rule-strong);
padding-bottom: 0.9rem;
margin-bottom: 1.25rem;
display: flex;
align-items: baseline;
gap: 0.6rem;
}
.lyrics-header .led {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 6px var(--amber-glow);
flex-shrink: 0;
}
.lyrics-header h2 {
font-family: var(--font-display);
font-size: 1.1rem;
color: var(--cream);
}
.lyrics-header p {
color: var(--cyan);
font-family: var(--font-mono);
font-size: 0.78rem;
margin-top: 0.15rem;
}
.lyrics-body {
flex: 1;
background: var(--glass-deep);
border: 1px solid var(--rule);
border-radius: 8px;
padding: 1.1rem 1.2rem;
font-family: var(--font-mono);
font-size: 0.88rem;
line-height: 1.85;
color: var(--cream);
overflow-y: auto;
max-height: 400px;
white-space: pre-wrap;
box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.5);
}
/* Canvas viewport bezel */
.canvas-panel {
display: flex;
flex-direction: column;
align-items: center;
gap: 1.5rem;
}
.canvas-wrapper {
width: 100%;
aspect-ratio: 9 / 16;
border-radius: 10px;
overflow: hidden;
background: #000;
border: 4px solid var(--metal-light);
position: relative;
max-width: 210px;
box-shadow: inset 0 0 0 2px var(--glass-deep), 0 4px 14px rgba(0, 0, 0, 0.5);
}
.canvas-video {
width: 100%;
height: 100%;
object-fit: cover;
}
.canvas-fallback {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: var(--cyan);
text-align: center;
padding: 1.5rem;
background:
repeating-linear-gradient(180deg, rgba(99, 230, 234, 0.03) 0px, rgba(99, 230, 234, 0.03) 1px, transparent 1px, transparent 3px),
var(--glass-deep);
font-family: var(--font-mono);
font-size: 0.7rem;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.canvas-fallback svg {
width: 22px;
height: 22px;
margin-bottom: 0.6rem;
opacity: 0.6;
}
/* Console / diagnostic CRT */
.sandbox-console {
grid-column: 1 / -1;
border-top: 1px solid var(--rule-strong);
padding-top: 1.5rem;
margin-top: 0.25rem;
}
.console-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 0.75rem;
}
.console-title {
font-family: var(--font-display);
font-size: 0.75rem;
letter-spacing: 0.04em;
color: var(--cream-dim);
display: flex;
align-items: center;
gap: 0.5rem;
}
.console-output {
background:
repeating-linear-gradient(180deg, rgba(255, 183, 51, 0.025) 0px, rgba(255, 183, 51, 0.025) 1px, transparent 1px, transparent 3px),
var(--glass-deep);
border: 1px solid var(--rule);
border-radius: 8px;
padding: 1rem 1.1rem;
font-family: var(--font-mono);
font-size: 0.74rem;
color: var(--amber);
text-shadow: 0 0 3px rgba(255, 183, 51, 0.25);
max-height: 180px;
overflow-y: auto;
white-space: pre-wrap;
box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.5);
}
.no-selection {
text-align: center;
padding: 4.5rem 2rem;
color: var(--cream-dim);
border: 2px dashed var(--metal-light);
border-radius: 12px;
font-family: var(--font-mono);
font-size: 0.85rem;
background: var(--bg-raised);
}
.no-selection strong {
display: block;
font-family: var(--font-display);
color: var(--amber);
font-size: 1rem;
margin-bottom: 0.6rem;
letter-spacing: 0.02em;
}
.hidden {
display: none !important;
}
/* Shimmer loader */
.shimmer {
background: linear-gradient(90deg, var(--cream) 25%, #e8dcc0 50%, var(--cream) 75%);
background-size: 200% 100%;
animation: loading 1.4s infinite linear;
}
@keyframes loading {
0% {
background-position: 200% 0;
}
100% {
background-position: -200% 0;
}
}
/* Ticket toast */
.toast {
position: fixed;
bottom: 2rem;
right: 2rem;
background: var(--cream);
border-left: 4px solid var(--red);
color: var(--ink);
padding: 0.85rem 1.25rem;
border-radius: 4px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
z-index: 10000;
transform: translateY(150%);
transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
font-family: var(--font-mono);
font-size: 0.8rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.toast.show {
transform: translateY(0);
}
/* Scrollbars */
.sidebar::-webkit-scrollbar,
.lyrics-body::-webkit-scrollbar,
.console-output::-webkit-scrollbar {
width: 8px;
}
.sidebar::-webkit-scrollbar-track,
.lyrics-body::-webkit-scrollbar-track,
.console-output::-webkit-scrollbar-track {
background: transparent;
}
.sidebar::-webkit-scrollbar-thumb,
.lyrics-body::-webkit-scrollbar-thumb,
.console-output::-webkit-scrollbar-thumb {
background: var(--metal-light);
border-radius: 4px;
}
</style>
</head>
<body>
<div class="metal-bg"></div>
<header>
<div class="marquee">
<h1>MLC<span class="sep">/</span><em>PLAYGROUND</em></h1>
<p>Fallback cache · synced lyrics & canvas loops</p>
</div>
<div class="header-right">
<div class="vu-pill">
<span class="led"></span> Lyrics <strong id="stats-lyrics">···</strong>
</div>
<div class="vu-pill cyan-led">
<span class="led"></span> Canvas <strong id="stats-canvas">···</strong>
</div>
<div class="vu-pill" style="border-color: var(--cyan); color: var(--cyan); margin-left: 0.5rem;">
<span class="led" style="background: var(--cyan); box-shadow: 0 0 6px var(--cyan);"></span> Stream <strong id="stats-stream">···</strong>
</div>
<div class="vu-pill" style="border-color: var(--amber); color: var(--amber); margin-left: 0.5rem;">
<span class="led" style="background: var(--amber); box-shadow: 0 0 6px var(--amber-glow);"></span> Lossless <strong id="stats-lossless">···</strong>
</div>
<div class="chrome-badge">BASE: shashwatidr-filestore.hf.space</div>
</div>
</header>
<div class="app-layout">
<!-- Sidebar / API Docs -->
<aside class="sidebar">
<div>
<span class="panel-label">Selector Panel</span>
<!-- Endpoint A1 -->
<div class="api-endpoint-card endpoint-card">
<div class="endpoint-code">A1</div>
<div style="flex:1; min-width:0;">
<span class="endpoint-badge">GET</span>
<div class="endpoint-path">/api/lyrics/:id</div>
<div class="endpoint-desc">Retrieves synced and plain-text lyrics for a given YouTube video ID. Requires
song metadata query parameters to fetch live if not cached.</div>
<div class="code-snippet">curl shashwatidr-filestore.hf.space/api/lyrics/dQw4w9WgXcQ</div>
</div>
</div>
<!-- Endpoint A2 -->
<div class="api-endpoint-card endpoint-card">
<div class="endpoint-code">A2</div>
<div style="flex:1; min-width:0;">
<span class="endpoint-badge">GET</span>
<div class="endpoint-path">/api/canvas</div>
<div class="endpoint-desc">Retrieves loop video configuration and CDN file stream URL for a given YouTube
ID.</div>
<div class="code-snippet">curl "shashwatidr-filestore.hf.space/api/canvas?id=dQw4w9WgXcQ"</div>
</div>
</div>
<!-- Endpoint A3 -->
<div class="api-endpoint-card endpoint-card">
<div class="endpoint-code">A3</div>
<div style="flex:1; min-width:0;">
<span class="endpoint-badge">GET</span>
<div class="endpoint-path">/api/canvas/stream/:id</div>
<div class="endpoint-desc">Redirects to the direct Hugging Face hosted CDN video file for low-overhead
client streaming.</div>
</div>
</div>
<!-- Endpoint A4 -->
<div class="api-endpoint-card endpoint-card">
<div class="endpoint-code">A4</div>
<div style="flex:1; min-width:0;">
<span class="endpoint-badge" style="background: var(--cyan); color: var(--glass-deep);">GET</span>
<div class="endpoint-path">/api/stream</div>
<div class="endpoint-desc">Retrieves audio stream configuration and CDN stream file URL for a given YouTube ID.</div>
<div class="code-snippet">curl "shashwatidr-filestore.hf.space/api/stream?id=dQw4w9WgXcQ"</div>
</div>
</div>
<!-- Endpoint A5 -->
<div class="api-endpoint-card endpoint-card">
<div class="endpoint-code">A5</div>
<div style="flex:1; min-width:0;">
<span class="endpoint-badge" style="background: var(--cyan); color: var(--glass-deep);">GET</span>
<div class="endpoint-path">/api/stream/listen/:id</div>
<div class="endpoint-desc">Redirects to the direct Hugging Face hosted LFS audio file.</div>
</div>
</div>
<!-- Endpoint B1 -->
<div class="api-endpoint-card endpoint-card">
<div class="endpoint-code">B1</div>
<div style="flex:1; min-width:0;">
<span class="endpoint-badge">GET</span>
<div class="endpoint-path">/api/search</div>
<div class="endpoint-desc">Queries cached database entries by song title, artists, or video ID.</div>
<div class="code-snippet">curl "shashwatidr-filestore.hf.space/api/search?q=Never+Gonna+Give"</div>
</div>
</div>
<!-- Endpoint B2 -->
<div class="api-endpoint-card endpoint-card">
<div class="endpoint-code">B2</div>
<div style="flex:1; min-width:0;">
<span class="endpoint-badge">GET</span>
<div class="endpoint-path">/api/stats</div>
<div class="endpoint-desc">Retrieves total cached counts of lyrics and canvas video loops.</div>
<div class="code-snippet">curl "shashwatidr-filestore.hf.space/api/stats"</div>
</div>
</div>
<!-- Sync & Queue Monitor -->
<div style="margin-top: 2rem; border-top: 2px solid var(--rule-strong); padding-top: 1.5rem;">
<span class="panel-label" style="border-color: var(--cyan); color: var(--cyan);">Sync & Queue Monitor</span>
<button onclick="triggerSync()" style="width: 100%; padding: 0.6rem; background: var(--cyan); border: none; border-radius: 6px; color: var(--glass-deep); font-family: var(--font-display); font-size: 0.75rem; cursor: pointer; margin-bottom: 0.5rem; box-shadow: 0 0 8px var(--cyan-soft); font-weight: bold;">
Trigger Stream Sync
</button>
<button onclick="triggerLosslessBackup()" style="width: 100%; padding: 0.6rem; background: var(--amber); border: none; border-radius: 6px; color: var(--ink); font-family: var(--font-display); font-size: 0.75rem; cursor: pointer; margin-bottom: 1rem; box-shadow: 0 0 8px var(--amber-soft); font-weight: bold;">
Start Lossless Backup
</button>
<div style="display: flex; flex-direction: column; gap: 0.5rem; font-family: var(--font-mono); font-size: 0.7rem; color: var(--cream-dim);">
<div style="display: flex; justify-content: space-between;">
<span>Queue / Progressing:</span>
<strong style="color: var(--amber);" id="val-progressing">0</strong>
</div>
<div style="display: flex; justify-content: space-between;">
<span>Cached (Done):</span>
<strong style="color: var(--cyan);" id="val-done">0</strong>
</div>
<div style="display: flex; justify-content: space-between;">
<span>Failed:</span>
<strong style="color: var(--red);" id="val-failed">0</strong>
</div>
<div style="display: flex; justify-content: space-between;">
<span>Upload Rate:</span>
<strong style="color: var(--cream);" id="val-rate">0.0 / min</strong>
</div>
<div style="display: flex; justify-content: flex-start; gap: 0.25rem; flex-direction: column; margin-top: 0.5rem; border-top: 1px dashed var(--rule); padding-top: 0.5rem;">
<div>DL: <span id="val-dl-speed" style="color: var(--cyan);">0.0 KB/s</span> (Total: <span id="val-total-dl">0.0 MB</span>)</div>
<div>UL: <span id="val-ul-speed" style="color: var(--amber);">0.0 KB/s</span> (Total: <span id="val-total-ul">0.0 MB</span>)</div>
</div>
</div>
</div>
</div>
</aside>
<!-- Main Workspace -->
<main class="main-content">
<!-- Search -->
<div class="search-wrapper">
<input type="text" class="search-input" id="searchBar" placeholder="SEARCH BY NAME, ARTIST, OR VIDEO ID...">
<svg class="search-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
</svg>
</div>
<!-- Featured Grid -->
<div>
<h3 class="section-title">Now Stocked</h3>
<div class="songs-grid" id="songsGrid">
<!-- Rendered song cards -->
</div>
</div>
<!-- Playback Sandbox Details -->
<div>
<h3 class="section-title">Test Deck</h3>
<div id="noSelection" class="no-selection">
<strong>No title loaded</strong>
Press a title strip above to run a live cache lookup and view the response payload.
</div>
<div id="detailContainer" class="detail-container hidden">
<!-- Lyrics Explorer -->
<div class="lyrics-panel">
<div class="lyrics-header">
<span class="led"></span>
<div>
<h2 id="detailTitle">Song Title</h2>
<p id="detailArtist">Artist Name</p>
</div>
</div>
<div class="lyrics-body" id="lyricsBody">
<!-- Synced lyrics content -->
</div>
</div>
<!-- Canvas Player Loop -->
<div class="canvas-panel">
<div class="canvas-wrapper">
<video class="canvas-video hidden" id="canvasVideo" autoplay loop muted playsinline></video>
<div class="canvas-fallback" id="canvasFallback">
<svg fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z">
</path>
</svg>
<p>No Signal — Canvas Not Cached</p>
</div>
</div>
</div>
<!-- Stream Player -->
<div class="stream-panel" style="margin-top: 1rem;">
<div class="stream-wrapper" style="background: var(--glass); border: 1px solid var(--rule); border-radius: 8px; padding: 1rem; display: flex; flex-direction: column; gap: 0.5rem;">
<span class="panel-label" style="font-size: 0.7rem; text-transform: uppercase; color: var(--cyan); letter-spacing: 0.05em; font-weight: 600; display: block; margin-bottom: 0.25rem;">Audio Stream</span>
<audio id="streamAudio" controls style="width: 100%; height: 32px; outline: none; display: none;"></audio>
<div id="streamFallback" style="text-align: center; color: var(--cream-dim); font-size: 0.75rem; padding: 0.5rem 0;">
No Audio Cache
</div>
<div id="streamLinkContainer" style="display: none; margin-top: 0.25rem;">
<a id="streamLink" href="#" target="_blank" style="color: var(--cyan); text-decoration: none; font-size: 0.75rem; word-break: break-all; font-family: var(--font-mono);">[HF Resolve Link]</a>
</div>
</div>
</div>
<!-- API Sandbox Console -->
<div class="sandbox-console">
<div class="console-header" style="display: flex; gap: 1rem; justify-content: flex-start; border-bottom: 1px solid var(--rule-strong); padding-bottom: 0.5rem; margin-bottom: 0.75rem;">
<button onclick="switchTab('api')" id="tab-api" style="background: none; border: none; color: var(--amber); font-family: var(--font-display); font-size: 0.72rem; cursor: pointer; border-bottom: 2px solid var(--amber); padding-bottom: 0.25rem;">Diagnostic Readout</button>
<button onclick="switchTab('logs')" id="tab-logs" style="background: none; border: none; color: var(--cream-dim); font-family: var(--font-display); font-size: 0.72rem; cursor: pointer; padding-bottom: 0.25rem;">Server Logs & Queue</button>
</div>
<div class="console-output" id="consoleOutput">
Standing by for request...
</div>
<div class="console-output hidden" id="logsQueueOutput" style="display: grid; grid-template-columns: 1.5fr 1fr; gap: 1.5rem; max-height: 250px; overflow-y: auto;">
<div style="border-right: 1px solid var(--rule-strong); padding-right: 0.5rem;">
<div style="font-family: var(--font-display); font-size: 0.6rem; color: var(--cyan); margin-bottom: 0.5rem; text-transform: uppercase;">Live Logs</div>
<div id="liveLogsContainer" style="font-family: var(--font-mono); font-size: 0.68rem; line-height: 1.4; color: var(--cream); overflow-y: auto; max-height: 200px;"></div>
</div>
<div>
<div style="font-family: var(--font-display); font-size: 0.6rem; color: var(--cyan); margin-bottom: 0.5rem; text-transform: uppercase;">Active Stream Jobs</div>
<div id="activeJobsContainer" style="font-family: var(--font-mono); font-size: 0.68rem; line-height: 1.4; color: var(--cream); overflow-y: auto; max-height: 200px;"></div>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
<div class="toast" id="toast">
<svg style="width: 16px; height: 16px;" fill="none" stroke="currentColor" viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
</svg>
<span id="toastMsg">Success</span>
</div>
<script>
const searchBar = document.getElementById('searchBar');
const songsGrid = document.getElementById('songsGrid');
const noSelection = document.getElementById('noSelection');
const detailContainer = document.getElementById('detailContainer');
const detailTitle = document.getElementById('detailTitle');
const detailArtist = document.getElementById('detailArtist');
const lyricsBody = document.getElementById('lyricsBody');
const canvasVideo = document.getElementById('canvasVideo');
const canvasFallback = document.getElementById('canvasFallback');
const consoleOutput = document.getElementById('consoleOutput');
const toast = document.getElementById('toast');
const toastMsg = document.getElementById('toastMsg');
let allSongs = [];
function logConsole(url, method, status, responseData) {
consoleOutput.innerHTML = `[HTTP] ${method} ${url}\n[STATUS] ${status}\n[RESPONSE]\n${JSON.stringify(responseData, null, 2)}`;
consoleOutput.scrollTop = 0;
}
async function loadFeaturedSongs() {
songsGrid.innerHTML = Array(4).fill(0).map(() => `
<div class="song-card shimmer">
<div style="height: 0.95rem; width: 60%; background: rgba(0,0,0,0.08); margin-bottom: 0.5rem; border-radius: 4px;"></div>
<div style="height: 0.8rem; width: 40%; background: rgba(0,0,0,0.08); border-radius: 4px;"></div>
</div>
`).join('');
try {
const res = await fetch('/api/search?limit=12');
if (res.ok) {
allSongs = await res.json();
renderSongs(allSongs);
} else {
songsGrid.innerHTML = '<p style="grid-column: 1/-1; text-align: center; color: var(--cream-dim); font-family: var(--font-mono);">Failed to load cached songs.</p>';
}
} catch (err) {
songsGrid.innerHTML = '<p style="grid-column: 1/-1; text-align: center; color: var(--cream-dim); font-family: var(--font-mono);">Error connecting to fallback worker.</p>';
}
}
function renderSongs(songs) {
if (songs.length === 0) {
songsGrid.innerHTML = '<p style="grid-column: 1/-1; text-align: center; color: var(--cream-dim); font-family: var(--font-mono);">No songs found in database.</p>';
return;
}
songsGrid.innerHTML = songs.map(song => {
const hasLyrics = !!song.lyrics_sources;
const hasCanvas = !!song.canvas_sources;
const hasStream = song.stream_sources && song.stream_sources.startsWith('3');
return `
<div class="song-card" onclick="selectSong('${song.id}', '${encodeURIComponent(song.name)}', '${encodeURIComponent(song.artists)}')">
<div>
<div class="song-title-text" title="${song.name}">${song.name}</div>
<div class="song-artist" title="${song.artists}">${song.artists || 'Unknown Artist'}</div>
</div>
<div class="song-meta-tags">
${hasLyrics ? '<span class="meta-tag tag-lyrics">Lyrics</span>' : ''}
${hasCanvas ? '<span class="meta-tag tag-canvas">Canvas</span>' : ''}
${hasStream ? '<span class="meta-tag tag-stream">Stream</span>' : ''}
</div>
</div>
`;
}).join('');
}
async function selectSong(id, name, artist) {
name = decodeURIComponent(name);
artist = decodeURIComponent(artist);
noSelection.classList.add('hidden');
detailContainer.classList.remove('hidden');
detailTitle.textContent = name;
detailArtist.textContent = artist;
lyricsBody.innerHTML = `<span style="color: var(--cream-dim)">Fetching lyrics...</span>`;
canvasVideo.classList.add('hidden');
canvasFallback.classList.remove('hidden');
consoleOutput.textContent = "Initializing request...";
// Fetch lyrics
try {
const targetUrl = `/api/lyrics/${id}?name=${encodeURIComponent(name)}&artist=${encodeURIComponent(artist)}`;
const lyricsRes = await fetch(targetUrl);
if (lyricsRes.ok) {
const data = await lyricsRes.json();
let text = data.syncedLyrics || data.plainLyrics || "No lyric content available.";
lyricsBody.innerHTML = text;
showToast("Lyrics fetched!");
logConsole(`shashwatidr-filestore.hf.space${targetUrl}`, 'GET', lyricsRes.status, data);
} else {
lyricsBody.innerHTML = `<span style="color: var(--red)">Failed to read file. Status ${lyricsRes.status}</span>`;
const errorData = await lyricsRes.json().catch(() => ({}));
logConsole(`shashwatidr-filestore.hf.space${targetUrl}`, 'GET', lyricsRes.status, errorData);
}
} catch (e) {
lyricsBody.innerHTML = `<span style="color: var(--red)">Error loading lyrics.</span>`;
}
// Fetch canvas
try {
const targetCanvasUrl = `/api/canvas?id=${id}&name=${encodeURIComponent(name)}&artist=${encodeURIComponent(artist)}`;
const canvasRes = await fetch(targetCanvasUrl);
if (canvasRes.ok) {
const data = await canvasRes.json();
if (data.url) {
canvasVideo.src = data.url;
canvasVideo.classList.remove('hidden');
canvasFallback.classList.add('hidden');
}
}
} catch (e) {
// Fallback remains visible
}
// Fetch stream
try {
const streamAudio = document.getElementById('streamAudio');
const streamFallback = document.getElementById('streamFallback');
const streamLinkContainer = document.getElementById('streamLinkContainer');
const streamLink = document.getElementById('streamLink');
streamAudio.style.display = 'none';
streamLinkContainer.style.display = 'none';
streamFallback.style.display = 'block';
streamFallback.textContent = "Checking audio cache...";
const targetStreamUrl = `/api/stream?id=${id}`;
const streamRes = await fetch(targetStreamUrl);
if (streamRes.ok) {
const data = await streamRes.json();
if (data.url) {
streamAudio.src = data.url;
streamAudio.style.display = 'block';
streamFallback.style.display = 'none';
streamLink.href = data.url;
streamLink.textContent = data.url;
streamLinkContainer.style.display = 'block';
}
} else {
streamFallback.textContent = "No Audio Cache";
}
} catch (e) {
document.getElementById('streamFallback').textContent = "Error loading stream";
}
}
// Search bar event listener
let searchTimeout;
searchBar.addEventListener('input', (e) => {
clearTimeout(searchTimeout);
const query = e.target.value.trim();
searchTimeout = setTimeout(async () => {
if (!query) {
renderSongs(allSongs);
return;
}
songsGrid.innerHTML = `<div style="grid-column: 1/-1; text-align: center; color: var(--cream-dim); font-family: var(--font-mono);">Searching fallback db...</div>`;
try {
const res = await fetch(`/api/search?q=${encodeURIComponent(query)}`);
if (res.ok) {
const results = await res.json();
renderSongs(results);
}
} catch (err) {
// Ignore
}
}, 300);
});
async function triggerSync() {
try {
const res = await fetch('/api/stream/sync', { method: 'POST' });
const data = await res.json();
showToast(data.message);
} catch (e) {
showToast("Failed to start sync");
}
}
function switchTab(tab) {
const tabApi = document.getElementById('tab-api');
const tabLogs = document.getElementById('tab-logs');
const consoleOutput = document.getElementById('consoleOutput');
const logsQueueOutput = document.getElementById('logsQueueOutput');
if (tab === 'api') {
tabApi.style.color = 'var(--amber)';
tabApi.style.borderBottom = '2px solid var(--amber)';
tabLogs.style.color = 'var(--cream-dim)';
tabLogs.style.borderBottom = 'none';
consoleOutput.classList.remove('hidden');
logsQueueOutput.classList.add('hidden');
} else {
tabLogs.style.color = 'var(--amber)';
tabLogs.style.borderBottom = '2px solid var(--amber)';
tabApi.style.color = 'var(--cream-dim)';
tabApi.style.borderBottom = 'none';
consoleOutput.classList.add('hidden');
logsQueueOutput.classList.remove('hidden');
}
}
async function loadStats() {
try {
const res = await fetch('/api/stats');
if (res.ok) {
const stats = await res.json();
document.getElementById('stats-lyrics').textContent = stats.lyrics;
document.getElementById('stats-canvas').textContent = stats.canvas;
if (stats.stream !== undefined) {
document.getElementById('stats-stream').textContent = stats.stream;
}
// Queue monitor values
if (stats.stats) {
document.getElementById('val-progressing').textContent = stats.stats.progressing;
document.getElementById('val-done').textContent = stats.stats.done;
document.getElementById('val-failed').textContent = stats.stats.failed;
}
if (stats.avg_uploads_per_min !== undefined) {
document.getElementById('val-rate').textContent = stats.avg_uploads_per_min.toFixed(1) + ' / min';
}
// Network stats
if (stats.network_stats) {
const dlSpeed = stats.network_stats.last_download_speed_kbps;
document.getElementById('val-dl-speed').textContent = dlSpeed > 1024
? (dlSpeed / 1024).toFixed(1) + ' MB/s'
: dlSpeed.toFixed(1) + ' KB/s';
const ulSpeed = stats.network_stats.last_upload_speed_kbps;
document.getElementById('val-ul-speed').textContent = ulSpeed > 1024
? (ulSpeed / 1024).toFixed(1) + ' MB/s'
: ulSpeed.toFixed(1) + ' KB/s';
document.getElementById('val-total-dl').textContent = stats.network_stats.total_downloaded_mb.toFixed(1) + ' MB';
document.getElementById('val-total-ul').textContent = stats.network_stats.total_uploaded_mb.toFixed(1) + ' MB';
}
// Logs
if (stats.logs) {
const liveLogsContainer = document.getElementById('liveLogsContainer');
liveLogsContainer.innerHTML = stats.logs.map(line => {
let color = 'var(--cream)';
if (line.includes('[ERROR]')) color = 'var(--red)';
else if (line.includes('[WARNING]')) color = 'var(--amber)';
return `<div style="color: ${color}; margin-bottom: 0.15rem; white-space: pre-wrap;">${line}</div>`;
}).join('');
}
// Jobs
if (stats.jobs) {
const activeJobsContainer = document.getElementById('activeJobsContainer');
const sortedJobs = Object.entries(stats.jobs).sort((a, b) => b[1].timestamp - a[1].timestamp);
if (sortedJobs.length === 0) {
activeJobsContainer.innerHTML = '<div style="color: var(--cream-dim); font-style: italic;">No active tasks</div>';
} else {
activeJobsContainer.innerHTML = sortedJobs.map(([id, job]) => {
let badgeColor = 'var(--amber)';
if (job.status === 'done') badgeColor = 'var(--cyan)';
if (job.status === 'failed') badgeColor = 'var(--red)';
return `<div style="display: flex; justify-content: space-between; margin-bottom: 0.2rem;">
<span style="word-break: break-all; margin-right: 0.5rem;">${id}</span>
<span style="color: ${badgeColor}; font-weight: bold;">${job.status}</span>
</div>`;
}).join('');
}
}
}
} catch (e) {
// Ignore
}
}
function showToast(msg) {
toastMsg.textContent = msg;
toast.classList.add('show');
setTimeout(() => {
toast.classList.remove('show');
}, 3000);
}
// Load initial songs & stats
loadFeaturedSongs();
loadStats();
setInterval(loadStats, 2000);
</script>
</body>
</html>
"""
return HTMLResponse(content=html_content)
|