add argparser for booru command
This commit is contained in:
parent
f8051314cc
commit
c65401abc7
3 changed files with 168 additions and 68 deletions
|
@ -1,10 +1,19 @@
|
|||
import requests
|
||||
from telegram import Update, InlineQueryResultPhoto
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes
|
||||
|
||||
from tgbot.config import STICKER_SHITHOLE, BOORU_API_URL
|
||||
from tgbot.shit.hentai import QueryParams
|
||||
from tgbot.config import STICKER_SHITHOLE, BOORU_API_URL, INLINE_QUERY_CACHE_SECONDS
|
||||
from tgbot.shit.hentai import parse_args, create_parser
|
||||
from tgbot.shit.render import render_text_on_image
|
||||
import re
|
||||
|
||||
|
||||
def escape_markdown(text):
|
||||
"""
|
||||
Escapes special characters for MarkdownV2 formatting.
|
||||
"""
|
||||
escape_chars = r"[_*[\]()~`>#+-=|{}.!]"
|
||||
return re.sub(f"([{escape_chars}])", r"\\\1", text)
|
||||
|
||||
|
||||
async def handle_xitter(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
|
@ -44,30 +53,62 @@ async def handle_red_ebalo(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||
|
||||
|
||||
async def handle_hentai(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
query = update.inline_query.query.replace("hentai", "").replace("—", "--")
|
||||
|
||||
error_response = [
|
||||
{
|
||||
"type": "article",
|
||||
"id": "1",
|
||||
"title": "ERROR OCCURRED! That means, at least one of us is a retard.",
|
||||
"description": "None",
|
||||
"input_message_content": {
|
||||
"message_text": "None"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
query = update.inline_query.query
|
||||
parser = create_parser()
|
||||
|
||||
try:
|
||||
params = QueryParams.from_query_str(query).__dict__
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
error_response[0]["description"] = "error parsing query"
|
||||
error_response[0]["input_message_content"]["message_text"] = "error parsing query"
|
||||
return await context.bot.answer_inline_query(update.inline_query.id, error_response)
|
||||
|
||||
response = requests.post(BOORU_API_URL, json=params)
|
||||
args = parse_args(query, parser)
|
||||
|
||||
except RuntimeError as e:
|
||||
|
||||
if getattr(parser, 'help_message', None):
|
||||
help_message = parser.help_message
|
||||
|
||||
escaped_help_message = escape_markdown(help_message)
|
||||
|
||||
help_response = [
|
||||
{
|
||||
"type": "article",
|
||||
"id": "1",
|
||||
"title": "Help message",
|
||||
"description": "Print help",
|
||||
"input_message_content": {
|
||||
"message_text": f"```\n{escaped_help_message}\n```",
|
||||
"parse_mode": "MarkdownV2",
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
return await context.bot.answer_inline_query(update.inline_query.id, help_response,
|
||||
cache_time=INLINE_QUERY_CACHE_SECONDS)
|
||||
|
||||
else:
|
||||
|
||||
error_message = str(e)
|
||||
|
||||
escaped_error_message = escape_markdown(error_message)
|
||||
|
||||
error_response = [
|
||||
{
|
||||
"type": "article",
|
||||
"id": "1",
|
||||
"title": "Invalid command!",
|
||||
"description": error_message,
|
||||
"input_message_content": {
|
||||
"message_text": f"```\n{escaped_error_message}\n```",
|
||||
"parse_mode": "MarkdownV2",
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
return await context.bot.answer_inline_query(update.inline_query.id, error_response,
|
||||
cache_time=INLINE_QUERY_CACHE_SECONDS)
|
||||
|
||||
print(args)
|
||||
|
||||
response = requests.post(BOORU_API_URL, json=args)
|
||||
|
||||
if response.status_code == 200:
|
||||
|
||||
|
@ -79,24 +120,51 @@ async def handle_hentai(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||
booru_post_url = post["booru"]["domain"] + \
|
||||
post["booru"]["site"]["api"]["postView"] + post["id"]
|
||||
|
||||
results.append(
|
||||
InlineQueryResultPhoto(
|
||||
id="%d" % idx,
|
||||
photo_url=post["fileUrl"],
|
||||
thumbnail_url=post["previewUrl"],
|
||||
title="IMAGE #%d" % idx,
|
||||
description="Image from booru",
|
||||
caption="source -> " + booru_post_url
|
||||
)
|
||||
)
|
||||
if not args["spoiler"]:
|
||||
|
||||
await context.bot.answer_inline_query(update.inline_query.id, results)
|
||||
results.append(
|
||||
{
|
||||
"type": "photo",
|
||||
"id": "%d" % idx,
|
||||
"photo_url": post["fileUrl"],
|
||||
"thumb_url": post["previewUrl"],
|
||||
"caption": "source -> " + booru_post_url
|
||||
}
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
results.append({
|
||||
"type": "photo",
|
||||
"id": "%d" % idx,
|
||||
"photo_url": post["fileUrl"],
|
||||
"thumb_url": post["previewUrl"],
|
||||
"caption": "source -> " + booru_post_url,
|
||||
"input_message_content": {
|
||||
"message_text": "image with tags = " + str(args["tags"]) + " -> " + booru_post_url,
|
||||
"has_spoiler": True
|
||||
}
|
||||
})
|
||||
|
||||
await context.bot.answer_inline_query(update.inline_query.id, results,
|
||||
cache_time=INLINE_QUERY_CACHE_SECONDS)
|
||||
|
||||
else:
|
||||
|
||||
print(response.text)
|
||||
error_response = [
|
||||
{
|
||||
"type": "article",
|
||||
"id": "1",
|
||||
"title": "ERROR!",
|
||||
"description": "None",
|
||||
"input_message_content": {
|
||||
"message_text": "None"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
error_response[0]["description"] = "booru error"
|
||||
error_response[0]["input_message_content"]["message_text"] = response.text
|
||||
|
||||
return await context.bot.answer_inline_query(update.inline_query.id, error_response)
|
||||
return await context.bot.answer_inline_query(update.inline_query.id, error_response,
|
||||
cache_time=INLINE_QUERY_CACHE_SECONDS)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue