Spaces:
Configuration error
Configuration error
Download app.py from Rudra3779/grably-api: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/Rudra3779/grably-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/Rudra3779/grably-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Rudra3779/grably-api/resolve/main/app.py
3.1 kB
| import os | |
| import tempfile | |
| import uuid | |
| import glob | |
| from fastapi import FastAPI, HTTPException | |
| import gradio as gr | |
| import yt_dlp | |
| import uvicorn | |
| app = FastAPI() | |
| # Write cookies on startup | |
| if os.getenv("YT_COOKIES"): | |
| with open("/tmp/cookies.txt", "w") as f: | |
| f.write(os.getenv("YT_COOKIES")) | |
| def extract_media(url: str, mode: str = "proxy"): | |
| cookies_path = "/tmp/cookies.txt" | |
| has_cookies = os.path.exists(cookies_path) | |
| # Common options | |
| ydl_opts = { | |
| 'quiet': True, | |
| 'no_warnings': True, | |
| } | |
| if has_cookies: | |
| ydl_opts['cookiefile'] = cookies_path | |
| try: | |
| if mode == "direct": | |
| ydl_opts['extractor_args'] = {'youtube': {'player_client': ['android']}} | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| info = ydl.extract_info(url, download=False) | |
| return { | |
| "success": True, | |
| "title": info.get("title", "Untitled"), | |
| "thumbnail": info.get("thumbnail"), | |
| "duration": info.get("duration"), | |
| "download_url": info.get("url"), | |
| "is_proxied": False | |
| } | |
| elif mode == "proxy": | |
| temp_dir = os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) | |
| os.makedirs(temp_dir, exist_ok=True) | |
| ydl_opts.update({ | |
| 'format': 'bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', | |
| 'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'), | |
| 'merge_output_format': 'mp4', | |
| 'extractor_args': {'youtube': {'player_client': ['android']}} | |
| }) | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| info = ydl.extract_info(url, download=True) | |
| # Find the downloaded mp4 file | |
| downloaded_files = glob.glob(os.path.join(temp_dir, "*.mp4")) | |
| if not downloaded_files: | |
| raise Exception("Could not locate the downloaded mp4 file.") | |
| final_file = downloaded_files[0] | |
| space_host = os.getenv("SPACE_HOST", "") | |
| if space_host: | |
| download_url = f"https://{space_host}/file={final_file}" | |
| else: | |
| download_url = f"/file={final_file}" | |
| return { | |
| "success": True, | |
| "title": info.get("title", "Untitled"), | |
| "thumbnail": info.get("thumbnail"), | |
| "duration": info.get("duration"), | |
| "download_url": download_url, | |
| "is_proxied": True | |
| } | |
| else: | |
| raise HTTPException(status_code=400, detail="Invalid mode. Must be 'direct' or 'proxy'.") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# Grably API Backend") | |
| app = gr.mount_gradio_app(app, demo, path="/") | |