From 47a46265cf364d5348ae4ffa6e3395f541723ca1 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Sat, 6 Aug 2022 22:40:45 -0700 Subject: [PATCH] [FOLLOWUP] Actually process YT channels and playlists ascending (#160) * actually sort * use playlist_index instead --- src/ytdl_sub/downloaders/downloader.py | 12 ++++++++++-- src/ytdl_sub/downloaders/youtube/channel.py | 4 ++-- src/ytdl_sub/downloaders/youtube/playlist.py | 4 ++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/ytdl_sub/downloaders/downloader.py b/src/ytdl_sub/downloaders/downloader.py index 539ab514..5f19999a 100644 --- a/src/ytdl_sub/downloaders/downloader.py +++ b/src/ytdl_sub/downloaders/downloader.py @@ -213,7 +213,10 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT, DownloaderEntryT] ) def _filter_entry_dicts( - self, entry_dicts: List[Dict], extractor: Optional[str] = None + self, + entry_dicts: List[Dict], + extractor: Optional[str] = None, + sort_by: Optional[str] = None, ) -> List[Dict]: """ Parameters @@ -223,6 +226,8 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT, DownloaderEntryT] extractor Optional. Extractor that the entry dicts must have. If None, defaults to the entry type's extractor + sort_by + Optional. Sort the entry dicts on this key Returns ------- @@ -231,9 +236,12 @@ class Downloader(DownloadArchiver, Generic[DownloaderOptionsT, DownloaderEntryT] if extractor is None: extractor = self.downloader_entry_type.entry_extractor - return [ + output = [ entry_dict for entry_dict in entry_dicts if entry_dict.get("extractor") == extractor ] + if sort_by: + output = sorted(output, key=lambda entry_dict: entry_dict[sort_by]) + return output def _get_entry_dicts_from_info_json_files(self) -> List[Dict]: """ diff --git a/src/ytdl_sub/downloaders/youtube/channel.py b/src/ytdl_sub/downloaders/youtube/channel.py index 34d4ca6a..f132393b 100644 --- a/src/ytdl_sub/downloaders/youtube/channel.py +++ b/src/ytdl_sub/downloaders/youtube/channel.py @@ -150,9 +150,9 @@ class YoutubeChannelDownloader(YoutubeDownloader[YoutubeChannelDownloaderOptions url=self.download_options.channel_url, ) self.channel = self._get_channel(entry_dicts=entry_dicts) - channel_videos = self._filter_entry_dicts(entry_dicts=entry_dicts) + channel_videos = self._filter_entry_dicts(entry_dicts, sort_by="playlist_index") - # Iterate in reverse to process older videos first. In case an error occurs and a + # Iterate in descending order to process older videos first. In case an error occurs and a # the channel must be redownloaded, it will fetch most recent metadata first, and break # on the older video that's been processed and is in the download archive. for idx, entry_dict in enumerate(reversed(channel_videos), start=1): diff --git a/src/ytdl_sub/downloaders/youtube/playlist.py b/src/ytdl_sub/downloaders/youtube/playlist.py index 295463d0..514d2ab2 100644 --- a/src/ytdl_sub/downloaders/youtube/playlist.py +++ b/src/ytdl_sub/downloaders/youtube/playlist.py @@ -78,11 +78,11 @@ class YoutubePlaylistDownloader( log_prefix_on_info_json_dl="Downloading metadata for", url=self.download_options.playlist_url, ) - playlist_videos = self._filter_entry_dicts(entry_dicts=entry_dicts) - # Iterate in reverse to process older videos first. In case an error occurs and a + # Iterate in reverse order to process older videos first. In case an error occurs and a # the playlist must be redownloaded, it will fetch most recent metadata first, and break # on the older video that's been processed and is in the download archive. + playlist_videos = self._filter_entry_dicts(entry_dicts, sort_by="playlist_index") for idx, entry_dict in enumerate(reversed(playlist_videos), start=1): video = YoutubePlaylistVideo( entry_dict=entry_dict, working_directory=self.working_directory