Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ import boto3
|
|
| 12 |
from botocore.exceptions import NoCredentialsError
|
| 13 |
from duckduckgo_search import DDGS
|
| 14 |
from fp.fp import FreeProxy
|
| 15 |
-
import requests_random_user_agent
|
| 16 |
|
| 17 |
app = FastAPI()
|
| 18 |
|
|
@@ -31,7 +30,7 @@ INVIDIOUS_INSTANCES = [
|
|
| 31 |
"https://invidious.adminforge.de"
|
| 32 |
]
|
| 33 |
API_KEY = os.environ.get("API_KEY")
|
| 34 |
-
TIMEOUT =
|
| 35 |
|
| 36 |
# S3 Configuration
|
| 37 |
S3_ACCESS_KEY_ID = os.environ.get("S3_ACCESS_KEY_ID")
|
|
@@ -44,7 +43,7 @@ if not all([API_KEY, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_BUCKET, S3_REGIO
|
|
| 44 |
|
| 45 |
def get_proxy():
|
| 46 |
return FreeProxy(
|
| 47 |
-
timeout=
|
| 48 |
rand=True,
|
| 49 |
https=True
|
| 50 |
).get()
|
|
@@ -54,7 +53,7 @@ async def search_and_get_videos(query: str, num_videos: int = 2) -> List[Dict]:
|
|
| 54 |
url = f"{instance}/api/v1/search?q={query}&type=video"
|
| 55 |
try:
|
| 56 |
async with aiohttp.ClientSession() as session:
|
| 57 |
-
async with session.get(url
|
| 58 |
response.raise_for_status()
|
| 59 |
search_results = await response.json()
|
| 60 |
videos = [
|
|
@@ -80,7 +79,7 @@ async def get_youtube_audio(video_id: str, max_retries: int = 3) -> Dict:
|
|
| 80 |
url = f"{instance}/api/v1/videos/{video_id}"
|
| 81 |
|
| 82 |
async with aiohttp.ClientSession() as session:
|
| 83 |
-
async with session.get(url
|
| 84 |
response.raise_for_status()
|
| 85 |
video_data = await response.json()
|
| 86 |
|
|
@@ -91,7 +90,7 @@ async def get_youtube_audio(video_id: str, max_retries: int = 3) -> Dict:
|
|
| 91 |
audio_url = audio_format.get('url')
|
| 92 |
if audio_url:
|
| 93 |
try:
|
| 94 |
-
async with session.get(audio_url
|
| 95 |
audio_content = await audio_response.read()
|
| 96 |
|
| 97 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.m4a') as temp_file:
|
|
@@ -163,7 +162,7 @@ def web_search(query: str, num_results: int = 5) -> dict:
|
|
| 163 |
with DDGS(
|
| 164 |
proxy=proxy,
|
| 165 |
timeout=TIMEOUT,
|
| 166 |
-
headers={'User-Agent':
|
| 167 |
) as ddgs:
|
| 168 |
results = list(ddgs.text(query, max_results=num_results))
|
| 169 |
formatted_results = [
|
|
@@ -191,7 +190,7 @@ def image_search(query: str, num_results: int = 5) -> dict:
|
|
| 191 |
with DDGS(
|
| 192 |
proxy=proxy,
|
| 193 |
timeout=TIMEOUT,
|
| 194 |
-
headers={'User-Agent':
|
| 195 |
) as ddgs:
|
| 196 |
results = list(ddgs.images(query, max_results=num_results))
|
| 197 |
formatted_results = [
|
|
|
|
| 12 |
from botocore.exceptions import NoCredentialsError
|
| 13 |
from duckduckgo_search import DDGS
|
| 14 |
from fp.fp import FreeProxy
|
|
|
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
|
|
| 30 |
"https://invidious.adminforge.de"
|
| 31 |
]
|
| 32 |
API_KEY = os.environ.get("API_KEY")
|
| 33 |
+
TIMEOUT = 20 # Timeout for requests in seconds
|
| 34 |
|
| 35 |
# S3 Configuration
|
| 36 |
S3_ACCESS_KEY_ID = os.environ.get("S3_ACCESS_KEY_ID")
|
|
|
|
| 43 |
|
| 44 |
def get_proxy():
|
| 45 |
return FreeProxy(
|
| 46 |
+
timeout=0.9, # Set timeout to 0.9 seconds
|
| 47 |
rand=True,
|
| 48 |
https=True
|
| 49 |
).get()
|
|
|
|
| 53 |
url = f"{instance}/api/v1/search?q={query}&type=video"
|
| 54 |
try:
|
| 55 |
async with aiohttp.ClientSession() as session:
|
| 56 |
+
async with session.get(url) as response:
|
| 57 |
response.raise_for_status()
|
| 58 |
search_results = await response.json()
|
| 59 |
videos = [
|
|
|
|
| 79 |
url = f"{instance}/api/v1/videos/{video_id}"
|
| 80 |
|
| 81 |
async with aiohttp.ClientSession() as session:
|
| 82 |
+
async with session.get(url) as response:
|
| 83 |
response.raise_for_status()
|
| 84 |
video_data = await response.json()
|
| 85 |
|
|
|
|
| 90 |
audio_url = audio_format.get('url')
|
| 91 |
if audio_url:
|
| 92 |
try:
|
| 93 |
+
async with session.get(audio_url) as audio_response:
|
| 94 |
audio_content = await audio_response.read()
|
| 95 |
|
| 96 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.m4a') as temp_file:
|
|
|
|
| 162 |
with DDGS(
|
| 163 |
proxy=proxy,
|
| 164 |
timeout=TIMEOUT,
|
| 165 |
+
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
|
| 166 |
) as ddgs:
|
| 167 |
results = list(ddgs.text(query, max_results=num_results))
|
| 168 |
formatted_results = [
|
|
|
|
| 190 |
with DDGS(
|
| 191 |
proxy=proxy,
|
| 192 |
timeout=TIMEOUT,
|
| 193 |
+
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
|
| 194 |
) as ddgs:
|
| 195 |
results = list(ddgs.images(query, max_results=num_results))
|
| 196 |
formatted_results = [
|