diff --git a/get_peertube_videos/get_peertube_videos.py b/get_peertube_videos/get_peertube_videos.py index 3dbc3b9..7a2425b 100644 --- a/get_peertube_videos/get_peertube_videos.py +++ b/get_peertube_videos/get_peertube_videos.py @@ -7,13 +7,13 @@ import sys import os -import json import logging from logging.handlers import SysLogHandler import click import click_config_file import requests from transmission_rpc import Client +import feedparser class GetPeertubeVideos: @@ -48,10 +48,15 @@ class GetPeertubeVideos: def _process_uris(self): for uri in self.config['uris']: - result = self.session.get(uri) - json_data = result.json() - for item in json_data['items']: - self._process_item(item) + if '.json' in uri: + result = self.session.get(uri) + json_data = result.json() + for item in json_data['items']: + self._process_item(item) + elif '.rss' in uri: + feed = feedparser.parse(uri) + for entry in feed['entries']: + self._process_item(entry) def _process_item(self, item): self._log.debug( @@ -67,35 +72,59 @@ class GetPeertubeVideos: "%s attachments (videos) for this item", len(item['attachments']) ) - for attachment in item['attachments']: - if attachment['size_in_bytes'] > selected['size_in_bytes']: + if 'attachements' in item: + attachments = item['attachments'] + size_field = 'size_in_bytes' + elif 'media_content' in item: + attachments = item['media_content'] + size_field = 'filesize' + else: + self._log.error( + "Unrecognize item in feed. %s", + item + ) + return None + for attachment in attachments: + if attachment[size_field] > selected[size_field]: selected = attachment 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._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']) - self._write_downloaded_items() + if '.torrent' in selected['url']: + 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._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']) + self._write_downloaded_items() + elif 'video' in selected['type']: + self._log.info( + "Downloading video '%s'...", + selected['url'] + ) + file_extension = selected['type'].replace('video/', '.') + file_name = os.path.join(self.config['download_dir'], item['title'], file_extension) + result = self.session.get(selected['url']) + video_bytes = result.content + with open(file_name, mode='wb') as video_file: + video_file.write(video_bytes) else: self._log.debug( "Item already downloaded, skipping."