"""YouTube downloader.""" import logging from typing import Any, Dict import config from download.base import my_hook import yt_dlp logger = logging.getLogger("syllabus") class youtube: """YouTube content downloader.""" @staticmethod def ydl(url: str, location: str) -> None: """Download a YouTube video to the specified location.""" try: logger.info(f'Received download request for {url}') dl_ops: Dict[str, Any] = { 'progress_hooks': [my_hook], 'download_archive': str(config.YOUTUBE_ARCHIVE), 'paths': { 'temp': str(config.TEMP_DIR), 'home': location }, 'outtmpl': '%(uploader)s/%(title)s.%(ext)s' } # Audio format configuration audio_format_config = { 'format': config.AUDIO_FORMAT, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': config.AUDIO_QUALITY, }, { 'key': 'FFmpegMetadata', 'add_metadata': True, }] } # Apply format-specific options based on location if location == str(config.PODCASTS_DIR) or location == str(config.ASMR_DIR): dl_ops.update(audio_format_config) elif location == str(config.YOUTUBE_DIR): dl_ops['format'] = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best' dl_ops['cookiefile'] = str(config.YOUTUBE_COOKIES) else: dl_ops['format'] = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best' with yt_dlp.YoutubeDL(dl_ops) as ydl: ydl.download([url]) logger.info(f"Download completed for {url}") except Exception as e: logger.error(f"Error downloading {url}: {e}") raise