From 41c24a9d60e5a686368ddbaf50a7c96470b4bde4 Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Tue, 19 Nov 2024 12:06:08 +0200 Subject: [PATCH] Add previous code --- get_peertube_videos/get_peertube_videos.py | 95 +++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/get_peertube_videos/get_peertube_videos.py b/get_peertube_videos/get_peertube_videos.py index 341b6b9..d3262ad 100644 --- a/get_peertube_videos/get_peertube_videos.py +++ b/get_peertube_videos/get_peertube_videos.py @@ -7,13 +7,17 @@ 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 class GetPeertubeVideos: + '''Get PeerTube videos from a series of URIs containing the JSON feed''' def __init__(self, **kwargs): self.config = kwargs @@ -30,6 +34,54 @@ class GetPeertubeVideos: 'get_peertube_videos.log' ) self._init_log() + self.downloaded_items = [] + self._get_downloaded_items() + self.trans = Client( + host=self.config['tr_host'], + port=self.config['tr_port'], + username=self.config['tr_user'], + password=self.config['tr_password'], + path=self.config['tr_path'], + ) + self.session=requests.Session() + self._process_uris() + + 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) + + def _process_item(self, item): + if item['id'] not in self.downloaded_items: + selected={ + 'size_in_bytes': 0, + } + 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']}'..." + ) + result_torrent = self.session.get(selected['url']) + torrent_bytes = result_torrent.content + self.trans.add_torrent( + torrent_bytes, + download_dir=self.config['download_dir'], labels=item['tags'] + ) + 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") + + def _get_downloaded_items(self): + if os.path.exists(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) def _init_log(self): ''' Initialize log object ''' @@ -83,8 +135,47 @@ class GetPeertubeVideos: help='Set the debug level for the standard output.' ) @click.option('--log-file', '-l', help="File to store all debug messages.") -# @click.option("--dummy","-n", is_flag=True, -# help="Don't do anything, just show what would be done.") +@click.option( + '--uris', '-u', + multiple=True, + required=True, + help='PeerTube channels URI to look up' +) +@click.option( + '--downloaded-database', '-d', + default=f"{os.environ.get('HOME', os.environ.get('USERPROFILE', ''))}/.config/downloaded_youtube_videos", + help='File to store the IDs of downloaded videos' +) +@click.option( + '--download-dir', '-f', + default=f"{os.environ.get('HOME', os.environ.get('USERPROFILE', ''))}/downloaded_youtube_videos", + help='Folder to store the downloaded videos' +) +@click.option( + '--tr-host', '-H', + default='localhost', + help='Transmission daemon host' +) +@click.option( + '--tr-port', '-p', + default=12345, + help='Transmission daemon RPC port' +) +@click.option( + '--tr-user', '-u', + default='transmission', + help='Transmission daemon user name' +) +@click.option( + '--tr-password', '-p', + default='', + help='Transmission daemon user password' +) +@click.option( + '--tr-path', '-P', + default='/transmission/', + help='Transmission daemon RPC path' +) @click_config_file.configuration_option() def __main__(**kwargs): return GetPeertubeVideos(**kwargs)