style: use html_parser in inline_query.py
This commit is contained in:
parent
bd957e2bd2
commit
78db4c85db
1 changed files with 37 additions and 22 deletions
|
@ -1,10 +1,17 @@
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from telegram import Update, InlineKeyboardButton, InlineQueryResultArticle, InputTextMessageContent, \
|
from telegram import (
|
||||||
InlineKeyboardMarkup, LinkPreviewOptions
|
Update,
|
||||||
|
InlineKeyboardButton,
|
||||||
|
InlineQueryResultArticle,
|
||||||
|
InputTextMessageContent,
|
||||||
|
InlineKeyboardMarkup,
|
||||||
|
LinkPreviewOptions,
|
||||||
|
)
|
||||||
from telegram.constants import ParseMode
|
from telegram.constants import ParseMode
|
||||||
from telegram.ext import ContextTypes
|
from telegram.ext import ContextTypes
|
||||||
|
|
||||||
|
import html_parser
|
||||||
from config import *
|
from config import *
|
||||||
from extensions import humanize_tags_from_json, format_rating, get_json
|
from extensions import humanize_tags_from_json, format_rating, get_json
|
||||||
|
|
||||||
|
@ -28,22 +35,29 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
||||||
|
|
||||||
|
|
||||||
async def answer_query(update: Update, query: str, data) -> None:
|
async def answer_query(update: Update, query: str, data) -> None:
|
||||||
characters = humanize_tags_from_json(data['tag_string_character'], "no characters")
|
characters = humanize_tags_from_json(data["tag_string_character"], "no characters")
|
||||||
copyrights = humanize_tags_from_json(data['tag_string_copyright'], "unknown copyright")
|
copyrights = humanize_tags_from_json(
|
||||||
artists = humanize_tags_from_json(data['tag_string_artist'], "unknown artist")
|
data["tag_string_copyright"], "unknown copyright"
|
||||||
rating = format_rating(data['rating'])
|
)
|
||||||
|
artists = humanize_tags_from_json(data["tag_string_artist"], "unknown artist")
|
||||||
|
rating = format_rating(data["rating"])
|
||||||
|
|
||||||
if data['is_banned']:
|
if data["is_banned"]:
|
||||||
results = [
|
results = [
|
||||||
InlineQueryResultArticle(
|
InlineQueryResultArticle(
|
||||||
id=str(uuid4()),
|
id=str(uuid4()),
|
||||||
title=f"ID: {query}",
|
title=f"ID: {query}",
|
||||||
description=f"{characters} ({copyrights}) drawn by {artists}",
|
description=f"{characters} ({copyrights}) drawn by {artists}",
|
||||||
input_message_content=InputTextMessageContent(
|
input_message_content=InputTextMessageContent(
|
||||||
f"<code>#{query}</code> <s><b>{characters} ({copyrights})</b> drawn by <b>{artists}</b></s> {rating}\n"
|
html_parser.code(f"#{query}")
|
||||||
f"This post has been removed because of a takedown request or rule violation.",
|
+ html_parser.strikethrough(
|
||||||
parse_mode=ParseMode.HTML
|
f" {html_parser.bold(f"{characters} ({copyrights})")}"
|
||||||
)
|
f"drawn by {html_parser.bold(artists)}"
|
||||||
|
)
|
||||||
|
+ rating
|
||||||
|
+ "\nThis post has been removed because of a takedown request or rule violation.",
|
||||||
|
parse_mode=ParseMode.HTML,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -52,9 +66,10 @@ async def answer_query(update: Update, query: str, data) -> None:
|
||||||
|
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[
|
[
|
||||||
InlineKeyboardButton(f"Open in {app.name}",
|
InlineKeyboardButton(
|
||||||
url=f"http://{app.hostname}/posts/{query}"),
|
f"Open in {app.name}", url=f"http://{app.hostname}/posts/{query}"
|
||||||
InlineKeyboardButton("View original", url=data['file_url'])
|
),
|
||||||
|
InlineKeyboardButton("View original", url=data["file_url"]),
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -63,13 +78,14 @@ async def answer_query(update: Update, query: str, data) -> None:
|
||||||
id=str(uuid4()),
|
id=str(uuid4()),
|
||||||
title=f"ID: {query}",
|
title=f"ID: {query}",
|
||||||
description=f"{characters} ({copyrights}) drawn by {artists}",
|
description=f"{characters} ({copyrights}) drawn by {artists}",
|
||||||
thumbnail_url=data['preview_file_url'],
|
thumbnail_url=data["preview_file_url"],
|
||||||
input_message_content=InputTextMessageContent(
|
input_message_content=InputTextMessageContent(
|
||||||
f"<code>#{query}</code> <b>{characters} ({copyrights})</b> drawn by <b>{artists}</b> {rating}",
|
html_parser.code(f"#{query}")
|
||||||
|
+ f" {html_parser.bold(f"{characters} ({copyrights})")} drawn by {html_parser.bold(artists)} {rating}",
|
||||||
parse_mode=ParseMode.HTML,
|
parse_mode=ParseMode.HTML,
|
||||||
link_preview_options=LinkPreviewOptions(url=data['large_file_url'])
|
link_preview_options=LinkPreviewOptions(url=data["large_file_url"]),
|
||||||
),
|
),
|
||||||
reply_markup=InlineKeyboardMarkup(keyboard)
|
reply_markup=InlineKeyboardMarkup(keyboard),
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -83,10 +99,9 @@ async def invalid_query(update: Update, query: str) -> None:
|
||||||
title=f"ID: {query}",
|
title=f"ID: {query}",
|
||||||
description="Error",
|
description="Error",
|
||||||
input_message_content=InputTextMessageContent(
|
input_message_content=InputTextMessageContent(
|
||||||
f"<code>#{query}</code>\n"
|
html_parser.code(f"#{query}") + "That record was not found.",
|
||||||
f"That record was not found.",
|
parse_mode=ParseMode.HTML,
|
||||||
parse_mode=ParseMode.HTML
|
),
|
||||||
)
|
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue