Compare commits
10 commits
4adfcd316f
...
c70d7fe3be
Author | SHA1 | Date | |
---|---|---|---|
c70d7fe3be | |||
f07a51f76a | |||
df1735ef36 | |||
0cc77dd42c | |||
d646920210 | |||
0ee018e2ce | |||
7fdf18b5cb | |||
ff86c78b21 | |||
05cbca7509 | |||
b503234b85 |
5 changed files with 31 additions and 20 deletions
|
@ -4,7 +4,7 @@ from typing import Optional
|
|||
import requests
|
||||
|
||||
_c = configparser.ConfigParser()
|
||||
_c.read("config.ini")
|
||||
_c.read("config.ini", "utf-8")
|
||||
|
||||
|
||||
class _General:
|
||||
|
|
|
@ -1,20 +1,13 @@
|
|||
import re
|
||||
|
||||
|
||||
def humanize_tags_from_json(value: str, default: str):
|
||||
def humanize_tags_from_json(value: str, default: str) -> str:
|
||||
if value != "":
|
||||
output = str()
|
||||
tags = value.split()
|
||||
|
||||
for t in tags:
|
||||
output += f"{re.sub('_\\(.*', '', t)}, "
|
||||
output = output[:-2]
|
||||
|
||||
return output
|
||||
return ", ".join([re.sub('_\\(.*', '', t) for t in value.split()])
|
||||
return default
|
||||
|
||||
|
||||
def format_rating(value: str):
|
||||
def format_rating(value: str) -> str | None:
|
||||
match value:
|
||||
case "g":
|
||||
# Negative Squared Latin Capital Letter G
|
||||
|
@ -28,3 +21,4 @@ def format_rating(value: str):
|
|||
case "e":
|
||||
# Negative Squared Latin Capital Letter E
|
||||
return "🅴"
|
||||
return None
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
from uuid import uuid4
|
||||
|
||||
import requests
|
||||
from telegram import Update, InlineKeyboardButton, InlineQueryResultArticle, InputTextMessageContent, \
|
||||
InlineKeyboardMarkup
|
||||
InlineKeyboardMarkup, LinkPreviewOptions
|
||||
from telegram.constants import ParseMode
|
||||
from telegram.ext import ContextTypes
|
||||
|
||||
|
@ -36,6 +35,24 @@ async def answer_query(update: Update, query: str, data) -> None:
|
|||
copyrights = humanize_tags_from_json(data['tag_string_copyright'], "unknown copyright")
|
||||
artists = humanize_tags_from_json(data['tag_string_artist'], "unknown artist")
|
||||
rating = format_rating(data['rating'])
|
||||
|
||||
if data['is_banned']:
|
||||
results = [
|
||||
InlineQueryResultArticle(
|
||||
id=str(uuid4()),
|
||||
title=f"ID: {query}",
|
||||
description=f"{characters} ({copyrights}) drawn by {artists}",
|
||||
input_message_content=InputTextMessageContent(
|
||||
f"<code>#{query}</code> <s><b>{characters} ({copyrights})</b> drawn by <b>{artists}</b></s> {rating}\n"
|
||||
f"This post has been removed because of a takedown request or rule violation.",
|
||||
parse_mode=ParseMode.HTML
|
||||
)
|
||||
)
|
||||
]
|
||||
|
||||
await update.inline_query.answer(results)
|
||||
return
|
||||
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton(f"Open in {app.name}",
|
||||
|
@ -51,10 +68,9 @@ async def answer_query(update: Update, query: str, data) -> None:
|
|||
description=f"{characters} ({copyrights}) drawn by {artists}",
|
||||
thumbnail_url=data['preview_file_url'],
|
||||
input_message_content=InputTextMessageContent(
|
||||
f"ID: <code>{query}</code> {rating}\n"
|
||||
f"<a href='{data['large_file_url']}'><b>{characters} ({copyrights})</b> "
|
||||
f"drawn by <b>{artists}</b></a>",
|
||||
parse_mode=ParseMode.HTML
|
||||
f"<code>#{query}</code> <b>{characters} ({copyrights})</b> drawn by <b>{artists}</b> {rating}",
|
||||
parse_mode=ParseMode.HTML,
|
||||
link_preview_options=LinkPreviewOptions(url=data['large_file_url'])
|
||||
),
|
||||
reply_markup=InlineKeyboardMarkup(keyboard)
|
||||
)
|
||||
|
@ -68,9 +84,10 @@ async def invalid_query(update: Update, query: str) -> None:
|
|||
InlineQueryResultArticle(
|
||||
id=str(uuid4()),
|
||||
title=f"ID: {query}",
|
||||
description="not found.",
|
||||
description="Error",
|
||||
input_message_content=InputTextMessageContent(
|
||||
f"ID: <code>{query}</code>\n<b>requested post does not exist.</b>",
|
||||
f"<code>#{query}</code>\n"
|
||||
f"That record was not found.",
|
||||
parse_mode=ParseMode.HTML
|
||||
)
|
||||
)
|
||||
|
|
2
main.py
2
main.py
|
@ -28,7 +28,7 @@ def main() -> None:
|
|||
application.run_polling(allowed_updates=commands.Update.ALL_TYPES)
|
||||
|
||||
|
||||
def get_token():
|
||||
def get_token() -> None:
|
||||
if os.getenv("BOT_TOKEN") is not None:
|
||||
return os.getenv("BOT_TOKEN")
|
||||
|
||||
|
|
BIN
requirements.txt
Normal file
BIN
requirements.txt
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue