Improved TR RPC client.

This commit is contained in:
AB
2020-08-14 11:54:20 +00:00
parent bf6bcf8264
commit c41022ce32
6 changed files with 63 additions and 25 deletions

View File

@ -105,13 +105,13 @@ class DataBase:
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"""
self.execute(sql, attrs)
def add_client(self, user_id, host, port):
sql = """INSERT OR REPLACE INTO tr_clients(user_id, host, port)
VALUES(?, ?, ?);"""
self.execute(sql, (user_id, host, port))
def add_client(self, user_id, scheme, hostname, port, username, password, path):
sql = """INSERT OR REPLACE INTO tr_clients(user_id, scheme, hostname, port, username, password, path)
VALUES(?, ?, ?, ?, ?, ?, ?);"""
self.execute(sql, (user_id, scheme, hostname, port, username, password, path))
def get_client(self, user_id):
sql = "SELECT host, port FROM tr_clients WHERE user_id = ?"
sql = "SELECT scheme, hostname, port, username, password, path FROM tr_clients WHERE user_id = ?"
return self.execute(sql, (user_id,))[0]
def get_attr(self, tor_id, attr):

View File

@ -79,16 +79,30 @@ def main():
u_id,
update.message.from_user.username)
try:
host, port = update.message.text.split(':')
host = host.split(' ')[1]
addr = update.message.text.split()[1]
log.info("Client Transmission RPC address - %s", addr)
tr = parse.urlparse(addr)
scheme = tr.scheme if tr.scheme else False
hostname = tr.hostname if tr.hostname else False
username = tr.username if tr.username else False
password = tr.password if tr.password else False
path = tr.path if tr.path else '/transmission/rpc'
port = tr.port if tr.port else (80 if scheme == 'http' else 443)
if not scheme or not hostname:
update.message.reply_text(
f'Can\'t understand : <b>{update.message.text}</b>. '
'Send transmission RPC address like <b>http(s)://[user:pass]host[:port][/rpc_path]</b>',
parse_mode='HTML',
disable_web_page_preview=True)
return
except:
update.message.reply_text(
'Send transmission RPC address like <b>host:port</b>',
'Gaspar is able to add new topics to your private Transmission server.'
'Send transmission RPC address like <b>http(s)://[user:pass]host[:port][/rpc_path]</b>',
parse_mode='HTML',
disable_web_page_preview=True)
return
torrent.db.add_client(u_id, host, port)
log.info(torrent.db.get_client(u_id))
torrent.db.add_client(u_id, scheme, hostname, port, username, password, path)
updater = Updater(token, use_context=True)

View File

@ -52,10 +52,17 @@ def update_watcher(bot):
subs = torrent.db.get_subscribers(alert['id'])
for sub in subs:
bot.sendMessage(sub, msg, parse_mode='HTML', disable_web_page_preview=True)
host, port = torrent.db.get_client(sub)
if host and port:
add_tor(host, port, torrent.meta['info_hash'])
time.sleep(10)
try:
scheme, hostname, port, username, password, path = torrent.db.get_client(sub)
if add_tor(scheme, hostname, port, username, password, path, torrent.meta['info_hash']):
log.info("Push update to client Transmission RPC for %s", torrent.meta['info_hash'])
else:
log.warning("Failed push update to client Transmission RPC for %s", torrent.meta['info_hash'])
except:
pass
time.sleep(1)
else:
log.info("There is no update for %s", alert['topic_title'])
time.sleep(UPDATE_INTERVAL)
update_thread = threading.Thread(target=__thread)
update_thread.start()

View File

@ -23,7 +23,8 @@ class Torrent:
def tor_id(self, tor_id):
self.__tor_id = tor_id
if tor_id:
self.get_tor_topic_data(tor_id)
return self.get_tor_topic_data(tor_id)
def get_tor_topic_data(self, tor_id):
data = dict()
@ -32,9 +33,13 @@ class Torrent:
self.api_url, tor_id)) as url:
data = json.loads(url.read().decode())
data = data["result"][tor_id]
data["id"] = tor_id
log.info("Getting info for [%s] %s%s", tor_id, data["topic_title"][:60], '...')
self.meta = data
try:
data["id"] = tor_id
log.info("Getting info for [%s] %s%s", tor_id, data["topic_title"][:60], '...')
self.meta = data
except TypeError:
log.warning("Tor_id %s fetch failed, maybe removed on server.", tor_id)
return False
def is_outdated(self):
if not self.tor_id:

View File

@ -38,9 +38,12 @@ CREATE TABLE IF NOT EXISTS "alerts" (
);
CREATE TABLE IF NOT EXISTS "tr_clients" (
user_id TEXT,
host TEXT,
scheme TEXT,
hostname TEXT,
port TEXT,
username TEXT,
password TEXT,
path TEXT,
UNIQUE(user_id)
);
COMMIT;

View File

@ -1,8 +1,17 @@
from transmission_rpc import Client
def add_tor(host, port, tor_hash):
c = Client(host=host, port=port)
m = f'magnet:?xt=urn:btih:{tor_hash}'
c.add_torrent(m)
def add_tor(scheme, hostname, port, username, password, path, tor_hash):
try:
c = Client(
host=hostname,
port=port,
username=username,
password=password,
protocol=scheme,
path=path)
m = f'magnet:?xt=urn:btih:{tor_hash}'
c.add_torrent(m)
return True
except:
return False