File size: 2,982 Bytes
886a006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
    )