Update app.py
Browse files4 unmodified lines567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101114 unmodified linesapp = Flask(__name__)# --- UI CODE ---HTML_CODE = """<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Universal Video Downloader</title><script src="https://cdn.tailwindcss.com"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet"><style>body { font-family: sans-serif; background-color: #0f172a; color: white; }.glass { background: rgba(30, 41, 59, 0.8); backdrop-filter: blur(12px); border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; }.loader { border: 3px solid #f3f3f3; border-top: 3px solid #3498db; border-radius: 50%; width: 20px; height: 20px; animation: spin 1s linear infinite; display: none; }@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }</style></head><body class="flex items-center justify-center min-h-screen p-4"><div class="glass w-full max-w-md p-6 shadow-2xl"><h1 class="text-3xl font-bold text-center mb-6 text-blue-400">UniLoader <span class="text-xs text-gray-400">v2.0</span></h1><form method="POST" action="/fetch" onsubmit="document.getElementById('spinner').style.display='inline-block'" class="space-y-4"><input type="text" name="url" class="w-full bg-gray-900 border border-gray-700 rounded-lg p-3 focus:outline-none focus:border-blue-500" placeholder="Paste Link (YouTube/TikTok)..." required><button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 p-3 rounded-lg font-bold flex justify-center items-center gap-2"><span>Search</span> <div class="loader" id="spinner"></div></button></form>{% if formats %}<div class="mt-6 space-y-2"><p class="text-xs text-gray-400 text-center uppercase">Select Quality</p>{% for f in formats %}<form method="POST" action="/download"><input type="hidden" name="url" value="{{ url }}"><input type="hidden" name="format_id" value="{{ f.id }}"><button type="submit" class="w-full bg-gray-800 hover:bg-gray-700 p-3 rounded-lg flex justify-between items-center border border-gray-700"><span class="font-bold">{{ f.label }}</span><span class="text-xs text-gray-400">{{ f.ext }}</span></button></form>{% endfor %}</div>{% endif %}{% if error %}<div class="mt-4 p-3 bg-red-900/50 text-red-200 text-sm rounded border border-red-500 text-center">{{ error }}</div>{% endif %}</div></body></html>"""@app.route("/", methods=["GET"])def index():return render_template_string(HTML_CODE)@app.route("/fetch", methods=["POST"])def fetch():url = request.form.get("url")try:# Latest yt-dlp needs minimal configopts = {'quiet': True,'no_warnings': True,'nocheckcertificate': True,}with yt_dlp.YoutubeDL(opts) as ydl:info = ydl.extract_info(url, download=False)formats = []seen = set()for f in info.get('formats', []):if f.get('vcodec') != 'none':h = f.get('height', 0)if h:label = f"{h}p"if h >= 1080: label += " HD"if label not in seen:formats.append({'id': f['format_id'], 'label': label, 'ext': f['ext']})seen.add(label)formats.sort(key=lambda x: int(x['label'].split('p')[0]), reverse=True)return render_template_string(HTML_CODE, formats=formats, url=url)except Exception as e:return render_template_string(HTML_CODE, error=str(e))@app.route("/download", methods=["POST"])def dl():url = request.form.get("url")fid = request.form.get("format_id")name = f"Vid_{uuid.uuid4().hex[:5]}.mp4"try:opts = {'format': f'{fid}+bestaudio/best', 'outtmpl': name, 'merge_output_format': 'mp4'}with yt_dlp.YoutubeDL(opts) as ydl:ydl.download([url])@after_this_requestdef cln(r):if os.path.exists(name): os.remove(name)return rreturn send_file(name, as_attachment=True)except Exception as e:return f"Error: {str(e)}"if __name__ == "__main__":app.run(host='0.0.0.0', port=7860)4 unmodified lines567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161174 unmodified linesapp = Flask(__name__)# ---------------- HTML UI ----------------HTML_CODE = """<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Universal Video Downloader</title><script src="https://cdn.tailwindcss.com"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet"><style>body { background:#0f172a; color:white; font-family:sans-serif; }.glass { background:rgba(30,41,59,.85); backdrop-filter:blur(12px);border-radius:16px; border:1px solid rgba(255,255,255,.1); }.loader { border:3px solid #f3f3f3; border-top:3px solid #3498db;border-radius:50%; width:18px; height:18px;animation:spin 1s linear infinite; display:none; }@keyframes spin {100%{transform:rotate(360deg)}}</style></head><body class="flex items-center justify-center min-h-screen p-4"><div class="glass w-full max-w-md p-6"><h1 class="text-3xl text-center font-bold text-blue-400 mb-6">UniLoader <span class="text-xs text-gray-400">HF Edition</span></h1><form method="POST" action="/download"onsubmit="document.getElementById('sp').style.display='inline-block'"class="space-y-4"><input name="url" requiredclass="w-full bg-gray-900 border border-gray-700 p-3 rounded-lg"placeholder="Paste YouTube / TikTok link"><button class="w-full bg-blue-600 hover:bg-blue-700 p-3 rounded-lgfont-bold flex items-center justify-center gap-2">Download Best Quality<div id="sp" class="loader"></div></button></form>{% if error %}<div class="mt-4 bg-red-900/50 p-3 rounded text-center text-sm">{{ error }}</div>{% endif %}<p class="text-xs text-gray-400 mt-4 text-center">Note: TikTok HD on Hugging Face is limited by platform.</p></div></body></html>"""# ---------------- ROUTES ----------------@app.route("/", methods=["GET"])def index():return render_template_string(HTML_CODE)@app.route("/download", methods=["POST"])def download():url = request.form.get("url")filename = f"video_{uuid.uuid4().hex[:6]}.mp4"try:ydl_opts = {# 🔥 BEST POSSIBLE FORMAT (HF SAFE)"format": ("bv*[ext=mp4][height<=720]/""bv*[height<=720]/""best[height<=720]/best"),"outtmpl": filename,"merge_output_format": "mp4",# 🔥 Browser-like headers (slight quality improvement)"http_headers": {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)","Accept-Language": "en-US,en;q=0.9",},"quiet": True,"no_warnings": True,"nocheckcertificate": True,}with yt_dlp.YoutubeDL(ydl_opts) as ydl:ydl.download([url])@after_this_requestdef cleanup(resp):try:if os.path.exists(filename):os.remove(filename)except:passreturn respreturn send_file(filename, as_attachment=True)except Exception as e:return render_template_string(HTML_CODE, error=str(e))# ---------------- RUN ----------------if __name__ == "__main__":app.run(host="0.0.0.0", port=7860)
app.py+87-81