Update app.py
Browse files
app.py
CHANGED
@@ -423,65 +423,4 @@ async def web_search_endpoint(query: str, num_results: int = 5, api_key: str = D
|
|
423 |
result = await web_search(query, num_results)
|
424 |
if not result['success']:
|
425 |
raise HTTPException(status_code=500, detail=result['error'])
|
426 |
-
return result
|
427 |
-
try:
|
428 |
-
logger.info(f"Starting YouTube audio extraction for video ID: {video_id}")
|
429 |
-
|
430 |
-
# Create an InnerTube client for iOS
|
431 |
-
client = InnerTube("IOS")
|
432 |
-
logger.info("InnerTube client created")
|
433 |
-
|
434 |
-
# Fetch video info
|
435 |
-
logger.info("Fetching video info")
|
436 |
-
video_info = client.player(video_id)
|
437 |
-
logger.info("Video info fetched successfully")
|
438 |
-
|
439 |
-
# Extract the audio streams
|
440 |
-
streams = video_info["streamingData"]["adaptiveFormats"]
|
441 |
-
audio_streams = [s for s in streams if s['mimeType'].startswith('audio/')]
|
442 |
-
|
443 |
-
if not audio_streams:
|
444 |
-
logger.warning(f"No audio streams found for video ID {video_id}")
|
445 |
-
return {'success': False, 'error': "No audio streams found"}
|
446 |
-
|
447 |
-
# Choose the highest quality audio stream
|
448 |
-
audio_stream = max(audio_streams, key=lambda x: x.get('bitrate', 0))
|
449 |
-
audio_url = audio_stream['url']
|
450 |
-
logger.info(f"Selected audio stream URL: {audio_url[:100]}...") # Log first 100 chars of URL
|
451 |
-
|
452 |
-
# Prepare headers
|
453 |
-
headers = {
|
454 |
-
'User-Agent': random.choice(USER_AGENTS),
|
455 |
-
'Accept': '*/*',
|
456 |
-
'Accept-Encoding': 'gzip, deflate, br',
|
457 |
-
'Range': 'bytes=0-',
|
458 |
-
'Connection': 'keep-alive',
|
459 |
-
}
|
460 |
-
|
461 |
-
# Download the audio
|
462 |
-
logger.info("Starting audio download")
|
463 |
-
response = requests.get(audio_url, headers=headers, stream=True)
|
464 |
-
logger.info(f"Download response status code: {response.status_code}")
|
465 |
-
|
466 |
-
if response.status_code in [200, 206]: # 200 OK or 206 Partial Content
|
467 |
-
# Create a temporary file
|
468 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
|
469 |
-
temp_file_path = temp_file.name
|
470 |
-
logger.info(f"Created temporary file: {temp_file_path}")
|
471 |
-
# Write the audio data to the file
|
472 |
-
total_size = 0
|
473 |
-
for chunk in response.iter_content(chunk_size=8192):
|
474 |
-
temp_file.write(chunk)
|
475 |
-
total_size += len(chunk)
|
476 |
-
if total_size % (1024 * 1024) == 0: # Log every 1MB
|
477 |
-
logger.info(f"Downloaded {total_size / (1024 * 1024):.2f} MB")
|
478 |
-
|
479 |
-
logger.info(f"Audio download completed. Total size: {total_size / (1024 * 1024):.2f} MB")
|
480 |
-
return {'success': True, 'temp_file_path': temp_file_path}
|
481 |
-
else:
|
482 |
-
logger.error(f"Failed to download audio: HTTP {response.status_code}")
|
483 |
-
return {'success': False, 'error': f"Download failed: HTTP {response.status_code}"}
|
484 |
-
|
485 |
-
except Exception as e:
|
486 |
-
logger.exception(f"Error fetching YouTube audio for video ID {video_id}: {str(e)}")
|
487 |
-
return {'success': False, 'error': f"Error: {str(e)}"} , add ulpoad to s3 funtion and send the s3 file
|
|
|
423 |
result = await web_search(query, num_results)
|
424 |
if not result['success']:
|
425 |
raise HTTPException(status_code=500, detail=result['error'])
|
426 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|