|
10 | 10 | from services.authentication import authenticate |
11 | 11 | from services.file_management import download_file |
12 | 12 | from urllib.parse import quote, urlparse |
| 13 | +import requests |
13 | 14 |
|
14 | 15 | v1_media_download_bp = Blueprint('v1_media_download', __name__) |
15 | 16 | logger = logging.getLogger(__name__) |
|
57 | 58 | "properties": { |
58 | 59 | "download": {"type": "boolean"}, |
59 | 60 | "languages": {"type": "array", "items": {"type": "string"}}, |
60 | | - "formats": {"type": "array", "items": {"type": "string"}} |
| 61 | + "format": { |
| 62 | + "type": "string", |
| 63 | + "enum": [ |
| 64 | + "srt", # SubRip Subtitle (most common) |
| 65 | + "vtt", # Web Video Text Tracks |
| 66 | + "json3" # YouTube's JSON format |
| 67 | + ] |
| 68 | + }, |
| 69 | + "cloud_upload": {"type": "boolean"} |
61 | 70 | } |
62 | 71 | }, |
63 | 72 | "download": { |
@@ -150,8 +159,8 @@ def download_media(job_id, data): |
150 | 159 | ydl_opts['writesubtitles'] = subtitle_options.get('download', False) |
151 | 160 | if subtitle_options.get('languages'): |
152 | 161 | ydl_opts['subtitleslangs'] = subtitle_options['languages'] |
153 | | - if subtitle_options.get('formats'): |
154 | | - ydl_opts['subtitlesformat'] = subtitle_options['formats'] |
| 162 | + if subtitle_options.get('format'): |
| 163 | + ydl_opts['subtitlesformat'] = subtitle_options['format'] |
155 | 164 |
|
156 | 165 | # Add download options if specified |
157 | 166 | if download_options: |
@@ -222,6 +231,56 @@ def download_media(job_id, data): |
222 | 231 | except Exception as e: |
223 | 232 | logger.error(f"Error processing thumbnail: {str(e)}") |
224 | 233 | continue |
| 234 | + |
| 235 | + # Process subtitles if available |
| 236 | + if 'subtitles' in info and subtitle_options.get('download', False): |
| 237 | + logger.info(f"Job {job_id}: Found subtitles in info: {info['subtitles']}") |
| 238 | + response["subtitles"] = {} # Changed from array to object |
| 239 | + requested_languages = subtitle_options.get('languages', []) |
| 240 | + requested_format = subtitle_options.get('format', 'srt') |
| 241 | + subtitle_cloud_upload = subtitle_options.get('cloud_upload', True) # Default to True |
| 242 | + |
| 243 | + # If no languages specified, use all available languages |
| 244 | + if not requested_languages: |
| 245 | + requested_languages = list(info['subtitles'].keys()) |
| 246 | + logger.info(f"Job {job_id}: No languages specified, using all available: {requested_languages}") |
| 247 | + |
| 248 | + for lang, subtitle_list in info['subtitles'].items(): |
| 249 | + # Skip if language not in requested list |
| 250 | + if lang not in requested_languages: |
| 251 | + continue |
| 252 | + |
| 253 | + try: |
| 254 | + logger.info(f"Job {job_id}: Processing subtitle for language {lang}") |
| 255 | + # Find the requested format |
| 256 | + subtitle_data = None |
| 257 | + for subtitle in subtitle_list: |
| 258 | + if subtitle['ext'] == requested_format: |
| 259 | + subtitle_data = subtitle |
| 260 | + break |
| 261 | + |
| 262 | + if not subtitle_data: |
| 263 | + logger.warning(f"Job {job_id}: Requested format {requested_format} not available for {lang}") |
| 264 | + continue |
| 265 | + |
| 266 | + # If cloud upload is requested, download and upload the subtitle |
| 267 | + if subtitle_cloud_upload: |
| 268 | + try: |
| 269 | + subtitle_path = download_file(subtitle_data['url'], temp_dir) |
| 270 | + cloud_url = upload_file(subtitle_path) |
| 271 | + subtitle_data['url'] = cloud_url |
| 272 | + except Exception as e: |
| 273 | + logger.warning(f"Job {job_id}: Failed to download subtitle for {lang}: {str(e)}") |
| 274 | + continue |
| 275 | + |
| 276 | + # Add subtitle data to response using language code as key |
| 277 | + response["subtitles"][lang] = subtitle_data |
| 278 | + logger.info(f"Job {job_id}: Successfully processed subtitle for {lang}") |
| 279 | + except Exception as e: |
| 280 | + logger.error(f"Job {job_id}: Error processing subtitle: {str(e)}") |
| 281 | + continue |
| 282 | + else: |
| 283 | + logger.info(f"Job {job_id}: No subtitles found in info or download not requested") |
225 | 284 |
|
226 | 285 | return response, "/v1/media/download", 200 |
227 | 286 |
|
|
0 commit comments