From c41022ce32180d34c9101fc5867c401e15bf4dc6 Mon Sep 17 00:00:00 2001 From: AB Date: Fri, 14 Aug 2020 11:54:20 +0000 Subject: [PATCH] Improved TR RPC client. --- gaspar/database.py | 10 +++++----- gaspar/gaspar.py | 24 +++++++++++++++++++----- gaspar/notify.py | 15 +++++++++++---- gaspar/rutracker.py | 13 +++++++++---- gaspar/scheme.sql | 7 +++++-- gaspar/transmission.py | 19 ++++++++++++++----- 6 files changed, 63 insertions(+), 25 deletions(-) diff --git a/gaspar/database.py b/gaspar/database.py index 1bf398c..58581f8 100644 --- a/gaspar/database.py +++ b/gaspar/database.py @@ -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): diff --git a/gaspar/gaspar.py b/gaspar/gaspar.py index 632c2e8..55cf502 100644 --- a/gaspar/gaspar.py +++ b/gaspar/gaspar.py @@ -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 : {update.message.text}. ' + 'Send transmission RPC address like http(s)://[user:pass]host[:port][/rpc_path]', + parse_mode='HTML', + disable_web_page_preview=True) + return except: update.message.reply_text( - 'Send transmission RPC address like host:port', + 'Gaspar is able to add new topics to your private Transmission server.' + 'Send transmission RPC address like http(s)://[user:pass]host[:port][/rpc_path]', 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) diff --git a/gaspar/notify.py b/gaspar/notify.py index f672a7c..3de028c 100644 --- a/gaspar/notify.py +++ b/gaspar/notify.py @@ -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() diff --git a/gaspar/rutracker.py b/gaspar/rutracker.py index a514b90..79609dd 100644 --- a/gaspar/rutracker.py +++ b/gaspar/rutracker.py @@ -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: diff --git a/gaspar/scheme.sql b/gaspar/scheme.sql index eed7bba..316d513 100644 --- a/gaspar/scheme.sql +++ b/gaspar/scheme.sql @@ -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; - diff --git a/gaspar/transmission.py b/gaspar/transmission.py index aee024d..05636cf 100644 --- a/gaspar/transmission.py +++ b/gaspar/transmission.py @@ -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