feat: add /info command, pt. 1
# Conflicts: # extensions.py
This commit is contained in:
parent
c70d7fe3be
commit
b079d56250
4 changed files with 79 additions and 6 deletions
43
commands.py
43
commands.py
|
@ -4,6 +4,7 @@ from telegram.ext import ContextTypes
|
||||||
|
|
||||||
import html_parser
|
import html_parser
|
||||||
from config import *
|
from config import *
|
||||||
|
from extensions import get_json, format_rating, format_status
|
||||||
|
|
||||||
|
|
||||||
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
@ -45,3 +46,45 @@ async def about_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> N
|
||||||
parse_mode=ParseMode.HTML,
|
parse_mode=ParseMode.HTML,
|
||||||
reply_markup=reply_markup
|
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?
|
||||||
|
|
||||||
|
await context.bot.edit_message_text(
|
||||||
|
f"{html_parser.bold("Information")}\n"
|
||||||
|
f"ID: {html_parser.code(post_data['id'])}\n"
|
||||||
|
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']}")}\n"
|
||||||
|
f"Date: {post_data['created_at']}\n"
|
||||||
|
f"Size: {post_data['media_asset']['file_size']} .{post_data['media_asset']['file_ext']} "
|
||||||
|
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']}")}\n"
|
||||||
|
f"Source: {post_data['source']}\n"
|
||||||
|
f"Rating: {format_rating(post_data['rating'])}\n"
|
||||||
|
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']})\n"
|
||||||
|
f"Favorites: {html_parser.hyperlink(post_data['fav_count'],
|
||||||
|
f"http://{app.host}/posts/{post_data['id']}/favorites")}\n"
|
||||||
|
f"Status: {format_status(post_data)}",
|
||||||
|
update.effective_chat.id, message.message_id,
|
||||||
|
parse_mode=ParseMode.HTML)
|
||||||
|
except (IndexError, ValueError):
|
||||||
|
await update.message.reply_text(
|
||||||
|
f"{html_parser.bold("Usage")}: {html_parser.code(f"/info <post ID>")}",
|
||||||
|
parse_mode=ParseMode.HTML)
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
import re
|
import re
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
from config import app
|
||||||
|
|
||||||
|
|
||||||
def humanize_tags_from_json(value: str, default: str) -> str:
|
def humanize_tags_from_json(value: str, default: str) -> str:
|
||||||
|
@ -22,3 +26,31 @@ def format_rating(value: str) -> str | None:
|
||||||
# Negative Squared Latin Capital Letter E
|
# Negative Squared Latin Capital Letter E
|
||||||
return "🅴"
|
return "🅴"
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def format_status(data) -> str:
|
||||||
|
is_active = True
|
||||||
|
is_pending = data['is_pending']
|
||||||
|
is_flagged = data['is_flagged']
|
||||||
|
is_deleted = data['is_deleted']
|
||||||
|
is_banned = data['is_banned']
|
||||||
|
if is_pending | is_flagged | is_deleted:
|
||||||
|
is_active = False
|
||||||
|
|
||||||
|
status = []
|
||||||
|
if is_active:
|
||||||
|
status.append("Active")
|
||||||
|
if is_pending:
|
||||||
|
status.append("Pending")
|
||||||
|
if is_flagged:
|
||||||
|
status.append("Flagged")
|
||||||
|
if is_banned:
|
||||||
|
status.append("Banned")
|
||||||
|
return " ".join(status)
|
||||||
|
|
||||||
|
def get_json(pathname: str) -> Any | None:
|
||||||
|
r = requests.get(f"http://{app.host}/{pathname}.json")
|
||||||
|
|
||||||
|
if r.status_code != 200:
|
||||||
|
return None
|
||||||
|
return r.json()
|
||||||
|
|
|
@ -6,7 +6,7 @@ from telegram.constants import ParseMode
|
||||||
from telegram.ext import ContextTypes
|
from telegram.ext import ContextTypes
|
||||||
|
|
||||||
from config import *
|
from config import *
|
||||||
from extensions import humanize_tags_from_json, format_rating
|
from extensions import humanize_tags_from_json, format_rating, get_json
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyUnusedLocal
|
# noinspection PyUnusedLocal
|
||||||
|
@ -19,14 +19,11 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
||||||
if not query.isdigit():
|
if not query.isdigit():
|
||||||
return
|
return
|
||||||
|
|
||||||
response = requests.get(f"http://{app.host}/posts/{query}.json")
|
data = get_json(f"posts/{query}")
|
||||||
|
if data is None:
|
||||||
if response.status_code != 200:
|
|
||||||
await invalid_query(update, query)
|
await invalid_query(update, query)
|
||||||
return
|
return
|
||||||
|
|
||||||
data = response.json()
|
|
||||||
|
|
||||||
await answer_query(update, query, data)
|
await answer_query(update, query, data)
|
||||||
|
|
||||||
|
|
||||||
|
|
1
main.py
1
main.py
|
@ -21,6 +21,7 @@ def main() -> None:
|
||||||
application.add_handler(CommandHandler("start", commands.start_command))
|
application.add_handler(CommandHandler("start", commands.start_command))
|
||||||
application.add_handler(CommandHandler("help", commands.help_command))
|
application.add_handler(CommandHandler("help", commands.help_command))
|
||||||
application.add_handler(CommandHandler("about", commands.about_command))
|
application.add_handler(CommandHandler("about", commands.about_command))
|
||||||
|
application.add_handler(CommandHandler("info", commands.info_command))
|
||||||
|
|
||||||
from inline_query import inline_query
|
from inline_query import inline_query
|
||||||
application.add_handler(InlineQueryHandler(inline_query))
|
application.add_handler(InlineQueryHandler(inline_query))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue