Fixes. Added buttons for instant download.

This commit is contained in:
AB
2021-03-26 15:34:54 +03:00
parent 89dde3b658
commit 790c968c5b
6 changed files with 68 additions and 19 deletions

View File

@ -0,0 +1 @@
__version__ = "0.0.5"

View File

@ -35,7 +35,7 @@ class DataBase:
self.basefile = os.environ.get('TG_DB') if os.environ.get('TG_DB') else '/usr/share/gaspar/data.sqlite' self.basefile = os.environ.get('TG_DB') if os.environ.get('TG_DB') else '/usr/share/gaspar/data.sqlite'
try: try:
conn = self.connect() conn = self.connect()
log.info("Using '%s' base file.", os.path.realpath(self.basefile)) log.debig("Using '%s' base file.", os.path.realpath(self.basefile))
except: except:
log.debug('Could not connect to DataBase.') log.debug('Could not connect to DataBase.')
return None return None
@ -52,7 +52,7 @@ class DataBase:
else: else:
log.debug("Error! cannot create the database connection.") log.debug("Error! cannot create the database connection.")
raise DBInitException raise DBInitException
log.info('DB connected.') log.debug('DB connected.')
self.close(conn) self.close(conn)
def connect(self): def connect(self):
@ -62,7 +62,7 @@ class DataBase:
:type basefile: string :type basefile: string
:return: sqlite3 connect object :return: sqlite3 connect object
""" """
log.info("Open connection to %s", os.path.realpath(self.basefile)) log.debug("Open connection to %s", os.path.realpath(self.basefile))
return sqlite3.connect(self.basefile, check_same_thread=False) return sqlite3.connect(self.basefile, check_same_thread=False)
def execute(self, sql, params): def execute(self, sql, params):
@ -78,7 +78,8 @@ class DataBase:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(sql, params) cursor.execute(sql, params)
conn.commit() conn.commit()
result = cursor.fetchall() _result = cursor.fetchall()
result = _result if _result else cursor.lastrowid
self.close(conn) self.close(conn)
return result return result
@ -109,7 +110,7 @@ class DataBase:
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ? )""" ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"""
self.execute(sql, attrs) self.execute(sql, attrs)
def add_client(self, user_id, scheme, hostname, port, username, password, path): def add_client_rpc(self, user_id, scheme, hostname, port, username, password, path):
if check_connection(scheme, hostname, port, username, password, path): if check_connection(scheme, hostname, port, username, password, path):
sql = """INSERT OR REPLACE INTO tr_clients(user_id, scheme, hostname, port, username, password, path) sql = """INSERT OR REPLACE INTO tr_clients(user_id, scheme, hostname, port, username, password, path)
VALUES(?, ?, ?, ?, ?, ?, ?);""" VALUES(?, ?, ?, ?, ?, ?, ?);"""
@ -118,7 +119,7 @@ class DataBase:
else: else:
return False return False
def get_client(self, user_id): def get_client_rpc(self, user_id):
sql = "SELECT scheme, hostname, port, username, password, path FROM tr_clients WHERE user_id = ?" sql = "SELECT scheme, hostname, port, username, password, path FROM tr_clients WHERE user_id = ?"
res = self.execute(sql, (user_id,)) res = self.execute(sql, (user_id,))
if len(res): if len(res):
@ -126,7 +127,7 @@ class DataBase:
else: else:
return False return False
def drop_client(self, user_id): def drop_client_rpc(self, user_id):
sql = "DELETE FROM tr_clients WHERE user_id = ?" sql = "DELETE FROM tr_clients WHERE user_id = ?"
self.execute(sql, (user_id,)) self.execute(sql, (user_id,))
@ -174,7 +175,7 @@ class DataBase:
'topic_title', 'topic_title',
'seeder_last_seen' 'seeder_last_seen'
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""" ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""
self.execute(sql, ( row_id = self.execute(sql, (
tor_data["id"], tor_data["id"],
tor_data["info_hash"], tor_data["info_hash"],
tor_data["forum_id"], tor_data["forum_id"],
@ -186,6 +187,7 @@ class DataBase:
tor_data["topic_title"], tor_data["topic_title"],
tor_data["seeder_last_seen"], tor_data["seeder_last_seen"],
)) ))
return row_id
def delete_tor(self, user_id, tor_id): def delete_tor(self, user_id, tor_id):
sql = "DELETE FROM alerts WHERE user_id = ? AND tor_id = ?" sql = "DELETE FROM alerts WHERE user_id = ? AND tor_id = ?"

View File

@ -3,11 +3,13 @@ import os
import sys import sys
from urllib import parse from urllib import parse
from telegram.ext import Updater, MessageHandler, CommandHandler, filters from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Updater, MessageHandler, CommandHandler, filters, CallbackQueryHandler, CallbackContext
from .notify import update_watcher from .notify import update_watcher
from .rutracker import Torrent from .rutracker import Torrent
from .tools import format_topic from .tools import format_topic
from .transmission import easy_send
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
@ -49,7 +51,16 @@ def main():
torrent.meta['info_hash'], torrent.meta['info_hash'],
torrent.meta['reg_time'], torrent.meta['reg_time'],
pre='You will be alerted about\n') pre='You will be alerted about\n')
update.message.reply_text(msg, parse_mode='HTML', disable_web_page_preview=True) keyboard = []
if torrent.db.get_client_rpc(update.message.chat['id']):
keyboard.append([
InlineKeyboardButton("Add torrent to RPC client", callback_data=f"start_rpc.{torrent.meta['id']}"),
InlineKeyboardButton("Don't!", callback_data=f"close.{torrent.meta['id']}"), ],
)
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(msg, parse_mode='HTML', disable_web_page_preview=True, reply_markup=reply_markup)
def list_alerts(update, context): def list_alerts(update, context):
log.info( log.info(
@ -95,8 +106,7 @@ def main():
disable_web_page_preview=True) disable_web_page_preview=True)
return return
except: except:
tr_client = Torrent().db.get_client(u_id) tr_client = Torrent().db.get_client_rpc(u_id)
log.info(tr_client)
if tr_client: if tr_client:
tr_line = f"Your client: <code>{tr_client[0]}://{tr_client[1]}:{tr_client[2]}{tr_client[5]}</code>\n" \ tr_line = f"Your client: <code>{tr_client[0]}://{tr_client[1]}:{tr_client[2]}{tr_client[5]}</code>\n" \
r"/delete_client" r"/delete_client"
@ -110,7 +120,7 @@ def main():
disable_web_page_preview=True) disable_web_page_preview=True)
return return
if Torrent().db.add_client(u_id, scheme, hostname, port, username, password, path): if Torrent().db.add_client_rpc(u_id, scheme, hostname, port, username, password, path):
update.message.reply_text(f'Client reachable and saved.') update.message.reply_text(f'Client reachable and saved.')
else: else:
update.message.reply_text(f'Client unreachable.') update.message.reply_text(f'Client unreachable.')
@ -120,7 +130,7 @@ def main():
"Got /delete request from user [%s] %s", "Got /delete request from user [%s] %s",
update.message.chat['id'], update.message.chat['id'],
update.message.from_user.username) update.message.from_user.username)
Torrent().db.drop_client(update.message.chat['id']) Torrent().db.drop_client_rpc(update.message.chat['id'])
update.message.reply_text(f'Client deleted.') update.message.reply_text(f'Client deleted.')
def delete(update, context): def delete(update, context):
@ -135,6 +145,28 @@ def main():
except: except:
update.message.reply_text(f'Faled to delete {tor_id}') update.message.reply_text(f'Faled to delete {tor_id}')
def button(update: Update, context: CallbackContext) -> None:
query = update.callback_query
query.answer()
torrent_id = query.data.split('.')[1]
torrent = Torrent(torrent_id)
msg = format_topic(
torrent.meta['id'],
torrent.meta['topic_title'],
torrent.meta['size'],
torrent.meta['info_hash'],
torrent.meta['reg_time'],
pre='You will be alerted about\n')
if query.data.split('.')[0] == "close":
query.edit_message_text(text=f"{msg}", parse_mode='HTML',
disable_web_page_preview=True)
else:
easy_send(client_id=query.from_user, torent=torrent)
query.edit_message_text(text=f"{msg}📨 <b>Sent to RPC client</b>", parse_mode='HTML',
disable_web_page_preview=True)
updater = Updater(token, use_context=True) updater = Updater(token, use_context=True)
update_watcher(updater.bot) update_watcher(updater.bot)
@ -143,6 +175,7 @@ def main():
updater.dispatcher.add_handler(CommandHandler('delete_client', delete_client)) updater.dispatcher.add_handler(CommandHandler('delete_client', delete_client))
updater.dispatcher.add_handler(MessageHandler(filters.Filters.regex(r'/delete_'), delete)) updater.dispatcher.add_handler(MessageHandler(filters.Filters.regex(r'/delete_'), delete))
updater.dispatcher.add_handler(MessageHandler(filters.Filters.text, add)) updater.dispatcher.add_handler(MessageHandler(filters.Filters.text, add))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.start_polling() updater.start_polling()
updater.idle() updater.idle()

View File

@ -5,7 +5,7 @@ from datetime import datetime
from .rutracker import Torrent from .rutracker import Torrent
from .tools import format_topic from .tools import format_topic
from .transmission import add_tor from .transmission import send_to_client_rpc
UPDATE_INTERVAL = 2 * 60 * 60 # in secs. UPDATE_INTERVAL = 2 * 60 * 60 # in secs.
@ -57,8 +57,8 @@ def update_watcher(bot):
subs = torrent.db.get_subscribers(alert['id']) subs = torrent.db.get_subscribers(alert['id'])
for sub in subs: for sub in subs:
try: try:
scheme, hostname, port, username, password, path = torrent.db.get_client(sub) scheme, hostname, port, username, password, path = torrent.db.get_client_rpc(sub)
if add_tor(scheme, hostname, port, username, password, path, torrent.meta['info_hash']): if send_to_client_rpc(scheme, hostname, port, username, password, path, torrent.meta['info_hash']):
log.info("Push update to client Transmission RPC for %s", torrent.meta['info_hash']) log.info("Push update to client Transmission RPC for %s", torrent.meta['info_hash'])
msg = f"{msg}\n* Added to your Transmission: {scheme}://{hostname}:{port}/{path}" msg = f"{msg}\n* Added to your Transmission: {scheme}://{hostname}:{port}/{path}"
else: else:

View File

@ -1,7 +1,19 @@
from transmission_rpc import Client from transmission_rpc import Client
import logging
log = logging.getLogger(__name__)
def easy_send(torent, client_id):
try:
scheme, hostname, port, username, password, path = torent.db.get_client_rpc(client_id['id'])
if send_to_client_rpc(scheme, hostname, port, username, password, path, torent.meta['info_hash']):
log.info("Push update to client Transmission RPC for %s", torent.meta['topic_title'])
except Exception as e:
log.warning("Failed push update to client Transmission RPC for %s: %s",
torent.meta['topic_title'], e)
def add_tor(scheme, hostname, port, username, password, path, tor_hash): def send_to_client_rpc(scheme, hostname, port, username, password, path, tor_hash):
try: try:
c = Client( c = Client(
host=hostname, host=hostname,

View File

@ -1,5 +1,6 @@
import os, subprocess import os, subprocess
from setuptools import setup from setuptools import setup
from gaspar import __version__ as version
def read(fname): def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read() return open(os.path.join(os.path.dirname(__file__), fname)).read()
@ -16,7 +17,7 @@ def get_requires(rfile):
setup( setup(
name = "gaspar", name = "gaspar",
version = "0.0.5", version = version,
author = "UltraDesu", author = "UltraDesu",
author_email = "ultradesu@hexor.ru", author_email = "ultradesu@hexor.ru",
description = ("Telegram bot. Keep an eye on rutracker.org topics and let you " description = ("Telegram bot. Keep an eye on rutracker.org topics and let you "