Skip to content

Commit

Permalink
Add rTorrent backend
Browse files Browse the repository at this point in the history
  • Loading branch information
dolohow committed Apr 27, 2020
1 parent dda53b8 commit 88741e1
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 7 deletions.
93 changes: 93 additions & 0 deletions backends/rtorrent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import datetime
import math
import xmlrpc.client


class Client:

def __init__(self, config):
url = f'http://{config["host"]}:{config["port"]}/RPC2'
self.config = config
self.proxy = xmlrpc.client.ServerProxy(url, verbose=False)

def add_torrent(self, torrent, timeout=None, **kwargs):
self.proxy.load.start_verbose(
'',
torrent.file_path,
[f'd.custom1.set={self.config["set_label"]}'])

def get_torrents(self):
yield from map(lambda torrent: Torrent(torrent, self.proxy),
self.proxy.download_list())


class Torrent:

PROPERTIES = {
'chunk_size': 0,
'completed_bytes': 1,
'completed_chunks': 2,
'down.rate': 3,
'incomplete': 4,
'is_active': 5,
'is_open': 6,
'name': 7,
'peers_connected': 8,
'size_bytes': 9,
'size_chunks': 10,
}

def __init__(self, torrent, proxy):
self.torrent = torrent
self.proxy = proxy

self.multicall = xmlrpc.client.MultiCall(self.proxy)
for prop in self.PROPERTIES:
getattr(self.multicall, 'd.' + prop)(self.torrent)
self.update()

def is_downloading(self):
return (self.__get_prop('down.rate') and
self.__get_prop('is_open') and
self.__get_prop('is_active'))

def update(self):
try:
self.data = tuple(self.multicall())
except:
raise KeyError

@property
def download_rate(self):
return self.__get_prop('down.rate')

@property
def eta(self):
if self.__get_prop('down.rate') == 0:
return 'Unavailable'
chunks_left = (self.__get_prop('size_chunks') -
self.__get_prop('completed_chunks'))
down_rate = (chunks_left *
self.__get_prop('chunk_size') /
self.__get_prop('down.rate'))
return datetime.timedelta(seconds=math.floor(down_rate))

@property
def id(self):
return self.torrent

@property
def name(self):
return self.__get_prop('name')

@property
def peers_connected(self):
return self.__get_prop('peers_connected')

@property
def percent_done(self):
return (self.__get_prop('completed_bytes') /
self.__get_prop('size_bytes'))

def __get_prop(self, prop):
return self.data[self.PROPERTIES[prop]]
24 changes: 17 additions & 7 deletions tdpt.ini.template
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
[General]
# Specifies from which torrent client data should be fetched.
backend = Transmission
# When transmission is done with downloading it will execute command or
# script specified in this parameter. With the following environment
# Currently supported are
# * Transmission
# * rTorrent
# You must further uncomment section for that client down below.
#backend =
# When torrent client is done with downloading it will execute command
# or script specified in this parameter. With the following environment
# variables:
# TORRENT_NAME - name of the torrent that was downloaded
#call_on_finish =

[Transmission]
host = 127.0.0.1
port = 9091
#[Transmission]
#host = 127.0.0.1
#port = 9091

#[rTorrent]
#host = 127.0.0.1
#port = 8080
# Will set label to uploaded torrent visible in ruTorrent.
#set_label = tacajushi-telegram

[Telegram]
# You can obtain it by asking several question to @BotFather and
# creating your own bot
# creating your own bot.
bot_token =

# Every user or group has this parameter. If you don't know it ask
Expand Down

0 comments on commit 88741e1

Please sign in to comment.