From dda53b8e2494e20efd4252d06890af2bdeeb7853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20=C5=BBarnowiecki?= Date: Mon, 27 Apr 2020 15:54:28 +0200 Subject: [PATCH] Pass whole File object to Client Every `Client` can handle torrent files in different ways. One can work with `bytearrays` while the other one can only with a `path`. By passing raw `File` object we let the `Client` handle operations that it supports. --- backends/transmission.py | 12 +++++++++++- handlers/message_handlers.py | 6 ++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/backends/transmission.py b/backends/transmission.py index 972dc01..70b089c 100644 --- a/backends/transmission.py +++ b/backends/transmission.py @@ -1,3 +1,5 @@ +import base64 + import transmissionrpc @@ -15,7 +17,15 @@ def __init__(self, config): config['port']) def add_torrent(self, torrent, timeout=None, **kwargs): - return Torrent(self.transmission.add_torrent(torrent, timeout, **kwargs)) + """Adds new torrent to download list. + + Args: + torrent (telegram.File): File torrent object. + timeout (int): Drop connection after timeout seconds. + """ + barray = torrent.download_as_bytearray() + filedump = base64.b64encode(barray).decode('utf-8') + self.transmission.add_torrent(filedump, timeout, **kwargs) def get_torrents(self): """Yields Torrents objects.""" diff --git a/handlers/message_handlers.py b/handlers/message_handlers.py index a4dd186..6fad8c7 100644 --- a/handlers/message_handlers.py +++ b/handlers/message_handlers.py @@ -1,4 +1,3 @@ -import base64 import logging from telegram.ext import Filters, MessageHandler @@ -13,9 +12,8 @@ def __init__(self, client): super().__init__(self.filters, self.callback) def callback(self, update, context): - torrent = update.message.document.get_file().download_as_bytearray() - filedump = base64.b64encode(torrent).decode('utf-8') - self.client.add_torrent(filedump) + torrent = update.message.document.get_file() + self.client.add_torrent(torrent) logging.info('Added new torrent file %s', update.message.document.file_name) context.bot.send_message(