Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import os
|
2 |
-
from fastapi import FastAPI, HTTPException, UploadFile, Depends
|
3 |
from fastapi.security import APIKeyHeader
|
4 |
import aiohttp
|
5 |
import asyncio
|
@@ -48,7 +48,7 @@ def get_random_user_agent() -> str:
|
|
48 |
]
|
49 |
return random.choice(user_agents)
|
50 |
|
51 |
-
async def search_and_get_videos(query: str) -> List[Dict]:
|
52 |
for instance in INVIDIOUS_INSTANCES:
|
53 |
url = f"{instance}/api/v1/search?q={query}&type=video"
|
54 |
try:
|
@@ -65,7 +65,7 @@ async def search_and_get_videos(query: str) -> List[Dict]:
|
|
65 |
else "",
|
66 |
}
|
67 |
for video in search_results
|
68 |
-
][:
|
69 |
return videos
|
70 |
except aiohttp.ClientError as e:
|
71 |
logger.error(f"Error performing video search on {instance}: {e}")
|
@@ -162,8 +162,12 @@ async def verify_api_key(api_key: str = Depends(api_key_header)):
|
|
162 |
return api_key
|
163 |
|
164 |
@app.get("/search-videos/")
|
165 |
-
async def search_videos(
|
166 |
-
|
|
|
|
|
|
|
|
|
167 |
if not videos:
|
168 |
raise HTTPException(status_code=404, detail="No videos found or an error occurred during the search.")
|
169 |
return {"videos": videos}
|
|
|
1 |
import os
|
2 |
+
from fastapi import FastAPI, HTTPException, UploadFile, Depends, Query
|
3 |
from fastapi.security import APIKeyHeader
|
4 |
import aiohttp
|
5 |
import asyncio
|
|
|
48 |
]
|
49 |
return random.choice(user_agents)
|
50 |
|
51 |
+
async def search_and_get_videos(query: str, num_videos: int = 2) -> List[Dict]:
|
52 |
for instance in INVIDIOUS_INSTANCES:
|
53 |
url = f"{instance}/api/v1/search?q={query}&type=video"
|
54 |
try:
|
|
|
65 |
else "",
|
66 |
}
|
67 |
for video in search_results
|
68 |
+
][:num_videos]
|
69 |
return videos
|
70 |
except aiohttp.ClientError as e:
|
71 |
logger.error(f"Error performing video search on {instance}: {e}")
|
|
|
162 |
return api_key
|
163 |
|
164 |
@app.get("/search-videos/")
|
165 |
+
async def search_videos(
|
166 |
+
query: str,
|
167 |
+
num_videos: int = Query(default=2, ge=1, le=10),
|
168 |
+
api_key: str = Depends(verify_api_key)
|
169 |
+
):
|
170 |
+
videos = await search_and_get_videos(query, num_videos)
|
171 |
if not videos:
|
172 |
raise HTTPException(status_code=404, detail="No videos found or an error occurred during the search.")
|
173 |
return {"videos": videos}
|