From 4c67011264360774f3e8d3881c9cd0c6f861f7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20=C5=BBarnowiecki?= Date: Mon, 28 Dec 2020 22:58:01 +0100 Subject: [PATCH] backends: Add deluge --- tdpt/backends/deluge.py | 63 +++++++++++++++++++++++++++++++ tdpt/backends/rtorrent.py | 2 +- tdpt/backends/transmission.py | 2 +- tdpt/handlers/message_handlers.py | 6 +-- tdpt/tdpt.ini.template | 7 ++++ 5 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 tdpt/backends/deluge.py diff --git a/tdpt/backends/deluge.py b/tdpt/backends/deluge.py new file mode 100644 index 0000000..19c85f0 --- /dev/null +++ b/tdpt/backends/deluge.py @@ -0,0 +1,63 @@ +import base64 +import datetime + +from deluge_client import DelugeRPCClient + + +class Client: + def __init__(self, config): + self.client = DelugeRPCClient(config['host'], int(config['port']), + config['username'], config['password']) + + def add_torrent(self, file_name, torrent, timeout=None, **kwargs): + barray = torrent.download_as_bytearray() + filedump = base64.b64encode(barray).decode('utf-8') + self.client.core.add_torrent_file(file_name, filedump, timeout, + **kwargs) + + def get_torrents(self): + """Yields Torrents objects.""" + yield from map(lambda torrent: Torrent(torrent, self.client), + self.client.core.get_torrents_status({}, ())) + + +class Torrent: + + PROPERTIES = ('name', 'state', 'hash', 'progress', 'eta', 'peers', + 'download_payload_rate') + + def __init__(self, torrent_hash, client): + self.torrent = None + self.client = client + self.update(torrent_hash) + + def is_downloading(self) -> bool: + return self.torrent[b'state'] == b'Downloading' + + def update(self, torrent_hash=None): + h = torrent_hash if torrent_hash else self.torrent[b'hash'] + self.torrent = self.client.core.get_torrent_status(h, self.PROPERTIES) + + @property + def download_rate(self) -> int: + return self.torrent[b'download_payload_rate'] + + @property + def eta(self): + return datetime.timedelta(seconds=self.torrent[b'eta']) + + @property + def id(self): + return self.torrent[b'hash'] + + @property + def name(self): + return str(self.torrent[b'name'], 'utf-8') + + @property + def peers_connected(self): + return len(self.torrent[b'peers']) + + @property + def percent_done(self): + return self.torrent[b'progress'] / 100 diff --git a/tdpt/backends/rtorrent.py b/tdpt/backends/rtorrent.py index 4627b82..347afd6 100644 --- a/tdpt/backends/rtorrent.py +++ b/tdpt/backends/rtorrent.py @@ -10,7 +10,7 @@ def __init__(self, config): self.config = config self.proxy = xmlrpc.client.ServerProxy(url, verbose=False) - def add_torrent(self, torrent, timeout=None, **kwargs): + def add_torrent(self, file_name, torrent, timeout=None, **kwargs): self.proxy.load.start_verbose( '', torrent.file_path, diff --git a/tdpt/backends/transmission.py b/tdpt/backends/transmission.py index 70b089c..971629c 100644 --- a/tdpt/backends/transmission.py +++ b/tdpt/backends/transmission.py @@ -16,7 +16,7 @@ def __init__(self, config): self.transmission = transmissionrpc.Client(config['host'], config['port']) - def add_torrent(self, torrent, timeout=None, **kwargs): + def add_torrent(self, file_name, torrent, timeout=None, **kwargs): """Adds new torrent to download list. Args: diff --git a/tdpt/handlers/message_handlers.py b/tdpt/handlers/message_handlers.py index 6fad8c7..6aa47a9 100644 --- a/tdpt/handlers/message_handlers.py +++ b/tdpt/handlers/message_handlers.py @@ -13,9 +13,9 @@ def __init__(self, client): def callback(self, update, context): torrent = update.message.document.get_file() - self.client.add_torrent(torrent) - logging.info('Added new torrent file %s', - update.message.document.file_name) + file_name = update.message.document.file_name + self.client.add_torrent(file_name, torrent) + logging.info('Added new torrent file %s', file_name) context.bot.send_message( chat_id=update.effective_chat.id, text='Added *{}*'.format(update.message.document.file_name), diff --git a/tdpt/tdpt.ini.template b/tdpt/tdpt.ini.template index bd71d10..9773f86 100644 --- a/tdpt/tdpt.ini.template +++ b/tdpt/tdpt.ini.template @@ -21,6 +21,13 @@ # Will set label to uploaded torrent visible in ruTorrent. #set_label = tacajushi-telegram +#[deluge] +#host = 127.0.0.1 +#port = 58846 +#username = localclient +# You can find password in `auth` file in deluge config directory +#password = + [Telegram] # You can obtain it by asking several question to @BotFather and # creating your own bot.