Add transmision client support.

This commit is contained in:
AB
2020-06-07 17:47:27 +00:00
parent e50415b10f
commit b044d6e184
5 changed files with 27 additions and 4 deletions

View File

@ -105,6 +105,15 @@ 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 get_client(self, user_id):
sql = "SELECT host, port FROM tr_clients WHERE user_id = ?"
return self.execute(sql, (user_id,))[0]
def get_attr(self, tor_id, attr):
sql = """SELECT %s FROM torrents WHERE id = ? ORDER BY reg_time DESC LIMIT 1""" % attr
return self.execute(sql, (tor_id,))[0][0]

View File

@ -10,7 +10,7 @@ from .database import DataBase
from .tools import format_topic
logging.basicConfig(
level=logging.DEBUG,
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log = logging.getLogger(__name__)
@ -73,17 +73,22 @@ def main():
update.message.reply_text(msg, parse_mode='HTML', disable_web_page_preview=True)
def handle_client(update, context):
u_id = update.message.chat['id']
log.info(
"Got /client request from user [%s] %s",
update.message.chat['id'],
u_id,
update.message.from_user.username)
try:
host, port = update.message.text.split(':')
host = host.split(' ')[1]
except:
update.message.reply_text(
'Send transmission RPC address like <b>host:port</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))
updater = Updater(token, use_context=True)

View File

@ -52,6 +52,9 @@ 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)
time.sleep(UPDATE_INTERVAL)
update_thread = threading.Thread(target=__thread)

View File

@ -36,5 +36,11 @@ CREATE TABLE IF NOT EXISTS "alerts" (
tor_id TEXT,
UNIQUE(user_id, tor_id)
);
CREATE TABLE IF NOT EXISTS "tr_clients" (
user_id TEXT,
host TEXT,
port TEXT,
UNIQUE(user_id)
);
COMMIT;

View File

@ -1,7 +1,7 @@
from transmission_rpc import Client
def add_tor(tor_hash):
c = Client(host='msk.hexor.ru', port=80)
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)