Skip to content

Commit

Permalink
Make _config method more generic by utilizing **kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
AliOsm committed Nov 1, 2024
1 parent cf92fc6 commit 2fc1d3b
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions tafrigh/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,10 @@ class Downloader:
def __init__(self, playlist_items: str, output_dir: str):
self.playlist_items = playlist_items
self.output_dir = output_dir

self._initialize_youtube_dl_with_archive()
self._initialize_youtube_dl_without_archive()

def _config(self, download_archive: str | bool) -> dict[str, Any]:
return {
'download_archive': download_archive,
'extract_audio': True,
'extract_flat': True,
'format': 'bestaudio',
'ignoreerrors': True,
'outtmpl': os.path.join(self.output_dir, '%(id)s.%(ext)s'),
'playlist_items': self.playlist_items,
'postprocessors': [
{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
},
],
'quiet': True,
'verbose': False,
}

def download(self, url: str, retries: int = 3, save_response: bool = False) -> dict[str, Any]:
while True:
self.youtube_dl_with_archive.download(url)
Expand All @@ -49,10 +31,33 @@ def download(self, url: str, retries: int = 3, save_response: bool = False) -> d
return url_data

def _initialize_youtube_dl_with_archive(self) -> None:
self.youtube_dl_with_archive = yt_dlp.YoutubeDL(self._config(os.path.join(self.output_dir, 'archive.txt')))
self.youtube_dl_with_archive = yt_dlp.YoutubeDL(self._config(
download_archive=os.path.join(self.output_dir, 'archive.txt'),
))

def _initialize_youtube_dl_without_archive(self) -> None:
self.youtube_dl_without_archive = yt_dlp.YoutubeDL(self._config(False))
self.youtube_dl_without_archive = yt_dlp.YoutubeDL(self._config(extract_flat=True))

def _config(self, **kwargs: Any) -> dict[str, Any]:
config = {
'extract_audio': True,
'format': 'bestaudio',
'ignoreerrors': True,
'outtmpl': os.path.join(self.output_dir, '%(id)s.%(ext)s'),
'playlist_items': self.playlist_items,
'postprocessors': [
{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
},
],
'quiet': True,
'verbose': False,
}

config.update(kwargs)

return config

def _should_retry(self, url_data: dict[str, Any]) -> bool:
def file_exists(file_name: str) -> bool:
Expand Down

0 comments on commit 2fc1d3b

Please sign in to comment.