diff --git a/get_peertube_videos/get_peertube_videos.py b/get_peertube_videos/get_peertube_videos.py index d3262ad..2862968 100644 --- a/get_peertube_videos/get_peertube_videos.py +++ b/get_peertube_videos/get_peertube_videos.py @@ -54,34 +54,71 @@ class GetPeertubeVideos: self._process_item(item) def _process_item(self, item): + self._log.debug( + "Processing item: %s", + item + ) if item['id'] not in self.downloaded_items: selected={ 'size_in_bytes': 0, } + self._log.debug( + "%s attachments (videos) for this item", + len(item['attachments']) + ) for attachment in item['attachments']: if attachment['size_in_bytes'] > selected['size_in_bytes']: selected = attachment - print( - f"Adding torrent '{selected['url']}' for video '{selected['title']}'..." + if 'url' not in selected: + self._log.error( + "No attachments with size bigger than 0. No torrent to add." + ) + return False + self._log.info( + "Adding torrent '%s' for video '%s'...", + selected['url'], + selected['title'] ) result_torrent = self.session.get(selected['url']) torrent_bytes = result_torrent.content - self.trans.add_torrent( + self._log.debug( + "Torrent file downloaded with %s bytes of data", + len(torrent_bytes) + ) + result_add = self.trans.add_torrent( torrent_bytes, download_dir=self.config['download_dir'], labels=item['tags'] ) + self._log.debug( + "Torrent added to Transmission with result: %s", + result_add + ) self.downloaded_items.append(item['id']) - with open(self.config['downloaded_database'], 'w', encoding='utf-8') as db_file: - for item in self.downloaded_items: - db_file.write(f"{item}\n") + self._write_downloaded_items() + else: + self._log.debug( + "Item already downloaded, skipping." + ) + + def _write_downloaded_items(self): + with open(self.config['downloaded_database'], 'w', encoding='utf-8') as db_file: + for download_item in self.downloaded_items: + db_file.write(f"{download_item}\n") def _get_downloaded_items(self): if os.path.exists(self.config['downloaded_database']): + self._log.debug( + "Reading already downloaded items from '%s'...", + self.config['downloaded_database'] + ) with open(self.config['downloaded_database'], 'r', encoding='utf-8') as db_file: self.downloaded_items = db_file.read().split('\n') else: - with open(self.config['downloaded_database'], 'w', encoding='utf-8') as db_file: - db_file.write(self.downloaded_items) + self._log.debug( + "Initializing downloaded items database '%s'...", + self.config['downloaded_database'] + ) + self._write_downloaded_items() def _init_log(self): ''' Initialize log object '''