mirror of
https://github.com/house-of-vanity/gaspar.git
synced 2025-07-06 18:24:07 +00:00
Add /list pagination.
This commit is contained in:
@ -1,2 +1,2 @@
|
|||||||
import gaspar
|
import gaspar
|
||||||
__version__ = "0.2.1"
|
__version__ = "0.2.2"
|
||||||
|
@ -63,24 +63,67 @@ def main():
|
|||||||
update.message.reply_text(msg, parse_mode='HTML', disable_web_page_preview=True, reply_markup=reply_markup)
|
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):
|
||||||
|
per_page = 5
|
||||||
|
# log.info("list_alerts update.callback_query: %s", update.callback_query.message)
|
||||||
|
try:
|
||||||
|
query = int(update.callback_query.data.split('.')[-1])
|
||||||
|
chat_id = update.callback_query.message.chat['id']
|
||||||
|
username = update.callback_query.message.chat['username']
|
||||||
|
reply = True
|
||||||
|
except:
|
||||||
|
query = 0
|
||||||
|
username = update.message.from_user.username
|
||||||
|
chat_id = update.message.chat['id']
|
||||||
|
reply = False
|
||||||
log.info(
|
log.info(
|
||||||
"Got /list request from user [%s] %s",
|
"Got /list request from user [%s] %s",
|
||||||
update.message.chat['id'],
|
chat_id,
|
||||||
update.message.from_user.username)
|
username)
|
||||||
alerts = Torrent().db.get_alerts(update.message.chat['id'])
|
alerts = Torrent().db.get_alerts(chat_id)
|
||||||
if len(alerts) == 0:
|
if len(alerts) == 0:
|
||||||
update.message.reply_text("You have no configured alerts.")
|
update.message.reply_text("You have no configured alerts.")
|
||||||
return True
|
return True
|
||||||
msg = "<b>Configured alerts:</b>\n"
|
msg = f"<b>Configured {len(alerts)} alerts:</b>\n"
|
||||||
for alert in alerts:
|
log.info("interval: %s:%s", query, per_page+query)
|
||||||
|
item_num = query + 1
|
||||||
|
for alert in alerts[query:per_page+query]:
|
||||||
msg += format_topic(
|
msg += format_topic(
|
||||||
alert['id'],
|
alert['id'],
|
||||||
alert['topic_title'],
|
alert['topic_title'],
|
||||||
alert['size'],
|
alert['size'],
|
||||||
alert['info_hash'],
|
alert['info_hash'],
|
||||||
alert['reg_time'],
|
alert['reg_time'],
|
||||||
pre="\n")
|
pre="\n",
|
||||||
update.message.reply_text(msg, parse_mode='HTML', disable_web_page_preview=True)
|
item_num=item_num)
|
||||||
|
item_num += 1
|
||||||
|
log.info("list_alerts: %s", len(msg))
|
||||||
|
if len(alerts) > 5:
|
||||||
|
if len(alerts[query:per_page+query]) < per_page:
|
||||||
|
keyboard = [
|
||||||
|
[
|
||||||
|
InlineKeyboardButton("<", callback_data=f"list.{query-per_page}"),
|
||||||
|
]
|
||||||
|
]
|
||||||
|
elif query == 0:
|
||||||
|
keyboard = [
|
||||||
|
[
|
||||||
|
InlineKeyboardButton(">", callback_data=f"list.{query+per_page}"),
|
||||||
|
]
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
keyboard = [
|
||||||
|
[
|
||||||
|
InlineKeyboardButton("<", callback_data=f"list.{query-per_page}"),
|
||||||
|
InlineKeyboardButton(">", callback_data=f"list.{query+per_page}"),
|
||||||
|
]
|
||||||
|
]
|
||||||
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
if reply:
|
||||||
|
update.callback_query.edit_message_text(msg, reply_markup=reply_markup, parse_mode='HTML', disable_web_page_preview=True)
|
||||||
|
else:
|
||||||
|
update.message.reply_text(msg, reply_markup=reply_markup, parse_mode='HTML', disable_web_page_preview=True)
|
||||||
|
else:
|
||||||
|
update.message.reply_text(msg, parse_mode='HTML', disable_web_page_preview=True)
|
||||||
|
|
||||||
def handle_client(update, context):
|
def handle_client(update, context):
|
||||||
u_id = update.message.chat['id']
|
u_id = update.message.chat['id']
|
||||||
@ -135,32 +178,36 @@ def main():
|
|||||||
|
|
||||||
def button(update: Update, context: CallbackContext) -> None:
|
def button(update: Update, context: CallbackContext) -> None:
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
|
log.info("button: %s", query.data)
|
||||||
u_id = query.message.chat['id']
|
u_id = query.message.chat['id']
|
||||||
|
if any(w in query.data for w in ['close', 'start_rpc']):
|
||||||
|
torrent_id = query.data.split('.')[1]
|
||||||
|
torrent = Torrent(torrent_id)
|
||||||
|
|
||||||
torrent_id = query.data.split('.')[1]
|
msg = format_topic(
|
||||||
torrent = Torrent(torrent_id)
|
torrent.meta['id'],
|
||||||
|
torrent.meta['topic_title'],
|
||||||
msg = format_topic(
|
torrent.meta['size'],
|
||||||
torrent.meta['id'],
|
torrent.meta['info_hash'],
|
||||||
torrent.meta['topic_title'],
|
torrent.meta['reg_time'],
|
||||||
torrent.meta['size'],
|
pre='You will be alerted about\n')
|
||||||
torrent.meta['info_hash'],
|
if query.data.split('.')[0] == "close":
|
||||||
torrent.meta['reg_time'],
|
query.answer()
|
||||||
pre='You will be alerted about\n')
|
query.edit_message_text(text=f"{msg}", parse_mode='HTML',
|
||||||
if query.data.split('.')[0] == "close":
|
disable_web_page_preview=True)
|
||||||
|
elif query.data.split('.')[0] == "start_rpc":
|
||||||
|
query.answer()
|
||||||
|
easy_send(user_id=u_id, torrent_hash=torrent.meta['info_hash'])
|
||||||
|
query.edit_message_text(text=f"{msg}📨 <b>Sent to RPC /client</b>", parse_mode='HTML',
|
||||||
|
disable_web_page_preview=True)
|
||||||
|
if 'list.' in query.data:
|
||||||
query.answer()
|
query.answer()
|
||||||
query.edit_message_text(text=f"{msg}", parse_mode='HTML',
|
list_alerts(update, context)
|
||||||
disable_web_page_preview=True)
|
|
||||||
else:
|
|
||||||
easy_send(user_id=u_id, torrent_hash=torrent.meta['info_hash'])
|
|
||||||
query.answer()
|
|
||||||
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)
|
||||||
|
|
||||||
updater.dispatcher.add_handler(CommandHandler('list', list_alerts))
|
updater.dispatcher.add_handler(MessageHandler(filters.Filters.regex(r'/list'), list_alerts))
|
||||||
updater.dispatcher.add_handler(CommandHandler('client', handle_client))
|
updater.dispatcher.add_handler(CommandHandler('client', handle_client))
|
||||||
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))
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
def format_topic(tor_id, topic_title, size, info_hash, reg_time, pre=''):
|
def format_topic(tor_id, topic_title, size, info_hash, reg_time, pre='', item_num=False):
|
||||||
def sizeof_fmt(num, suffix='B'):
|
def sizeof_fmt(num, suffix='B'):
|
||||||
num = int(num)
|
num = int(num)
|
||||||
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
|
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
|
||||||
@ -13,7 +13,11 @@ def format_topic(tor_id, topic_title, size, info_hash, reg_time, pre=''):
|
|||||||
size = sizeof_fmt(size)
|
size = sizeof_fmt(size)
|
||||||
reg_time = datetime.utcfromtimestamp(int(reg_time)
|
reg_time = datetime.utcfromtimestamp(int(reg_time)
|
||||||
).strftime('%b-%d-%Y')
|
).strftime('%b-%d-%Y')
|
||||||
msg = f"""{pre}<a href='https://rutracker.org/forum/viewtopic.php?t={tor_id}'><b>{topic_title}</b></a>
|
if item_num:
|
||||||
|
item_num = f"[{item_num}] "
|
||||||
|
else:
|
||||||
|
item_num = ''
|
||||||
|
msg = f"""{pre}<a href='https://rutracker.org/forum/viewtopic.php?t={tor_id}'><b>{item_num}{topic_title}</b></a>
|
||||||
<b>💿 Size:</b> <code>{size}</code>
|
<b>💿 Size:</b> <code>{size}</code>
|
||||||
<b>#️⃣ Hash:</b> <code>{info_hash}</code>
|
<b>#️⃣ Hash:</b> <code>{info_hash}</code>
|
||||||
<b>📅 Updated:</b> <code>{reg_time}</code>
|
<b>📅 Updated:</b> <code>{reg_time}</code>
|
||||||
|
Reference in New Issue
Block a user