add other booru client

This commit is contained in:
Owl 2025-01-23 23:51:04 +07:00
parent c65401abc7
commit ef18f67115
4 changed files with 61 additions and 5 deletions

View File

@ -5,3 +5,6 @@ BOORU_API_URL = "http://booru-api:3456/booru"
INLINE_QUERY_CACHE_SECONDS = 30 INLINE_QUERY_CACHE_SECONDS = 30
STICKER_SHITHOLE = -1002471390283 STICKER_SHITHOLE = -1002471390283
DANBOORU_USERNAME = "owlrandomshitbot"
DANBOORU_API_KEY = os.getenv("DANBOORU_API_KEY")

View File

@ -8,3 +8,4 @@ pillow==11.0.0
python-telegram-bot==21.6 python-telegram-bot==21.6
sniffio==1.3.1 sniffio==1.3.1
requests==2.32.3 requests==2.32.3
Pybooru==4.2.2

View File

@ -1,9 +1,10 @@
import requests import requests
from pybooru import Danbooru
from telegram import Update from telegram import Update
from telegram.ext import ContextTypes from telegram.ext import ContextTypes
from tgbot.config import STICKER_SHITHOLE, BOORU_API_URL, INLINE_QUERY_CACHE_SECONDS from tgbot.config import STICKER_SHITHOLE, BOORU_API_URL, INLINE_QUERY_CACHE_SECONDS, DANBOORU_API_KEY
from tgbot.shit.hentai import parse_args, create_parser from tgbot.shit.hentai_argparser import parse_args, create_parser
from tgbot.shit.render import render_text_on_image from tgbot.shit.render import render_text_on_image
import re import re
@ -108,6 +109,57 @@ async def handle_hentai(update: Update, context: ContextTypes.DEFAULT_TYPE):
print(args) print(args)
# using danbooru
if args["booru"] in ["db", "dan", "danbooru"]:
if len(args["tags"]) > 2:
error_response = [
{
"type": "article",
"id": "1",
"title": "ERROR!",
"description": "None",
"input_message_content": {
"message_text": "None"
}
}
]
error_response[0]["description"] = "Danbooru won't let you search more than two tags at once cause " \
"they're greedy "
error_response[0]["input_message_content"]["message_text"] = error_response[0]["description"]
return await context.bot.answer_inline_query(update.inline_query.id, error_response,
cache_time=INLINE_QUERY_CACHE_SECONDS)
client = Danbooru('danbooru', username='owlrandomshitbot', api_key=DANBOORU_API_KEY)
posts = client.post_list(tags=" ".join(args["tags"]), limit=args["limit"], random=args["random"])
results = []
for idx, post in enumerate(posts):
booru_post_url = "https://danbooru.donmai.us/posts/" + str(post["id"])
try:
results.append(
{
"type": "photo",
"id": "%d" % idx,
"photo_url": post["file_url"],
"thumb_url": post["preview_file_url"],
"caption": "source -> " + booru_post_url
}
)
except Exception:
# this post requires a gold account
print(post)
return await context.bot.answer_inline_query(update.inline_query.id, results,
cache_time=INLINE_QUERY_CACHE_SECONDS)
# using node booru service
response = requests.post(BOORU_API_URL, json=args) response = requests.post(BOORU_API_URL, json=args)
if response.status_code == 200: if response.status_code == 200:
@ -146,7 +198,7 @@ async def handle_hentai(update: Update, context: ContextTypes.DEFAULT_TYPE):
} }
}) })
await context.bot.answer_inline_query(update.inline_query.id, results, return await context.bot.answer_inline_query(update.inline_query.id, results,
cache_time=INLINE_QUERY_CACHE_SECONDS) cache_time=INLINE_QUERY_CACHE_SECONDS)
else: else: