Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# main.py (Correcting SyntaxError at line
|
2 |
import os
|
3 |
import re
|
4 |
import logging
|
@@ -132,7 +132,7 @@ async def get_transcript_via_supadata(video_id: str, api_key: str) -> Optional[s
|
|
132 |
if not video_id: logger.error("[Supadata] No video_id provided"); return None
|
133 |
if not api_key: logger.error("[Supadata] API key missing."); return None
|
134 |
logger.info(f"[Supadata] Attempting fetch for video ID: {video_id}")
|
135 |
-
api_endpoint = "https://api.supadata.ai/v1/youtube/transcript"
|
136 |
params = {"videoId": video_id, "format": "text"}; headers = {"X-API-Key": api_key}
|
137 |
try:
|
138 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
@@ -153,22 +153,22 @@ async def get_transcript_via_supadata(video_id: str, api_key: str) -> Optional[s
|
|
153 |
else: logger.error(f"[Supadata] Unexpected status {response.status_code} for {video_id}. Resp: {response.text[:200]}"); return None
|
154 |
except httpx.TimeoutException: logger.error(f"[Supadata] Timeout connecting for {video_id}"); return None
|
155 |
except httpx.RequestError as e:
|
156 |
-
if "CERTIFICATE_VERIFY_FAILED" in str(e): logger.error(f"[Supadata] SSL Cert Verify Failed for {video_id}: {e}")
|
157 |
else: logger.error(f"[Supadata] Request error for {video_id}: {e}")
|
158 |
return None
|
159 |
except Exception as e: logger.error(f"[Supadata] Unexpected error for {video_id}: {e}", exc_info=True); return None
|
160 |
|
161 |
async def get_transcript_via_apify(video_url: str, api_token: str) -> Optional[str]:
|
162 |
"""Fetches YouTube transcript using Apify REST API (run-sync-get-dataset-items endpoint)."""
|
163 |
-
global APIFY_ACTOR_ID
|
164 |
if not video_url: logger.error("[Apify SyncItems] No video_url provided"); return None
|
165 |
if not api_token: logger.error("[Apify SyncItems] API token missing."); return None
|
166 |
logger.info(f"[Apify SyncItems] Attempting fetch for URL: {video_url} (Actor: {APIFY_ACTOR_ID})")
|
167 |
|
168 |
sync_items_endpoint = f"https://api.apify.com/v2/acts/{APIFY_ACTOR_ID}/run-sync-get-dataset-items"
|
169 |
-
params = {"token": api_token}
|
170 |
payload = { "urls": [video_url], "outputFormat": "singleStringText", "maxRetries": 5, "channelHandleBoolean": False, "channelNameBoolean": False, "datePublishedBoolean": False, "relativeDateTextBoolean": False, }
|
171 |
-
headers = {"Content-Type": "application/json"}
|
172 |
|
173 |
try:
|
174 |
async with httpx.AsyncClient(timeout=120.0) as client:
|
@@ -198,8 +198,14 @@ async def get_transcript_via_apify(video_url: str, api_token: str) -> Optional[s
|
|
198 |
elif response.status_code == 400: logger.error(f"[Apify SyncItems] Bad Request (400) for {video_url}. Check payload. Resp:{response.text[:200]}"); return None
|
199 |
elif response.status_code == 401: logger.error("[Apify SyncItems] Auth error (401). Check token."); return None
|
200 |
elif response.status_code == 404:
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
else: logger.error(f"[Apify SyncItems] Unexpected status {response.status_code} for {video_url}. Resp:{response.text[:200]}"); return None
|
204 |
|
205 |
except httpx.TimeoutException as e: logger.error(f"[Apify SyncItems] Timeout during API interaction for {video_url}: {e}"); return None
|
@@ -326,7 +332,7 @@ async def generate_summary(text: str, summary_type: str) -> str:
|
|
326 |
elif response.status_code == 429: logger.warning("OpenRouter Rate Limit Exceeded (429)."); return "Sorry, AI model is busy. Try again."
|
327 |
elif response.status_code == 500: logger.error(f"OpenRouter Internal Server Error (500). Resp:{response.text[:500]}"); return "Sorry, AI service internal error."
|
328 |
else:
|
329 |
-
#
|
330 |
error_info = ""
|
331 |
try:
|
332 |
error_info = response.json().get("error", {}).get("message", "")
|
|
|
1 |
+
# main.py (Correcting SyntaxError at line 201)
|
2 |
import os
|
3 |
import re
|
4 |
import logging
|
|
|
132 |
if not video_id: logger.error("[Supadata] No video_id provided"); return None
|
133 |
if not api_key: logger.error("[Supadata] API key missing."); return None
|
134 |
logger.info(f"[Supadata] Attempting fetch for video ID: {video_id}")
|
135 |
+
api_endpoint = "https://api.supadata.ai/v1/youtube/transcript"
|
136 |
params = {"videoId": video_id, "format": "text"}; headers = {"X-API-Key": api_key}
|
137 |
try:
|
138 |
async with httpx.AsyncClient(timeout=30.0) as client:
|
|
|
153 |
else: logger.error(f"[Supadata] Unexpected status {response.status_code} for {video_id}. Resp: {response.text[:200]}"); return None
|
154 |
except httpx.TimeoutException: logger.error(f"[Supadata] Timeout connecting for {video_id}"); return None
|
155 |
except httpx.RequestError as e:
|
156 |
+
if "CERTIFICATE_VERIFY_FAILED" in str(e): logger.error(f"[Supadata] SSL Cert Verify Failed for {video_id}: {e}")
|
157 |
else: logger.error(f"[Supadata] Request error for {video_id}: {e}")
|
158 |
return None
|
159 |
except Exception as e: logger.error(f"[Supadata] Unexpected error for {video_id}: {e}", exc_info=True); return None
|
160 |
|
161 |
async def get_transcript_via_apify(video_url: str, api_token: str) -> Optional[str]:
|
162 |
"""Fetches YouTube transcript using Apify REST API (run-sync-get-dataset-items endpoint)."""
|
163 |
+
global APIFY_ACTOR_ID
|
164 |
if not video_url: logger.error("[Apify SyncItems] No video_url provided"); return None
|
165 |
if not api_token: logger.error("[Apify SyncItems] API token missing."); return None
|
166 |
logger.info(f"[Apify SyncItems] Attempting fetch for URL: {video_url} (Actor: {APIFY_ACTOR_ID})")
|
167 |
|
168 |
sync_items_endpoint = f"https://api.apify.com/v2/acts/{APIFY_ACTOR_ID}/run-sync-get-dataset-items"
|
169 |
+
params = {"token": api_token}
|
170 |
payload = { "urls": [video_url], "outputFormat": "singleStringText", "maxRetries": 5, "channelHandleBoolean": False, "channelNameBoolean": False, "datePublishedBoolean": False, "relativeDateTextBoolean": False, }
|
171 |
+
headers = {"Content-Type": "application/json"}
|
172 |
|
173 |
try:
|
174 |
async with httpx.AsyncClient(timeout=120.0) as client:
|
|
|
198 |
elif response.status_code == 400: logger.error(f"[Apify SyncItems] Bad Request (400) for {video_url}. Check payload. Resp:{response.text[:200]}"); return None
|
199 |
elif response.status_code == 401: logger.error("[Apify SyncItems] Auth error (401). Check token."); return None
|
200 |
elif response.status_code == 404:
|
201 |
+
# *** FIX: Correct indentation for error info extraction ***
|
202 |
+
error_info = ""
|
203 |
+
try:
|
204 |
+
error_info = response.json().get("error", {}).get("message", "")
|
205 |
+
except Exception:
|
206 |
+
pass
|
207 |
+
logger.error(f"[Apify SyncItems] Endpoint/Actor Not Found (404). Error: '{error_info}' Resp:{response.text[:200]}");
|
208 |
+
return None
|
209 |
else: logger.error(f"[Apify SyncItems] Unexpected status {response.status_code} for {video_url}. Resp:{response.text[:200]}"); return None
|
210 |
|
211 |
except httpx.TimeoutException as e: logger.error(f"[Apify SyncItems] Timeout during API interaction for {video_url}: {e}"); return None
|
|
|
332 |
elif response.status_code == 429: logger.warning("OpenRouter Rate Limit Exceeded (429)."); return "Sorry, AI model is busy. Try again."
|
333 |
elif response.status_code == 500: logger.error(f"OpenRouter Internal Server Error (500). Resp:{response.text[:500]}"); return "Sorry, AI service internal error."
|
334 |
else:
|
335 |
+
# Corrected Indentation for error info extraction
|
336 |
error_info = ""
|
337 |
try:
|
338 |
error_info = response.json().get("error", {}).get("message", "")
|