Upload app.py
Browse files7 unmodified lines89101112131454 unmodified lines6970717273747576777879804 unmodified lines858687888990919293947 unmodified lines# --- DNS BYPASS PATCH START ---BYPASS_DOMAINS = ["youtube.com", "googlevideo.com", "youtu.be","vimeocdn.com","dailymotion.com", "dmcdn.net","twitch.tv", "ttvnw.net","facebook.com", "fbcdn.net",54 unmodified linesreturn jsonify({"error": "URL is missing"}), 400url = data['url']try:ydl_opts = {'quiet': True, 'noplaylist': True, 'skip_download': True, 'simulate': True, 'force_formats': True, 'cache_dir': '/tmp/.yt-dlp-cache', 'force_ipv4': True}with yt_dlp.YoutubeDL(ydl_opts) as ydl:info = ydl.extract_info(url, download=False)formats = info.get('formats', [])best = next((f for f in formats if f.get('ext') == 'mp4' and f.get('acodec') != 'none'), formats[-1] if formats else None)return jsonify({"status": "ok", "download_url": best.get('url') if best else None, "title": info.get('title'), "thumbnail": info.get('thumbnail')})except Exception as e:return jsonify({"error": str(e)}), 5004 unmodified linesreturn jsonify({"error": "M3U8 URL is missing"}), 400m3u8_url = data['url']try:ydl_opts = {'quiet': True, 'skip_download': True, 'force_ipv4': True, 'format': 'best'}with yt_dlp.YoutubeDL(ydl_opts) as ydl:info = ydl.extract_info(m3u8_url, download=False)return jsonify({"status": "ok", "download_url": info.get('url'), "title": info.get('title', 'HLS Stream'), "protocol": info.get('protocol'), "ext": info.get('ext')})except Exception as e:return jsonify({"error": str(e)}), 5007 unmodified lines89101112131454 unmodified lines697071727374757677787980818283848586878889909192939495969798991001011021034 unmodified lines1081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401417 unmodified lines# --- DNS BYPASS PATCH START ---BYPASS_DOMAINS = ["youtube.com", "googlevideo.com", "youtu.be","vimeo.com", "vimeocdn.com","dailymotion.com", "dmcdn.net","twitch.tv", "ttvnw.net","facebook.com", "fbcdn.net",54 unmodified linesreturn jsonify({"error": "URL is missing"}), 400url = data['url']try:# Improved format selection for better qualityydl_opts = {'quiet': True,'noplaylist': True,'skip_download': True,'simulate': True,'force_formats': True,'cache_dir': '/tmp/.yt-dlp-cache','force_ipv4': True,'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best'}with yt_dlp.YoutubeDL(ydl_opts) as ydl:info = ydl.extract_info(url, download=False)# yt-dlp with the above format string will put the best direct link in 'url'# but we also provide a fallback check in formatsdownload_url = info.get('url')if not download_url and info.get('formats'):# Fallback to manual selection if 'url' is missingformats = info.get('formats', [])best = next((f for f in reversed(formats) if f.get('ext') == 'mp4' and f.get('acodec') != 'none'), formats[-1])download_url = best.get('url')return jsonify({"status": "ok","download_url": download_url,"title": info.get('title'),"thumbnail": info.get('thumbnail')})except Exception as e:return jsonify({"error": str(e)}), 5004 unmodified linesreturn jsonify({"error": "M3U8 URL is missing"}), 400m3u8_url = data['url']try:# For HLS, we want to find the best quality variant manifestydl_opts = {'quiet': True,'skip_download': True,'force_ipv4': True,'format': 'best'}with yt_dlp.YoutubeDL(ydl_opts) as ydl:info = ydl.extract_info(m3u8_url, download=False)# If it's a master manifest, yt-dlp extracts sub-manifests into 'formats'formats = info.get('formats', [])if formats:# The formats list is usually sorted by quality (worst to best)best_format = formats[-1]download_url = best_format.get('url')else:# Direct stream or single-variant manifestdownload_url = info.get('url')return jsonify({"status": "ok","download_url": download_url,"title": info.get('title', 'HLS Stream'),"protocol": info.get('protocol'),"ext": info.get('ext'),"is_variant": len(formats) > 0})except Exception as e:return jsonify({"error": str(e)}), 500
app.py+54-7