Update app.py
Browse files
app.py
CHANGED
|
@@ -1,228 +1,138 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
import asyncio
|
| 4 |
-
import random
|
| 5 |
-
import logging
|
| 6 |
-
from typing import List, Dict, Optional, Any
|
| 7 |
-
from playwright.async_api import async_playwright
|
| 8 |
-
import uvicorn
|
| 9 |
-
from pydantic import BaseModel
|
| 10 |
-
|
| 11 |
-
# Set up logging
|
| 12 |
-
logging.basicConfig(level=logging.INFO,
|
| 13 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 14 |
-
logger = logging.getLogger(__name__)
|
| 15 |
-
|
| 16 |
-
app = FastAPI(title="Kling Video Fetcher",
|
| 17 |
-
description="API to fetch random videos from Kling AI gallery")
|
| 18 |
-
|
| 19 |
-
# Model for the response
|
| 20 |
-
class VideoResponse(BaseModel):
|
| 21 |
-
url: str
|
| 22 |
-
thumbnail: Optional[str] = None
|
| 23 |
-
title: Optional[str] = None
|
| 24 |
-
status: str = "success"
|
| 25 |
-
|
| 26 |
-
# Cache to store recent video URLs
|
| 27 |
-
video_cache: List[Dict[str, Any]] = []
|
| 28 |
-
last_fetch_time = 0
|
| 29 |
-
|
| 30 |
-
async def fetch_random_video_from_kling() -> Dict[str, Any]:
|
| 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 |
-
page
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
logger.info(f"Clicking on element (attempt {attempt+1}/{max_attempts})...")
|
| 139 |
-
await selected_element.click()
|
| 140 |
-
await page.wait_for_timeout(3000)
|
| 141 |
-
|
| 142 |
-
# Look for video elements that appeared after clicking
|
| 143 |
-
post_click_videos = await page.query_selector_all("video")
|
| 144 |
-
|
| 145 |
-
if post_click_videos:
|
| 146 |
-
logger.info(f"Found {len(post_click_videos)} video elements after clicking!")
|
| 147 |
-
|
| 148 |
-
# Find a video with a source
|
| 149 |
-
for video in post_click_videos:
|
| 150 |
-
try:
|
| 151 |
-
video_src = await video.evaluate("el => el.src || ''")
|
| 152 |
-
poster_src = await video.evaluate("el => el.poster || ''")
|
| 153 |
-
|
| 154 |
-
if video_src:
|
| 155 |
-
# Try to get title from page
|
| 156 |
-
title = await page.title()
|
| 157 |
-
# Check if we're on a different page now
|
| 158 |
-
current_url = page.url
|
| 159 |
-
|
| 160 |
-
result = {
|
| 161 |
-
"url": video_src,
|
| 162 |
-
"thumbnail": poster_src if poster_src else None,
|
| 163 |
-
"title": title if title != "Kling AI" else None,
|
| 164 |
-
"page_url": current_url
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
await browser.close()
|
| 168 |
-
return result
|
| 169 |
-
except Exception as e:
|
| 170 |
-
logger.error(f"Error extracting video details: {e}")
|
| 171 |
-
continue
|
| 172 |
-
|
| 173 |
-
except Exception as e:
|
| 174 |
-
logger.error(f"Error with element attempt {attempt+1}: {e}")
|
| 175 |
-
continue
|
| 176 |
-
|
| 177 |
-
# If we get here, we couldn't find a video after multiple attempts
|
| 178 |
-
await browser.close()
|
| 179 |
-
raise HTTPException(status_code=404, detail="Could not find any video with a valid source")
|
| 180 |
-
|
| 181 |
-
except Exception as e:
|
| 182 |
-
logger.error(f"Unexpected error: {e}")
|
| 183 |
-
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 184 |
-
|
| 185 |
-
@app.get("/api/random-video", response_model=VideoResponse)
|
| 186 |
-
async def get_random_video():
|
| 187 |
-
"""
|
| 188 |
-
Endpoint to get a random video from Kling AI gallery.
|
| 189 |
-
Returns the video URL and metadata.
|
| 190 |
-
"""
|
| 191 |
-
try:
|
| 192 |
-
# Fetch a random video
|
| 193 |
-
video_info = await fetch_random_video_from_kling()
|
| 194 |
-
|
| 195 |
-
# Return the video information
|
| 196 |
-
return {
|
| 197 |
-
"url": video_info["url"],
|
| 198 |
-
"thumbnail": video_info.get("thumbnail"),
|
| 199 |
-
"title": video_info.get("title"),
|
| 200 |
-
"status": "success"
|
| 201 |
-
}
|
| 202 |
-
except HTTPException as e:
|
| 203 |
-
# Re-raise HTTP exceptions
|
| 204 |
-
raise e
|
| 205 |
-
except Exception as e:
|
| 206 |
-
# Log and convert other exceptions to HTTPException
|
| 207 |
-
logger.error(f"Error in endpoint: {e}")
|
| 208 |
-
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 209 |
-
|
| 210 |
-
@app.get("/")
|
| 211 |
-
async def root():
|
| 212 |
-
"""Root endpoint with API information"""
|
| 213 |
-
return {
|
| 214 |
-
"name": "Kling Video Fetcher API",
|
| 215 |
-
"version": "1.0",
|
| 216 |
-
"endpoints": {
|
| 217 |
-
"/api/random-video": "Get a random video URL from Kling AI gallery"
|
| 218 |
-
}
|
| 219 |
-
}
|
| 220 |
-
|
| 221 |
-
@app.get("/health")
|
| 222 |
-
async def health_check():
|
| 223 |
-
"""Health check endpoint"""
|
| 224 |
-
return {"status": "healthy"}
|
| 225 |
-
|
| 226 |
-
# Run the server
|
| 227 |
-
if __name__ == "__main__":
|
| 228 |
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import asyncio
|
| 4 |
+
import random
|
| 5 |
+
import logging
|
| 6 |
+
from typing import List, Dict, Optional, Any
|
| 7 |
+
from playwright.async_api import async_playwright
|
| 8 |
+
import uvicorn
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
|
| 11 |
+
# Set up logging
|
| 12 |
+
logging.basicConfig(level=logging.INFO,
|
| 13 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
app = FastAPI(title="Kling Video Fetcher",
|
| 17 |
+
description="API to fetch random videos from Kling AI gallery")
|
| 18 |
+
|
| 19 |
+
# Model for the response
|
| 20 |
+
class VideoResponse(BaseModel):
|
| 21 |
+
url: str
|
| 22 |
+
thumbnail: Optional[str] = None
|
| 23 |
+
title: Optional[str] = None
|
| 24 |
+
status: str = "success"
|
| 25 |
+
|
| 26 |
+
# Cache to store recent video URLs
|
| 27 |
+
video_cache: List[Dict[str, Any]] = []
|
| 28 |
+
last_fetch_time = 0
|
| 29 |
+
|
| 30 |
+
async def fetch_random_video_from_kling() -> Dict[str, Any]:
|
| 31 |
+
try:
|
| 32 |
+
async with async_playwright() as p:
|
| 33 |
+
browser = await p.chromium.launch(headless=True, args=[
|
| 34 |
+
'--no-sandbox',
|
| 35 |
+
'--disable-setuid-sandbox',
|
| 36 |
+
'--disable-dev-shm-usage',
|
| 37 |
+
'--disable-accelerated-2d-canvas',
|
| 38 |
+
'--no-first-run',
|
| 39 |
+
'--no-zygote',
|
| 40 |
+
'--disable-gpu',
|
| 41 |
+
'--disable-web-security',
|
| 42 |
+
])
|
| 43 |
+
context = await browser.new_context(viewport={'width': 1280, 'height': 800})
|
| 44 |
+
page = await context.new_page()
|
| 45 |
+
|
| 46 |
+
video_urls = []
|
| 47 |
+
|
| 48 |
+
# Intercept requests to capture video URLs
|
| 49 |
+
async def on_response(response):
|
| 50 |
+
try:
|
| 51 |
+
url = response.url
|
| 52 |
+
content_type = response.headers.get("content-type", "")
|
| 53 |
+
if ".mp4" in url or "video" in content_type:
|
| 54 |
+
video_urls.append(url)
|
| 55 |
+
except Exception as e:
|
| 56 |
+
logger.error(f"Error processing response: {e}")
|
| 57 |
+
|
| 58 |
+
page.on("response", on_response)
|
| 59 |
+
|
| 60 |
+
await page.goto("https://app.klingai.com/global/community/material", timeout=90000, wait_until="networkidle")
|
| 61 |
+
await page.wait_for_timeout(5000)
|
| 62 |
+
|
| 63 |
+
for _ in range(6):
|
| 64 |
+
await page.evaluate("window.scrollBy(0, 800)")
|
| 65 |
+
await page.wait_for_timeout(1500)
|
| 66 |
+
|
| 67 |
+
elements = await page.query_selector_all("video, [class*='video'], [class*='card'], [class*='media']")
|
| 68 |
+
elements = [e for e in elements if await e.is_visible()]
|
| 69 |
+
random.shuffle(elements)
|
| 70 |
+
|
| 71 |
+
for element in elements[:5]:
|
| 72 |
+
try:
|
| 73 |
+
await element.scroll_into_view_if_needed()
|
| 74 |
+
await page.wait_for_timeout(1000)
|
| 75 |
+
await element.click()
|
| 76 |
+
await page.wait_for_timeout(3000)
|
| 77 |
+
|
| 78 |
+
if video_urls:
|
| 79 |
+
video_url = video_urls[0]
|
| 80 |
+
title = await page.title()
|
| 81 |
+
return {
|
| 82 |
+
"url": video_url,
|
| 83 |
+
"thumbnail": None,
|
| 84 |
+
"title": title if title != "Kling AI" else None,
|
| 85 |
+
"status": "success"
|
| 86 |
+
}
|
| 87 |
+
except Exception as e:
|
| 88 |
+
logger.warning(f"Element interaction failed: {e}")
|
| 89 |
+
|
| 90 |
+
raise HTTPException(status_code=404, detail="Could not find a downloadable video")
|
| 91 |
+
except Exception as e:
|
| 92 |
+
logger.error(f"Unexpected error: {e}")
|
| 93 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 94 |
+
|
| 95 |
+
@app.get("/api/random-video", response_model=VideoResponse)
|
| 96 |
+
async def get_random_video():
|
| 97 |
+
"""
|
| 98 |
+
Endpoint to get a random video from Kling AI gallery.
|
| 99 |
+
Returns the video URL and metadata.
|
| 100 |
+
"""
|
| 101 |
+
try:
|
| 102 |
+
# Fetch a random video
|
| 103 |
+
video_info = await fetch_random_video_from_kling()
|
| 104 |
+
|
| 105 |
+
# Return the video information
|
| 106 |
+
return {
|
| 107 |
+
"url": video_info["url"],
|
| 108 |
+
"thumbnail": video_info.get("thumbnail"),
|
| 109 |
+
"title": video_info.get("title"),
|
| 110 |
+
"status": "success"
|
| 111 |
+
}
|
| 112 |
+
except HTTPException as e:
|
| 113 |
+
# Re-raise HTTP exceptions
|
| 114 |
+
raise e
|
| 115 |
+
except Exception as e:
|
| 116 |
+
# Log and convert other exceptions to HTTPException
|
| 117 |
+
logger.error(f"Error in endpoint: {e}")
|
| 118 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 119 |
+
|
| 120 |
+
@app.get("/")
|
| 121 |
+
async def root():
|
| 122 |
+
"""Root endpoint with API information"""
|
| 123 |
+
return {
|
| 124 |
+
"name": "Kling Video Fetcher API",
|
| 125 |
+
"version": "1.0",
|
| 126 |
+
"endpoints": {
|
| 127 |
+
"/api/random-video": "Get a random video URL from Kling AI gallery"
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
@app.get("/health")
|
| 132 |
+
async def health_check():
|
| 133 |
+
"""Health check endpoint"""
|
| 134 |
+
return {"status": "healthy"}
|
| 135 |
+
|
| 136 |
+
# Run the server
|
| 137 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|