Add option for RSS feeds

This commit is contained in:
Antonio J. Delgado 2024-12-28 19:20:41 +02:00
parent 528aced689
commit 94c034da3b

View file

@ -7,13 +7,13 @@
import sys import sys
import os import os
import json
import logging import logging
from logging.handlers import SysLogHandler from logging.handlers import SysLogHandler
import click import click
import click_config_file import click_config_file
import requests import requests
from transmission_rpc import Client from transmission_rpc import Client
import feedparser
class GetPeertubeVideos: class GetPeertubeVideos:
@ -48,10 +48,15 @@ class GetPeertubeVideos:
def _process_uris(self): def _process_uris(self):
for uri in self.config['uris']: for uri in self.config['uris']:
result = self.session.get(uri) if '.json' in uri:
json_data = result.json() result = self.session.get(uri)
for item in json_data['items']: json_data = result.json()
self._process_item(item) 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): def _process_item(self, item):
self._log.debug( self._log.debug(
@ -67,35 +72,59 @@ class GetPeertubeVideos:
"%s attachments (videos) for this item", "%s attachments (videos) for this item",
len(item['attachments']) len(item['attachments'])
) )
for attachment in item['attachments']: if 'attachements' in item:
if attachment['size_in_bytes'] > selected['size_in_bytes']: 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 selected = attachment
if 'url' not in selected: if 'url' not in selected:
self._log.error( self._log.error(
"No attachments with size bigger than 0. No torrent to add." "No attachments with size bigger than 0. No torrent to add."
) )
return False return False
self._log.info( if '.torrent' in selected['url']:
"Adding torrent '%s' for video '%s'...", self._log.info(
selected['url'], "Adding torrent '%s' for video '%s'...",
selected['title'] selected['url'],
) selected['title']
result_torrent = self.session.get(selected['url']) )
torrent_bytes = result_torrent.content result_torrent = self.session.get(selected['url'])
self._log.debug( torrent_bytes = result_torrent.content
"Torrent file downloaded with %s bytes of data", self._log.debug(
len(torrent_bytes) "Torrent file downloaded with %s bytes of data",
) len(torrent_bytes)
result_add = self.trans.add_torrent( )
torrent_bytes, result_add = self.trans.add_torrent(
download_dir=self.config['download_dir'], labels=item['tags'] torrent_bytes,
) download_dir=self.config['download_dir'], labels=item['tags']
self._log.debug( )
"Torrent added to Transmission with result: %s", self._log.debug(
result_add "Torrent added to Transmission with result: %s",
) result_add
self.downloaded_items.append(item['id']) )
self._write_downloaded_items() 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: else:
self._log.debug( self._log.debug(
"Item already downloaded, skipping." "Item already downloaded, skipping."