feat: add /info command (#1)
Reviewed-on: #1 Co-authored-by: mctaylors <cantsendmails@mctaylors.ru> Co-committed-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
parent
c70d7fe3be
commit
6141d674a9
4 changed files with 115 additions and 7 deletions
71
commands.py
71
commands.py
|
@ -1,9 +1,11 @@
|
|||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, LinkPreviewOptions
|
||||
from telegram.constants import ParseMode
|
||||
from telegram.ext import ContextTypes
|
||||
|
||||
import html_parser
|
||||
from datetime import datetime
|
||||
from config import *
|
||||
from extensions import get_json, format_rating, format_status, humanize_filesize
|
||||
|
||||
|
||||
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
|
@ -45,3 +47,70 @@ async def about_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> N
|
|||
parse_mode=ParseMode.HTML,
|
||||
reply_markup=reply_markup
|
||||
)
|
||||
|
||||
|
||||
async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
try:
|
||||
post_id = context.args[0]
|
||||
message = await context.bot.send_message(update.effective_chat.id,
|
||||
f"{html_parser.bold("Information")}\n"
|
||||
f"Fetching...",
|
||||
parse_mode=ParseMode.HTML)
|
||||
post_data = get_json(f"posts/{post_id}")
|
||||
if post_data is None:
|
||||
await update.message.reply_text(
|
||||
f"{html_parser.bold("Error")}: That record was not found.",
|
||||
parse_mode=ParseMode.HTML)
|
||||
return
|
||||
uploader_data = get_json(f"users/{post_data['uploader_id']}")
|
||||
# well, we could check the uploader, but why would we do that?
|
||||
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton(f"Open in {app.name}",
|
||||
url=f"https://{app.hostname}/posts/{post_id}")
|
||||
]
|
||||
]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
# noinspection PyListCreation
|
||||
m = []
|
||||
m.append(f"ID: {html_parser.code(post_data['id'])}")
|
||||
m.append(f"Uploader: {html_parser.hyperlink(uploader_data['name'],
|
||||
f"http://{app.host}/users/{post_data['uploader_id']}")} "
|
||||
f"{html_parser.hyperlink("»", f"http://{app.host}/posts?tags=user:{uploader_data['name']}")}")
|
||||
created_at = datetime.fromisoformat(post_data['created_at'])
|
||||
m.append(f"Date: {html_parser.hyperlink(
|
||||
f"{created_at.strftime("%Y-%m-%d %X (%z)")}",
|
||||
f"http://{app.host}/posts?tags=date:{created_at.strftime("%Y-%m-%d")}")}")
|
||||
if post_data['approver_id'] is not None:
|
||||
approver_data = get_json(f"users/{post_data['approver_id']}")
|
||||
m.append(f"Approver: {html_parser.hyperlink(approver_data['name'],
|
||||
f"http://{app.host}/users/{post_data['approver_id']}")} "
|
||||
f"{html_parser.hyperlink("»", f"http://{app.host}/posts?tags=approver:{approver_data['name']}")}")
|
||||
m.append(f"Size: {html_parser.hyperlink(
|
||||
f"{humanize_filesize(post_data['media_asset']['file_size'])} .{post_data['media_asset']['file_ext']}",
|
||||
post_data['file_url'])} "
|
||||
f"({post_data['media_asset']['image_width']}x{post_data['media_asset']['image_height']}) "
|
||||
f"{html_parser.hyperlink("»", f"http://{app.host}/media_assets/{post_data['media_asset']['id']}")}")
|
||||
m.append(f"Source: {post_data['source'] if post_data['source'] != "" else "🚫"}")
|
||||
m.append(f"Rating: {format_rating(post_data['rating'])}")
|
||||
m.append(f"Score: {html_parser.hyperlink(post_data['score'],
|
||||
f"http://{app.host}/post_votes?search[post_id]={post_data['id']}&variant=compact")} "
|
||||
f"(+{post_data['up_score']} / -{post_data['down_score']})")
|
||||
m.append(f"Favorites: {html_parser.hyperlink(post_data['fav_count'],
|
||||
f"http://{app.host}/posts/{post_data['id']}/favorites")}")
|
||||
m.append(f"Status: {format_status(post_data)}")
|
||||
|
||||
link_preview_options = LinkPreviewOptions(True)
|
||||
if not post_data['is_banned']:
|
||||
link_preview_options = LinkPreviewOptions(url=post_data['large_file_url'])
|
||||
|
||||
await context.bot.edit_message_text(
|
||||
f"{html_parser.bold("Information")}\n" + "\n".join(m),
|
||||
update.effective_chat.id, message.message_id,
|
||||
parse_mode=ParseMode.HTML, reply_markup=reply_markup, link_preview_options=link_preview_options)
|
||||
except (IndexError, ValueError):
|
||||
await update.message.reply_text(
|
||||
f"{html_parser.bold("Usage")}: {html_parser.code(f"/info <post ID>")}",
|
||||
parse_mode=ParseMode.HTML)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue