Spaces:
Sleeping
Sleeping
Update providers/archive_provider.py
Browse files
providers/archive_provider.py
CHANGED
|
@@ -42,10 +42,12 @@ def search(query: str, has_transcript: bool = False):
|
|
| 42 |
})
|
| 43 |
|
| 44 |
return results_list
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
logging.error(f"An error occurred during search in archive_provider: {e}")
|
| 47 |
return {"error": "Failed to perform search."}
|
| 48 |
|
|
|
|
| 49 |
def get_details(identifier: str):
|
| 50 |
"""
|
| 51 |
Retrieves key details for a single item from the Internet Archive, excluding the transcript.
|
|
@@ -71,8 +73,31 @@ def get_details(identifier: str):
|
|
| 71 |
|
| 72 |
video_url = f"https://archive.org/download/{identifier}/{video_file['name']}" if video_file else None
|
| 73 |
|
| 74 |
-
# --- Find Audio File ---
|
| 75 |
-
audio_file =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
audio_url = f"https://archive.org/download/{identifier}/{audio_file['name']}" if audio_file else None
|
| 77 |
|
| 78 |
# --- Structure the final response ---
|
|
@@ -93,8 +118,34 @@ def get_details(identifier: str):
|
|
| 93 |
except Exception as e:
|
| 94 |
logging.error(f"An error occurred getting details in archive_provider for {identifier}: {e}")
|
| 95 |
return {"error": f"Failed to retrieve details for item {identifier}."}
|
| 96 |
-
return response
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
-
logging.error(f"An error occurred getting
|
| 100 |
-
return {"error": f"Failed to retrieve
|
|
|
|
| 42 |
})
|
| 43 |
|
| 44 |
return results_list
|
| 45 |
+
|
| 46 |
except Exception as e:
|
| 47 |
logging.error(f"An error occurred during search in archive_provider: {e}")
|
| 48 |
return {"error": "Failed to perform search."}
|
| 49 |
|
| 50 |
+
|
| 51 |
def get_details(identifier: str):
|
| 52 |
"""
|
| 53 |
Retrieves key details for a single item from the Internet Archive, excluding the transcript.
|
|
|
|
| 73 |
|
| 74 |
video_url = f"https://archive.org/download/{identifier}/{video_file['name']}" if video_file else None
|
| 75 |
|
| 76 |
+
# --- Find Audio File (الطريقة المحسّنة) ---
|
| 77 |
+
audio_file = None
|
| 78 |
+
|
| 79 |
+
# قائمة بأولويات الـ formats من الأحسن للأسوأ
|
| 80 |
+
audio_priority = [
|
| 81 |
+
'VBR MP3', # الأفضل في الجودة
|
| 82 |
+
'MP3', # عادي
|
| 83 |
+
'64Kbps MP3', # جودة أقل
|
| 84 |
+
'Ogg Vorbis', # بديل مفتوح المصدر
|
| 85 |
+
'FLAC', # lossless لكن حجم كبير
|
| 86 |
+
'M4A', # Apple format
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
# دور بالترتيب
|
| 90 |
+
for fmt in audio_priority:
|
| 91 |
+
audio_file = next((f for f in item.files if fmt in f.get('format', '')), None)
|
| 92 |
+
if audio_file:
|
| 93 |
+
break
|
| 94 |
+
|
| 95 |
+
# لو مفيش، دور على امتدادات
|
| 96 |
+
if not audio_file:
|
| 97 |
+
audio_file = next((f for f in item.files
|
| 98 |
+
if any(f.get('name', '').lower().endswith(ext)
|
| 99 |
+
for ext in ['.mp3', '.ogg', '.m4a', '.flac'])), None)
|
| 100 |
+
|
| 101 |
audio_url = f"https://archive.org/download/{identifier}/{audio_file['name']}" if audio_file else None
|
| 102 |
|
| 103 |
# --- Structure the final response ---
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
logging.error(f"An error occurred getting details in archive_provider for {identifier}: {e}")
|
| 120 |
return {"error": f"Failed to retrieve details for item {identifier}."}
|
|
|
|
| 121 |
|
| 122 |
+
|
| 123 |
+
def get_transcript(identifier: str):
|
| 124 |
+
"""
|
| 125 |
+
Retrieves just the transcript for a single item from the Internet Archive.
|
| 126 |
+
"""
|
| 127 |
+
item = _get_item_or_error(identifier)
|
| 128 |
+
if isinstance(item, dict) and "error" in item:
|
| 129 |
+
return item
|
| 130 |
+
|
| 131 |
+
try:
|
| 132 |
+
transcript_content = "Transcript not available."
|
| 133 |
+
transcript_file_info = next((f for f in item.files if f.get('format') == 'SubRip'), None) or \
|
| 134 |
+
next((f for f in item.files if f.get('format') == 'Page Turn'), None) or \
|
| 135 |
+
next((f for f in item.files if f.get('name', '').endswith('_ocr.txt')), None)
|
| 136 |
+
|
| 137 |
+
if transcript_file_info:
|
| 138 |
+
transcript_file = item.get_file(transcript_file_info['name'])
|
| 139 |
+
try:
|
| 140 |
+
transcript_content = transcript_file.read().decode('utf-8', 'ignore')
|
| 141 |
+
except Exception as e:
|
| 142 |
+
logging.error(f"Could not read transcript for {identifier}: {e}")
|
| 143 |
+
return {"error": f"Transcript found for item {identifier} but could not be read."}
|
| 144 |
+
|
| 145 |
+
return {
|
| 146 |
+
"identifier": identifier,
|
| 147 |
+
"transcript": transcript_content
|
| 148 |
+
}
|
| 149 |
except Exception as e:
|
| 150 |
+
logging.error(f"An error occurred getting transcript in archive_provider for {identifier}: {e}")
|
| 151 |
+
return {"error": f"Failed to retrieve transcript for item {identifier}."}
|