import httpx from fastapi import FastAPI, Form from pydantic import BaseModel import asyncio app = FastAPI(title="Still Frame Compilation API | Badal Special") # Tumhara apna OptiPix API endpoint OPTIPIX_API = "https://bk939448-image-optimizer-api.hf.space/upload-poster" class ProcessResponse(BaseModel): title_id: str total_found: int processed_urls: list @app.post("/get-top-shots", response_model=ProcessResponse) async def get_top_shots( title_id: str = Form(..., description="IMDb Title ID (e.g., tt12844910)"), top_shots: int = Form(5, description="Maximum screenshots to process"), level: str = Form("extreme", description="Compression level for OptiPix") ): imdb_api_url = f"https://api.imdbapi.dev/titles/{title_id}/images" # Hum direct API se bhi 'still_frame' filter karwa sakte hain jisse data kam aaye params = {"types": "still_frame"} valid_stills = [] async with httpx.AsyncClient(timeout=30.0) as client: # STEP 1 & 2: Pagination scan from start to end while True: response = await client.get(imdb_api_url, params=params) if response.status_code != 200: break data = response.json() images = data.get("images", []) # STEP 3: Filter for YouTube Thumbnail size (Width > Height) for img in images: width = img.get("width", 0) height = img.get("height", 0) if width > height: valid_stills.append({ "url": img.get("url"), "width": width, "height": height }) # Check for next page next_token = data.get("nextPageToken") if not next_token: break # Agar token nahi hai toh ant tak scan ho gaya params["pageToken"] = next_token # STEP 4: Sort by Highest Resolution (Width) aur limit lagana valid_stills.sort(key=lambda x: x["width"], reverse=True) top_selected = valid_stills[:top_shots] # STEP 5: OptiPix API par ek sath URLs bhejna processed_urls = [] for still in top_selected: form_data = { "level": level, "url": still["url"] } try: opti_res = await client.post(OPTIPIX_API, data=form_data) opti_data = opti_res.json() if opti_data.get("success"): processed_urls.append(opti_data.get("url")) except Exception as e: print(f"Bhai OptiPix API fail ho gayi is URL ke liye: {still['url']} - Error: {e}") return ProcessResponse( title_id=title_id, total_found=len(valid_stills), processed_urls=processed_urls )