refactor: merge html_parser.py with extensions.py
All checks were successful
Build / Upload to production (push) Successful in 1m37s
All checks were successful
Build / Upload to production (push) Successful in 1m37s
Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
parent
d0357ff7c9
commit
511dc3e9b6
4 changed files with 84 additions and 74 deletions
98
commands.py
98
commands.py
|
@ -10,14 +10,20 @@ from telegram.ext import ContextTypes
|
|||
import html_parser
|
||||
import config
|
||||
from datetime import datetime, timedelta
|
||||
from extensions import get_json, format_rating, format_status, humanize_filesize
|
||||
from extensions import (
|
||||
get_json,
|
||||
format_rating,
|
||||
format_status,
|
||||
humanize_filesize,
|
||||
HtmlFormat,
|
||||
)
|
||||
|
||||
|
||||
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
await update.message.reply_text(
|
||||
"\n".join(
|
||||
[
|
||||
f"hello, i'm {html_parser.bold(context.bot.first_name)}, an inline image grabber.\n",
|
||||
f"hello, i'm {HtmlFormat.bold(context.bot.first_name)}, an inline image grabber.\n",
|
||||
"to get help, use /help",
|
||||
]
|
||||
),
|
||||
|
@ -29,9 +35,9 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
await update.message.reply_text(
|
||||
"\n".join(
|
||||
[
|
||||
html_parser.bold(f"how to use {context.bot.first_name}\n"),
|
||||
HtmlFormat.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>"),
|
||||
HtmlFormat.code(f"@{context.bot.username} <post ID>"),
|
||||
"2. click on the box that has popped up",
|
||||
"3. ???",
|
||||
"4. done!",
|
||||
|
@ -50,11 +56,11 @@ async def about_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> N
|
|||
await update.message.reply_text(
|
||||
"\n".join(
|
||||
[
|
||||
html_parser.bold(f"about {context.bot.first_name}"),
|
||||
HtmlFormat.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(config.app.name)} ({config.app.hostname})",
|
||||
HtmlFormat.bold("currently configured instance:"),
|
||||
f"{HtmlFormat.italic(config.app.name)} ({config.app.hostname})",
|
||||
]
|
||||
),
|
||||
parse_mode=ParseMode.HTML,
|
||||
|
@ -67,13 +73,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,
|
||||
"\n".join([html_parser.bold("Information"), "Fetching..."]),
|
||||
"\n".join([HtmlFormat.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(
|
||||
" ".join([html_parser.bold("Error:"), "That record was not found."]),
|
||||
" ".join([HtmlFormat.bold("Error:"), "That record was not found."]),
|
||||
update.effective_chat.id,
|
||||
message.message_id,
|
||||
parse_mode=ParseMode.HTML,
|
||||
|
@ -93,15 +99,15 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
m = [
|
||||
" ".join(["ID:", html_parser.code(post_data["id"])]),
|
||||
" ".join(["ID:", HtmlFormat.code(post_data["id"])]),
|
||||
" ".join(
|
||||
[
|
||||
"Uploader:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
uploader_data["name"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/users/{post_data['uploader_id']}",
|
||||
),
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
"»",
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts?tags=user:{uploader_data['name']}",
|
||||
),
|
||||
|
@ -113,7 +119,7 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Date:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
created_at.strftime("%Y-%m-%d %X (%z)"),
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts?tags=date:{created_at.strftime("%Y-%m-%d")}",
|
||||
),
|
||||
|
@ -126,11 +132,11 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Approver:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
approver_data["name"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/users/{post_data['approver_id']}",
|
||||
),
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
"»",
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts?tags=approver:{approver_data['name']}",
|
||||
),
|
||||
|
@ -141,12 +147,12 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Size:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.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']})",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
"»",
|
||||
f"{config.app.protocol}://{config.app.hostname}/media_assets/{post_data['media_asset']['id']}",
|
||||
),
|
||||
|
@ -163,7 +169,7 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Score:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
post_data["score"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/post_votes?search[post_id]={post_data['id']}&variant=compact",
|
||||
),
|
||||
|
@ -175,7 +181,7 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Favorites:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
post_data["fav_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts/{post_data['id']}/favorites",
|
||||
),
|
||||
|
@ -189,7 +195,7 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
link_preview_options = LinkPreviewOptions(url=post_data["large_file_url"])
|
||||
|
||||
await context.bot.edit_message_text(
|
||||
"\n".join([html_parser.bold("Information")] + m),
|
||||
"\n".join([HtmlFormat.bold("Information")] + m),
|
||||
update.effective_chat.id,
|
||||
message.message_id,
|
||||
parse_mode=ParseMode.HTML,
|
||||
|
@ -199,7 +205,7 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
except (IndexError, ValueError):
|
||||
await update.message.reply_text(
|
||||
" ".join(
|
||||
[html_parser.bold("Usage:"), html_parser.code("/info <post ID>")]
|
||||
[HtmlFormat.bold("Usage:"), HtmlFormat.code("/info <post ID>")]
|
||||
),
|
||||
parse_mode=ParseMode.HTML,
|
||||
)
|
||||
|
@ -210,13 +216,13 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
name_matches = context.args[0]
|
||||
message = await context.bot.send_message(
|
||||
update.effective_chat.id,
|
||||
"\n".join([html_parser.bold(name_matches), "Fetching..."]),
|
||||
"\n".join([HtmlFormat.bold(name_matches), "Fetching..."]),
|
||||
parse_mode=ParseMode.HTML,
|
||||
)
|
||||
user_search_data = get_json(f"users", [f"search[name_matches]={name_matches}"])
|
||||
if len(user_search_data) == 0:
|
||||
await context.bot.edit_message_text(
|
||||
" ".join([html_parser.bold("Error:"), "That record was not found."]),
|
||||
" ".join([HtmlFormat.bold("Error:"), "That record was not found."]),
|
||||
update.effective_chat.id,
|
||||
message.message_id,
|
||||
parse_mode=ParseMode.HTML,
|
||||
|
@ -235,7 +241,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
]
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
m = [" ".join(["ID:", html_parser.code(user_data["id"])])]
|
||||
m = [" ".join(["ID:", HtmlFormat.code(user_data["id"])])]
|
||||
created_at = datetime.fromisoformat(user_data["created_at"])
|
||||
m.append(" ".join(["Join Date:", created_at.strftime("%Y-%m-%d")]))
|
||||
m.append(" ".join(["Level:", user_data["level_string"]]))
|
||||
|
@ -243,7 +249,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Uploads:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["post_upload_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts?tags=user:{user_data['name']}",
|
||||
),
|
||||
|
@ -268,7 +274,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Favorites:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
fav_post_count,
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts?tags=ordfav:{user_data['name']}",
|
||||
),
|
||||
|
@ -286,16 +292,16 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Post Votes:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
vote_post_count,
|
||||
f"{config.app.protocol}://{config.app.hostname}/post_votes?search[user_name]={user_data['name']}",
|
||||
),
|
||||
f"({' '.join([
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
f"up:{upvote_post_count}",
|
||||
f"{config.app.protocol}://{config.app.hostname}/post_votes?search[user_name]={user_data['name']}&search[score]=1",
|
||||
),
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
f"down:{downvote_post_count}",
|
||||
f"{config.app.protocol}://{config.app.hostname}/post_votes?search[user_name]={user_data['name']}&search[score]=-1",
|
||||
),
|
||||
|
@ -307,7 +313,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Favorite Groups:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["favorite_group_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/favorite_groups?search[creator_name]={user_data['name']}",
|
||||
),
|
||||
|
@ -318,7 +324,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Post Changes:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["post_update_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/post_versions?search[updater_name]={user_data['name']}",
|
||||
),
|
||||
|
@ -332,12 +338,12 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Note Changes:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["note_update_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/note_versions?search[updater_name]={user_data['name']}",
|
||||
),
|
||||
"in",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
noteupdater_post_count,
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts?tags=noteupdater:{user_data['name']}+order:note",
|
||||
),
|
||||
|
@ -349,7 +355,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Wiki Page Changes:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["wiki_page_version_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/wiki_page_versions?search[updater_name]={user_data['name']}",
|
||||
),
|
||||
|
@ -360,7 +366,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Artist Changes:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["artist_version_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/artist_versions?search[updater_name]={user_data['name']}",
|
||||
),
|
||||
|
@ -371,7 +377,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Commentary Changes:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["artist_commentary_version_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/artist_commentary_versions?search[updater_name]={user_data['name']}",
|
||||
),
|
||||
|
@ -382,7 +388,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Forum Posts:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["forum_post_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/forum_posts?search[creator_name]={user_data['name']}",
|
||||
),
|
||||
|
@ -396,12 +402,12 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Comments:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["comment_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/comments?group_by=comment&search[creator_name]={user_data['name']}",
|
||||
),
|
||||
"in",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
commenter_post_count,
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts?tags=commenter:{user_data['name']}+order:comment_bumped",
|
||||
),
|
||||
|
@ -413,7 +419,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Appeals:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
user_data["appeal_count"],
|
||||
f"{config.app.protocol}://{config.app.hostname}/post_appeals?search[creator_name]={user_data['name']}",
|
||||
),
|
||||
|
@ -429,20 +435,20 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
" ".join(
|
||||
[
|
||||
"Feedback:",
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
total_feedback_count,
|
||||
f"{config.app.protocol}://{config.app.hostname}/user_feedbacks?search[user_name]={user_data['name']}",
|
||||
),
|
||||
f"({' '.join([
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
f"positive:{user_data['positive_feedback_count']}",
|
||||
f"{config.app.protocol}://{config.app.hostname}/user_feedbacks?search[user_name]={user_data['name']}&search[category]=positive",
|
||||
),
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
f"neutral:{user_data['neutral_feedback_count']}",
|
||||
f"{config.app.protocol}://{config.app.hostname}/user_feedbacks?search[user_name]={user_data['name']}&search[category]=neutral",
|
||||
),
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.hyperlink(
|
||||
f"negative:{user_data['negative_feedback_count']}",
|
||||
f"{config.app.protocol}://{config.app.hostname}/user_feedbacks?search[user_name]={user_data['name']}&search[category]=negative",
|
||||
),
|
||||
|
@ -451,7 +457,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
)
|
||||
)
|
||||
await context.bot.edit_message_text(
|
||||
"\n".join([html_parser.bold(user_data["name"])] + m),
|
||||
"\n".join([HtmlFormat.bold(user_data["name"])] + m),
|
||||
update.effective_chat.id,
|
||||
message.message_id,
|
||||
parse_mode=ParseMode.HTML,
|
||||
|
@ -461,7 +467,7 @@ async def user_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||
except (IndexError, ValueError):
|
||||
await update.message.reply_text(
|
||||
" ".join(
|
||||
[html_parser.bold("Usage:"), html_parser.code("/user <username>")]
|
||||
[HtmlFormat.bold("Usage:"), HtmlFormat.code("/user <username>")]
|
||||
),
|
||||
parse_mode=ParseMode.HTML,
|
||||
)
|
||||
|
|
|
@ -5,6 +5,28 @@ from typing import Any, Optional
|
|||
import config
|
||||
|
||||
|
||||
class HtmlFormat:
|
||||
@staticmethod
|
||||
def bold(s: str) -> str:
|
||||
return f"<b>{s}</b>"
|
||||
|
||||
@staticmethod
|
||||
def italic(s: str) -> str:
|
||||
return f"<i>{s}</i>"
|
||||
|
||||
@staticmethod
|
||||
def strikethrough(s: str) -> str:
|
||||
return f"<s>{s}</s>"
|
||||
|
||||
@staticmethod
|
||||
def hyperlink(s: str, href: str) -> str:
|
||||
return f"<a href='{href}'>{s}</a>"
|
||||
|
||||
@staticmethod
|
||||
def code(s: str) -> str:
|
||||
return f"<code>{s}</code>"
|
||||
|
||||
|
||||
def humanize_tags_from_json(value: str, default: str) -> str:
|
||||
if value != "":
|
||||
return ", ".join([re.sub("_\\(.*", "", t) for t in value.split()])
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
def bold(s: str) -> str:
|
||||
return f"<b>{s}</b>"
|
||||
|
||||
|
||||
def italic(s: str) -> str:
|
||||
return f"<i>{s}</i>"
|
||||
|
||||
|
||||
def strikethrough(s: str) -> str:
|
||||
return f"<s>{s}</s>"
|
||||
|
||||
|
||||
def hyperlink(s: str, href: str) -> str:
|
||||
return f"<a href='{href}'>{s}</a>"
|
||||
|
||||
|
||||
def code(s: str) -> str:
|
||||
return f"<code>{s}</code>"
|
|
@ -14,7 +14,7 @@ from telegram.ext import ContextTypes
|
|||
|
||||
import html_parser
|
||||
import config
|
||||
from extensions import humanize_tags_from_json, format_rating, get_json
|
||||
from extensions import humanize_tags_from_json, format_rating, get_json, HtmlFormat
|
||||
|
||||
|
||||
async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
|
@ -66,15 +66,15 @@ async def answer_query(
|
|||
input_message_content=InputTextMessageContent(
|
||||
" ".join(
|
||||
[
|
||||
html_parser.code(f"#{query}"),
|
||||
html_parser.strikethrough(
|
||||
HtmlFormat.code(f"#{query}"),
|
||||
HtmlFormat.strikethrough(
|
||||
" ".join(
|
||||
[
|
||||
html_parser.bold(
|
||||
HtmlFormat.bold(
|
||||
f"{characters} ({copyrights})"
|
||||
),
|
||||
"drawn by",
|
||||
html_parser.bold(artists),
|
||||
HtmlFormat.bold(artists),
|
||||
]
|
||||
)
|
||||
),
|
||||
|
@ -102,13 +102,13 @@ async def answer_query(
|
|||
input_message_content=InputTextMessageContent(
|
||||
" ".join(
|
||||
[
|
||||
html_parser.code(f"#{query}"),
|
||||
html_parser.hyperlink(
|
||||
HtmlFormat.code(f"#{query}"),
|
||||
HtmlFormat.hyperlink(
|
||||
" ".join(
|
||||
[
|
||||
html_parser.bold(f"{characters} ({copyrights})"),
|
||||
HtmlFormat.bold(f"{characters} ({copyrights})"),
|
||||
"drawn by",
|
||||
html_parser.bold(artists),
|
||||
HtmlFormat.bold(artists),
|
||||
]
|
||||
),
|
||||
f"{config.app.protocol}://{config.app.hostname}/posts/{query}",
|
||||
|
@ -133,7 +133,7 @@ async def invalid_query(update: Update, query: int) -> None:
|
|||
title=f"#{query}",
|
||||
description="Error",
|
||||
input_message_content=InputTextMessageContent(
|
||||
" ".join([html_parser.code(f"#{query}"), "That record was not found."]),
|
||||
" ".join([HtmlFormat.code(f"#{query}"), "That record was not found."]),
|
||||
parse_mode=ParseMode.HTML,
|
||||
),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue