feat: add /info command, pt. 3
This commit is contained in:
parent
270e1630d4
commit
77f054f3c5
2 changed files with 18 additions and 3 deletions
12
commands.py
12
commands.py
|
@ -3,8 +3,9 @@ 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
|
||||
from extensions import get_json, format_rating, format_status, humanize_filesize
|
||||
|
||||
|
||||
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
|
@ -78,13 +79,18 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
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']}")}")
|
||||
m.append(f"Date: {post_data['created_at']}")
|
||||
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: {post_data['media_asset']['file_size']} .{post_data['media_asset']['file_ext']} "
|
||||
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 "🚫"}")
|
||||
|
|
|
@ -11,6 +11,14 @@ def humanize_tags_from_json(value: str, default: str) -> str:
|
|||
return default
|
||||
|
||||
|
||||
def humanize_filesize(value: int) -> str:
|
||||
for unit in ['B', 'KiB', 'MiB', 'GiB']:
|
||||
if value < 1024 or unit == 'GiB':
|
||||
break
|
||||
value /= 1024
|
||||
return f"{value:.2f} {unit}"
|
||||
|
||||
|
||||
def format_rating(value: str) -> str | None:
|
||||
match value:
|
||||
case "g":
|
||||
|
@ -48,6 +56,7 @@ def format_status(data) -> str:
|
|||
status.append("Banned")
|
||||
return " ".join(status)
|
||||
|
||||
|
||||
def get_json(pathname: str) -> Any | None:
|
||||
r = requests.get(f"http://{app.host}/{pathname}.json")
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue