refactor: abuse str.join for more readability (#4)
All checks were successful
Build / Upload to production (push) Successful in 1m44s
All checks were successful
Build / Upload to production (push) Successful in 1m44s
Reviewed-on: #4 Co-authored-by: mctaylors <cantsendmails@mctaylors.ru> Co-committed-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
parent
fff77f51f8
commit
6ccf2ace4a
1 changed files with 105 additions and 40 deletions
141
commands.py
141
commands.py
|
@ -15,20 +15,28 @@ from extensions import get_json, format_rating, format_status, humanize_filesize
|
|||
|
||||
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
await update.message.reply_text(
|
||||
f"hello, i'm {html_parser.bold(context.bot.first_name)}, an inline image grabber.\n\n"
|
||||
f"to get help, use /help",
|
||||
"\n".join(
|
||||
[
|
||||
f"hello, i'm {html_parser.bold(context.bot.first_name)}, an inline image grabber.\n",
|
||||
"to get help, use /help",
|
||||
]
|
||||
),
|
||||
parse_mode=ParseMode.HTML,
|
||||
)
|
||||
|
||||
|
||||
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
await update.message.reply_text(
|
||||
f"{html_parser.bold(f"how to use {context.bot.first_name}")}\n\n"
|
||||
f"1. open your message box and type:\n"
|
||||
f"{html_parser.code(f"@{context.bot.username} <post ID>")}\n"
|
||||
f"2. click on the box that has popped up\n"
|
||||
f"3. ???\n"
|
||||
f"4. done!",
|
||||
"\n".join(
|
||||
[
|
||||
html_parser.bold(f"how to use {context.bot.first_name}\n"),
|
||||
"1. open your message box and type:",
|
||||
html_parser.code(f"@{context.bot.username} <post ID>"),
|
||||
"2. click on the box that has popped up",
|
||||
"3. ???",
|
||||
"4. done!",
|
||||
]
|
||||
),
|
||||
parse_mode=ParseMode.HTML,
|
||||
)
|
||||
|
||||
|
@ -40,11 +48,15 @@ async def about_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> N
|
|||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
await update.message.reply_text(
|
||||
f"{html_parser.bold(f"about {context.bot.first_name}")}\n"
|
||||
f"{context.bot.first_name} is an inline image grabber written in python that grabs images from Danbooru "
|
||||
f"(or other similar services).\n\n"
|
||||
f"{html_parser.bold("currently configured instance:")}\n"
|
||||
"\n".join(
|
||||
[
|
||||
html_parser.bold(f"about {context.bot.first_name}"),
|
||||
f"{context.bot.first_name} is an inline image grabber written in python that grabs images from "
|
||||
"Danbooru (or other similar services).\n",
|
||||
html_parser.bold("currently configured instance:"),
|
||||
f"{html_parser.italic(app.name)} ({app.hostname})",
|
||||
]
|
||||
),
|
||||
parse_mode=ParseMode.HTML,
|
||||
reply_markup=reply_markup,
|
||||
)
|
||||
|
@ -55,13 +67,13 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
post_id = context.args[0]
|
||||
message = await context.bot.send_message(
|
||||
update.effective_chat.id,
|
||||
f"{html_parser.bold("Information")}\n" f"Fetching...",
|
||||
"\n".join([html_parser.bold("Information"), "Fetching..."]),
|
||||
parse_mode=ParseMode.HTML,
|
||||
)
|
||||
post_data = get_json(f"posts/{post_id}")
|
||||
if post_data is None:
|
||||
await context.bot.edit_message_text(
|
||||
f"{html_parser.bold("Error")}: That record was not found.",
|
||||
" ".join([html_parser.bold("Error:"), "That record was not found."]),
|
||||
update.effective_chat.id,
|
||||
message.message_id,
|
||||
parse_mode=ParseMode.HTML,
|
||||
|
@ -81,52 +93,103 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
m = [
|
||||
f"ID: {html_parser.code(post_data['id'])}",
|
||||
f"Uploader: {html_parser.hyperlink(uploader_data['name'],
|
||||
f"{app.protocol}://{app.hostname}/users/{post_data['uploader_id']}")} "
|
||||
f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/posts?tags=user:{uploader_data['name']}")}",
|
||||
" ".join(["ID:", html_parser.code(post_data["id"])]),
|
||||
" ".join(
|
||||
[
|
||||
"Uploader:",
|
||||
html_parser.hyperlink(
|
||||
uploader_data["name"],
|
||||
f"{app.protocol}://{app.hostname}/users/{post_data['uploader_id']}",
|
||||
),
|
||||
html_parser.hyperlink(
|
||||
"»",
|
||||
f"{app.protocol}://{app.hostname}/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"{app.protocol}://{app.hostname}/posts?tags=date:{created_at.strftime("%Y-%m-%d")}")}"
|
||||
" ".join(
|
||||
[
|
||||
"Date:",
|
||||
html_parser.hyperlink(
|
||||
created_at.strftime("%Y-%m-%d %X (%z)"),
|
||||
f"{app.protocol}://{app.hostname}/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"{app.protocol}://{app.hostname}/users/{post_data['approver_id']}")} "
|
||||
f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/posts?tags=approver:{approver_data['name']}")}"
|
||||
" ".join(
|
||||
[
|
||||
"Approver:",
|
||||
html_parser.hyperlink(
|
||||
approver_data["name"],
|
||||
f"{app.protocol}://{app.hostname}/users/{post_data['approver_id']}",
|
||||
),
|
||||
html_parser.hyperlink(
|
||||
"»",
|
||||
f"{app.protocol}://{app.hostname}/posts?tags=approver:{approver_data['name']}",
|
||||
),
|
||||
]
|
||||
)
|
||||
)
|
||||
m.append(
|
||||
f"Size: {html_parser.hyperlink(
|
||||
" ".join(
|
||||
[
|
||||
"Size:",
|
||||
html_parser.hyperlink(
|
||||
f"{humanize_filesize(post_data['media_asset']['file_size'])} .{post_data['media_asset']['file_ext']}",
|
||||
"" if post_data['is_banned'] else post_data['file_url'])} "
|
||||
f"({post_data['media_asset']['image_width']}x{post_data['media_asset']['image_height']}) "
|
||||
f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/media_assets/{post_data['media_asset']['id']}")}"
|
||||
"" if post_data["is_banned"] else post_data["file_url"],
|
||||
),
|
||||
f"({post_data['media_asset']['image_width']}x{post_data['media_asset']['image_height']})",
|
||||
html_parser.hyperlink(
|
||||
"»",
|
||||
f"{app.protocol}://{app.hostname}/media_assets/{post_data['media_asset']['id']}",
|
||||
),
|
||||
]
|
||||
)
|
||||
)
|
||||
m.append(
|
||||
f"Source: {post_data['source'] if post_data['source'] != "" else "🚫"}"
|
||||
" ".join(
|
||||
["Source:", post_data["source"] if post_data["source"] != "" else "🚫"]
|
||||
)
|
||||
m.append(f"Rating: {format_rating(post_data['rating'])}")
|
||||
)
|
||||
m.append(" ".join(["Rating:", format_rating(post_data["rating"])]))
|
||||
m.append(
|
||||
f"Score: {html_parser.hyperlink(post_data['score'],
|
||||
f"{app.protocol}://{app.hostname}/post_votes?search[post_id]={post_data['id']}&variant=compact")} "
|
||||
f"(+{post_data['up_score']} / -{post_data['down_score']})"
|
||||
" ".join(
|
||||
[
|
||||
"Score:",
|
||||
html_parser.hyperlink(
|
||||
post_data["score"],
|
||||
f"{app.protocol}://{app.hostname}/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"{app.protocol}://{app.hostname}/posts/{post_data['id']}/favorites")}"
|
||||
" ".join(
|
||||
[
|
||||
"Favorites:",
|
||||
html_parser.hyperlink(
|
||||
post_data["fav_count"],
|
||||
f"{app.protocol}://{app.hostname}/posts/{post_data['id']}/favorites",
|
||||
),
|
||||
]
|
||||
)
|
||||
m.append(f"Status: {format_status(post_data)}")
|
||||
)
|
||||
m.append(" ".join(["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),
|
||||
"\n".join([html_parser.bold("Information")] + m),
|
||||
update.effective_chat.id,
|
||||
message.message_id,
|
||||
parse_mode=ParseMode.HTML,
|
||||
|
@ -135,6 +198,8 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
)
|
||||
except (IndexError, ValueError):
|
||||
await update.message.reply_text(
|
||||
f"{html_parser.bold("Usage")}: {html_parser.code(f"/info <post ID>")}",
|
||||
" ".join(
|
||||
[html_parser.bold("Usage:"), html_parser.code("/info <post ID>")]
|
||||
),
|
||||
parse_mode=ParseMode.HTML,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue