File size: 5,900 Bytes
34af23e 93e6bb9 34af23e | 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 | #!/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=<resolved cdn 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)
|