Spaces:
Sleeping
Sleeping
| import os | |
| from fastapi import FastAPI, HTTPException | |
| import yt_dlp | |
| from supabase import create_client, Client | |
| app = FastAPI() | |
| # Your Supabase Credentials | |
| SUPABASE_URL = "https://pfgmficmhmefvlotriuf.supabase.co" | |
| SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBmZ21maWNtaG1lZnZsb3RyaXVmIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Njk4OTg3MDgsImV4cCI6MjA4NTQ3NDcwOH0.V_u7LMG7LWOzvGOPFrZdUGgESIB9Y6clu7UCfF1Xng0" | |
| # Initialize Supabase | |
| try: | |
| supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) | |
| except Exception as e: | |
| print(f"CRITICAL ERROR: Could not connect to Supabase: {e}") | |
| def home(): | |
| return {"status": "Vortex TOT API is LIVE", "database": "Connected"} | |
| def extract_video_data(url: str): | |
| # 1. Check Supabase Cache First | |
| try: | |
| cached_data = supabase.table("video_cache").select("json_data").eq("video_url", url).execute() | |
| if cached_data.data: | |
| # If found, return instantly! | |
| return cached_data.data[0]['json_data'] | |
| except Exception as e: | |
| print(f"Cache check error: {e}") | |
| # 2. If not in cache, run yt-dlp | |
| ydl_opts = { | |
| 'quiet': True, | |
| 'no_warnings': True, | |
| 'skip_download': True, | |
| 'extract_flat': False, | |
| } | |
| try: | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| info = ydl.extract_info(url, download=False) | |
| # --- Logic for TikTok Slideshows --- | |
| if 'entries' in info and (info.get('extractor') == 'TikTok' or 'tiktok' in url.lower()): | |
| response_data = { | |
| "type": "slideshow", | |
| "title": info.get('title', 'TikTok Slideshow'), | |
| "images": [e.get('url') for e in info['entries'] if e.get('url')], | |
| "audio": info.get('url'), | |
| "source": "TikTok" | |
| } | |
| # --- Logic for Standard Video/Audio --- | |
| else: | |
| formats = [] | |
| for f in info.get('formats', []): | |
| if f.get('url'): | |
| formats.append({ | |
| "id": f.get('format_id'), | |
| "ext": f.get('ext'), | |
| "res": f.get('resolution'), | |
| "note": f.get('format_note'), | |
| "url": f.get('url') | |
| }) | |
| response_data = { | |
| "type": "video", | |
| "title": info.get('title'), | |
| "thumbnail": info.get('thumbnail'), | |
| "duration": info.get('duration'), | |
| "formats": formats, | |
| "source": info.get('extractor') | |
| } | |
| # 3. Save new result to Supabase | |
| try: | |
| supabase.table("video_cache").insert({ | |
| "video_url": url, | |
| "json_data": response_data | |
| }).execute() | |
| except Exception as e: | |
| print(f"Failed to cache: {e}") | |
| return response_data | |
| except Exception as e: | |
| raise HTTPException(status_code=400, detail=str(e)) |