#!/usr/bin/env python3 """ dl.py Single-file Flask API that accepts a playmogo.com URL and returns the resolved direct download URL as JSON. Uses yt-dlp's own webpage fetch + Cloudflare impersonation (invoked as the real `yt-dlp` console script, not `python -m yt_dlp`) to bypass Cloudflare and extract the .mp4 link. Requires: yt-dlp installed and on PATH, plus flask. pip install flask --break-system-packages pip install yt-dlp --break-system-packages Run: python3 dl.py Usage: GET /resolve?url=https://playmogo.com/d/wl5smm5jtwtl """ import os import re import subprocess import tempfile import time from urllib.parse import urlparse, quote import requests from flask import Flask, request, jsonify, Response, stream_with_context app = Flask(__name__) PROXY_BASE_URL = "https://pycra-cras.hf.space" FINAL_LINK_RE = re.compile(r'href="(https?://[^"]+\.mp4\?[^"]*)"') DOWNLOAD_PAGE_RE = re.compile(r'href="(/download/[^"]+)"') # retry tuning MAX_RETRIES = 5 RETRY_BACKOFF_SECONDS = 1.5 # multiplied by attempt number def fetch_page_via_ytdlp(url: str) -> str: """Use yt-dlp --write-pages to fetch a page's HTML, bypassing Cloudflare.""" with tempfile.TemporaryDirectory() as tmpdir: subprocess.run( [ "yt-dlp", "-v", "--extractor-args", "generic:impersonate", "--write-pages", url, ], cwd=tmpdir, capture_output=True, text=True, ) dumps = [f for f in os.listdir(tmpdir) if f.endswith(".dump")] if not dumps: raise RuntimeError(f"yt-dlp produced no page dump for {url}") # most recent dump (webpage, not some secondary request) dumps.sort(key=lambda f: os.path.getmtime(os.path.join(tmpdir, f))) with open(os.path.join(tmpdir, dumps[-1]), "r", errors="ignore") as f: return f.read() def _resolve_once(playmogo_url: str) -> str: """Single attempt at the full resolve flow. Raises RuntimeError on failure.""" page_html = fetch_page_via_ytdlp(playmogo_url) match = DOWNLOAD_PAGE_RE.search(page_html) if not match: raise RuntimeError("Could not find /download/ link on the initial page.") download_url = f"https://playmogo.com{match.group(1)}" dl_html = fetch_page_via_ytdlp(download_url) match = FINAL_LINK_RE.search(dl_html) if not match: raise RuntimeError("Could not find final .mp4 link on the download page.") return match.group(1) def resolve(playmogo_url: str) -> str: """ Runs _resolve_once with retries, since Cloudflare impersonation and the upstream page occasionally fail transiently (challenge page instead of real content, dropped connection, etc.) and succeed again immediately on the next attempt. """ last_error = None for attempt in range(1, MAX_RETRIES + 1): try: return _resolve_once(playmogo_url) except subprocess.TimeoutExpired: raise # not worth retrying, let the caller handle it as-is except RuntimeError as e: last_error = e if attempt < MAX_RETRIES: time.sleep(RETRY_BACKOFF_SECONDS * attempt) continue raise RuntimeError( f"Failed after {MAX_RETRIES} attempts: {last_error}" ) @app.route("/resolve", methods=["GET"]) def resolve_endpoint(): url = request.args.get("url", "").strip() if not url: return jsonify({"error": "Missing 'url' query parameter"}), 400 parsed = urlparse(url) if parsed.scheme not in ("http", "https"): return jsonify({"error": "Invalid url"}), 400 try: download_url = resolve(url) except subprocess.TimeoutExpired: return jsonify({"error": "Upstream request timed out"}), 504 except Exception as e: return jsonify({"error": str(e)}), 502 proxy_dl_url = f"{PROXY_BASE_URL}/download?url={quote(download_url, safe='')}" return jsonify({ "source_url": url, "download_url": download_url, "proxy_dl_url": proxy_dl_url, }) @app.route("/download", methods=["GET"]) def download_proxy(): """ Proxies the actual file bytes back to the caller. Needed because the resolved CDN link is bound to the IP that requested it — so the server that resolved the link must also be the one downloading it. Usage: /download?url= """ url = request.args.get("url", "").strip() if not url: return jsonify({"error": "Missing 'url' query parameter"}), 400 parsed = urlparse(url) if parsed.scheme not in ("http", "https"): return jsonify({"error": "Invalid url"}), 400 headers = { "User-Agent": ( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/120.0 Safari/537.36" ), "Referer": "https://playmogo.com/", } # forward Range header so clients can resume/seek if "Range" in request.headers: headers["Range"] = request.headers["Range"] try: upstream = requests.get(url, headers=headers, stream=True, timeout=30, allow_redirects=True) except requests.RequestException as e: return jsonify({"error": f"Upstream request failed: {e}"}), 502 passthrough_headers = {} for h in ("Content-Type", "Content-Length", "Content-Disposition", "Accept-Ranges", "Content-Range"): if h in upstream.headers: passthrough_headers[h] = upstream.headers[h] return Response( stream_with_context(upstream.iter_content(chunk_size=1024 * 256)), status=upstream.status_code, headers=passthrough_headers, ) @app.route("/health", methods=["GET"]) def health(): return jsonify({"status": "ok"}) if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)