Skip to content

Commit

Permalink
Pass whole File object to Client
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dolohow committed Apr 27, 2020
1 parent 28fe00a commit dda53b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 11 additions & 1 deletion backends/transmission.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import base64

import transmissionrpc


Expand All @@ -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."""
Expand Down
6 changes: 2 additions & 4 deletions handlers/message_handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
import logging

from telegram.ext import Filters, MessageHandler
Expand All @@ -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(
Expand Down

0 comments on commit dda53b8

Please sign in to comment.