This commit is contained in:
jbannon 2022-03-30 22:14:21 +00:00
parent 3a5c6c80dc
commit 43cc4cd4c7
3 changed files with 32 additions and 11 deletions

View file

@ -9,8 +9,8 @@ import yt_dlp as ytdl
class Downloader:
"""
Class that interacts with ytdl to perform the download of metadata and content, and should translate that to
list of Entry objects.
Class that interacts with ytdl to perform the download of metadata and content,
and should translate that to list of Entry objects.
"""
@classmethod
@ -29,7 +29,7 @@ class Downloader:
) -> Dict:
"""Configure the ytdl options for the downloader"""
if ytdl_options is None:
ytdl_options = dict()
ytdl_options = {}
# Overwrite defaults with input
ytdl_options = dict(cls.ytdl_option_defaults(), **ytdl_options)

View file

@ -7,23 +7,36 @@ from ytdl_subscribe.entries.soundcloud import SoundcloudTrack
class SoundcloudDownloader(Downloader):
"""
Class that handles downloading soundcloud entries via ytdl and converting them into
SoundcloudTrack / SoundcloudAlbumTrack objects
"""
@classmethod
def ytdl_option_defaults(cls) -> Dict:
"""Returns default format to be best mp3"""
return {
"format": "bestaudio[ext=mp3]",
}
@classmethod
def artist_url(cls, artist_name: str) -> str:
"""Returns full artist url"""
return f"https://soundcloud.com/{artist_name}"
def download_albums(self, artist_name) -> List[SoundcloudAlbum]:
def download_albums(self, artist_name: str) -> List[SoundcloudAlbum]:
"""
Given an artist name, download all of their albums
"""
artist_albums_url = f"{self.artist_url(artist_name)}/albums"
info = self.extract_info(url=artist_albums_url)
return [SoundcloudAlbum(**e) for e in info["entries"]]
def download_tracks(self, artist_name) -> List[SoundcloudTrack]:
"""
Given an artist name, download all of their tracks
"""
artist_tracks_url = f"{self.artist_url(artist_name)}/tracks"
info = self.extract_info(url=artist_tracks_url)

View file

@ -8,15 +8,21 @@ from ytdl_subscribe.entries.youtube import YoutubeVideo
class YoutubeDownloader(Downloader):
"""
Class that handles downloading youtube entries via ytdl and converting them into
YoutubeVideo objects
"""
@classmethod
def playlist_url(cls, playlist_id: str) -> str:
"""Returns full playlist url"""
return f"https://youtube.com/playlist?list={playlist_id}"
def _download_metadata(self, url: str) -> None:
def _download_with_metadata(self, url: str) -> None:
"""
Do not get entries from the extract info, let it write to the info.json file and load that instead.
This is because if the video is already downloaded in a playlist, it will not fetch the metadata
(maybe there is a way??)
Do not get entries from the extract info, let it write to the info.json file and load
that instead. This is because if the video is already downloaded in a playlist, it will
not fetch the metadata (maybe there is a way??)
"""
ytdl_metadata_override = {
"download_archive": str(
@ -32,7 +38,7 @@ class YoutubeDownloader(Downloader):
"""
playlist_url = self.playlist_url(playlist_id=playlist_id)
self._download_metadata(url=playlist_url)
self._download_with_metadata(url=playlist_url)
# Load the entries from info.json, ignore the playlist entry
entries: List[YoutubeVideo] = []
@ -42,7 +48,9 @@ class YoutubeDownloader(Downloader):
if file_name.endswith(".info.json") and not file_name.startswith(
playlist_id
):
with open(Path(self.working_directory) / file_name, "r") as f:
entries.append(YoutubeVideo(**json.load(f)))
with open(
Path(self.working_directory) / file_name, "r", encoding="utf-8"
) as file:
entries.append(YoutubeVideo(**json.load(file)))
return entries