"""Video-link helpers for the Gradio Space (yt-dlp). Copied from the Flask app (app.py) and stripped of the web framework: same host allow-list, short-link resolving, format choice and Burmese error messages. Limits are the Space's (3 min / 100 MB), not the Spain site's (10 min / 250 MB). """ import json import re import subprocess import sys import time from pathlib import Path from urllib.parse import urlparse import urllib.request BASE = Path(__file__).parent MAX_DURATION = 180 # seconds — ZeroGPU CPU is small; longer videos belong on the Spain site MAX_MB = 100 YTDLP = Path(sys.executable).parent / "yt-dlp" LINK_HOSTS = ("youtube.com", "youtu.be", "tiktok.com", "instagram.com", "facebook.com", "fb.watch", "xiaohongshu.com", "xhslink.com", "rednote.com", "twitter.com", "x.com", "vimeo.com", "dailymotion.com", "bilibili.com") # best mp4 up to 720p; the /b fallback covers sites with combined formats only LINK_FMT = "bv*[height<=720]+ba/b[height<=720]/b" SHORT_HOSTS = ("xhslink.com", "fb.watch", "vm.tiktok.com", "vt.tiktok.com") _MOBILE_UA = ("Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) " "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 " "Mobile/15E148 Safari/604.1") def _host_ok(host): host = (host or "").lower() return any(host == h or host.endswith("." + h) for h in LINK_HOSTS) def _link_log(url, stage, msg): """Keep link failures on disk so a bad link can be diagnosed later.""" try: line = f"{time.strftime('%F %T')} [{stage}] {url} :: {msg}\n" with open(BASE / "link_check.log", "a") as fh: fh.write(line.replace("\n", " ")[:600] + "\n") except OSError: pass def resolve_link(url): """Follow a share short-link to its real URL. RedNote's xhslink lands on rednote.com, which yt-dlp can't read and which bounces anonymous visitors to a login page. The same URL on xiaohongshu.com (with its xsec_token kept) works, so the host is rewritten after resolving. """ host = (urlparse(url).hostname or "").lower() if host not in SHORT_HOSTS: return url req = urllib.request.Request(url, headers={"User-Agent": _MOBILE_UA}) try: with urllib.request.urlopen(req, timeout=20) as r: final = r.geturl() except Exception: # noqa: BLE001 - fall back to the short link itself return url parts = urlparse(final) if (parts.hostname or "").lower() in ("rednote.com", "www.rednote.com"): final = parts._replace(netloc="www.xiaohongshu.com").geturl() return final def check_link(url): """Check a pasted link looks safe to hand to yt-dlp. Returns the url. Share buttons often copy a whole sentence with the link inside, so the first http(s) URL in the pasted text is what counts. """ text = (url or "").strip() m = re.search(r"https?://[^\s\"'<>]+", text) url = (m.group(0).rstrip(").,,。") if m else "") if len(url) > 2000 or not url: raise ValueError("link မှားနေတယ် — http/https link ပဲ ထည့်ပါ") parts = urlparse(url) host = (parts.hostname or "").lower() if host in ("rednote.com", "www.rednote.com"): url = parts._replace(netloc="www.xiaohongshu.com").geturl() host = "www.xiaohongshu.com" if not _host_ok(host): raise ValueError("ဒီ site က link လက်မခံဘူး — YouTube / TikTok / RedNote / " "Instagram / Facebook link ပဲ ရတယ်") url = resolve_link(url) if not _host_ok(urlparse(url).hostname): raise ValueError("ဒီ site က link လက်မခံဘူး — YouTube / TikTok / RedNote / " "Instagram / Facebook link ပဲ ရတယ်") return url def link_err(stderr): """Turn yt-dlp errors into short Burmese messages.""" s = (stderr or "").lower() if "xiaohongshu" in s or "rednote" in s: return ("RedNote link ကို server က ဖွင့်မရဘူး — app ထဲက Share → " "Copy link နဲ့ ရတဲ့ link ကို အပြည့်အစုံ ပြန် paste လုပ်ကြည့်ပါ") if ("private" in s or "login" in s or "cookies" in s or "sign in" in s or "log in" in s): return ("ဒီ video က private / login လိုတယ် — video file ကို " "တိုက်ရိုက် တင်ပေးပါ") if "no video" in s: return "ဒီ link မှာ video မတွေ့ဘူး" return "link က video ကို ဖတ်မရဘူး — link မှန်ရဲ့လား စစ်ပြီး ပြန်စမ်းပါ" def ytdlp_info(url): """Fetch link metadata with yt-dlp (no download).""" p = subprocess.run([str(YTDLP), "--no-playlist", "--no-warnings", "--impersonate", "chrome", "-J", url], capture_output=True, text=True, timeout=60) if p.returncode != 0: _link_log(url, "info", p.stderr.strip()[-300:]) raise ValueError(link_err(p.stderr)) try: return json.loads(p.stdout) except ValueError: raise ValueError("video အချက်အလက် ဖတ်မရဘူး") def fetch_link_info(url): info = ytdlp_info(url) if info.get("is_live"): raise ValueError("live video လက်မခံဘူး") dur = info.get("duration") or 0 if dur and dur > MAX_DURATION: raise ValueError("၃ မိနစ်ထက် ရှည်တဲ့ ဗီဒီယို လက်မခံဘူး (Space version)") return {"title": (info.get("title") or "")[:120], "duration": int(dur) if dur else None, "thumbnail": info.get("thumbnail") or "", "site": info.get("extractor_key") or info.get("extractor") or ""} def download_link(url, d): """Download a pasted link into dir d as input.. Returns ext.""" p = subprocess.run([str(YTDLP), "--no-playlist", "--no-warnings", "--impersonate", "chrome", "-f", LINK_FMT, "--merge-output-format", "mp4", "--max-filesize", f"{MAX_MB}M", "--socket-timeout", "20", "--retries", "3", "-o", str(d / "input.%(ext)s"), url], capture_output=True, text=True, timeout=600) got = [f for f in d.glob("input.*") if f.suffix.lower() in (".mp4", ".mov", ".mkv", ".webm", ".m4v")] if p.returncode != 0 or not got: for f in got: # drop partials from a failed download f.unlink(missing_ok=True) _link_log(url, "download", p.stderr.strip()[-300:]) raise ValueError(link_err(p.stderr)) return got[0].suffix.lower()