diff --git a/get_youtube_videos/get_youtube_videos.py b/get_youtube_videos/get_youtube_videos.py index 5a1413d..f107c7c 100644 --- a/get_youtube_videos/get_youtube_videos.py +++ b/get_youtube_videos/get_youtube_videos.py @@ -202,7 +202,17 @@ class GetYoutubeVideos: } with yt_dlp.YoutubeDL(ydl_opts) as ydl: try: - ydl.download(uri) + return_code = ydl.download(uri) + self._process_download( + { + "return_code": return_code, + 'info_dict': { + 'id': video_id, + }, + 'filename': video_info.get('title', '?'), + 'video_info': video_info, + } + ) except yt_dlp.utils.DownloadError as error: self._log.error( "Error getting video. %s", @@ -227,32 +237,37 @@ class GetYoutubeVideos: self.summary['processed_channels'] = self.channels_count self.summary['total_count'] = self.total_count + + def _process_download(self, data): + self.total_count += 1 + self.channel_count += 1 + if self.total_count == self.config['total_limit']: + self._log.info( + "Limit (%s) reached for videos from all channels", + self.config['total_limit'], + ) + sys.exit(0) + if self.channel_count == self.config['channel_limit']: + self._log.info( + "Limit (%s) reached for videos for this channel '%s'", + self.config['channel_limit'], + data['info_dict'].get('channel', '?') + ) + # break + info_filename = os.path.join(self.config['download_dir'], f"{os.path.basename(data['filename'])}.download_info.json") + self._log.debug( + "Writting download information in to file '%s'", + info_filename + ) + with open(info_filename, 'w', encoding='utf-8') as info_file: + json.dump(data, info_file, indent=2) + if 'id' in data['info_dict']: + self._save_downloaded_items(data['info_dict']['id']) + + def _yt_progress_hook(self, data): if data['status'] == 'finished': - self.total_count += 1 - self.channel_count += 1 - if self.total_count == self.config['total_limit']: - self._log.info( - "Limit (%s) reached for videos from all channels", - self.config['total_limit'], - ) - sys.exit(0) - if self.channel_count == self.config['channel_limit']: - self._log.info( - "Limit (%s) reached for videos for this channel '%s'", - self.config['channel_limit'], - data['info_dict'].get('channel', '?') - ) - # break - info_filename = os.path.join(self.config['download_dir'], f"{os.path.basename(data['filename'])}.download_info.json") - self._log.debug( - "Writting download information in to file '%s'", - info_filename - ) - with open(info_filename, 'w', encoding='utf-8') as info_file: - json.dump(data, info_file, indent=2) - if 'id' in data['info_dict']: - self._save_downloaded_items(data['info_dict']['id']) + self._process_download(data) elif data['status'] == 'downloading': downloading = True else: