Skip to content

Commit

Permalink
backends: Add deluge
Browse files Browse the repository at this point in the history
  • Loading branch information
dolohow committed Dec 28, 2020
1 parent af90c15 commit 4c67011
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
63 changes: 63 additions & 0 deletions tdpt/backends/deluge.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tdpt/backends/rtorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tdpt/backends/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions tdpt/handlers/message_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions tdpt/tdpt.ini.template
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4c67011

Please sign in to comment.