From 8c01c473cc1f5d9d434090fb04abfedb3c74454b Mon Sep 17 00:00:00 2001 From: jbannon Date: Sun, 7 Aug 2022 05:08:17 +0000 Subject: [PATCH] actually sort --- src/ytdl_sub/downloaders/downloader.py | 12 ++++++++++-- src/ytdl_sub/downloaders/youtube/channel.py | 2 +- src/ytdl_sub/downloaders/youtube/playlist.py | 2 +- 3 files changed, 12 insertions(+), 4 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..62ded376 100644 --- a/src/ytdl_sub/downloaders/youtube/channel.py +++ b/src/ytdl_sub/downloaders/youtube/channel.py @@ -150,7 +150,7 @@ 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="upload_date") # Iterate in reverse 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 diff --git a/src/ytdl_sub/downloaders/youtube/playlist.py b/src/ytdl_sub/downloaders/youtube/playlist.py index 295463d0..dfe25865 100644 --- a/src/ytdl_sub/downloaders/youtube/playlist.py +++ b/src/ytdl_sub/downloaders/youtube/playlist.py @@ -78,7 +78,7 @@ 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) + playlist_videos = self._filter_entry_dicts(entry_dicts, sort_by="upload_date") # Iterate in reverse 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