for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
#by Linaname
from modules.logging import logging_decorator
from telegram.ext import CommandHandler
from telegram import ChatAction
import requests
import random
def module_init(gd):
commands = gd.config["commands"]
for command in commands:
gd.dp.add_handler(CommandHandler(command, gelbooru_search, pass_args=True))
@logging_decorator("gelbooru")
def gelbooru_search(bot, update, args):
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
query = " ".join(args)
try:
direct_link, page_link = get_image(query)
except:
update.message.reply_text("Sorry, something went wrong!")
return
if direct_link is None:
update.message.reply_text("Nothing found!")
msg_text = "[Image]({})".format(direct_link) + "\n" + "[View post]({})".format(page_link)
update.message.reply_text(msg_text, parse_mode="Markdown")
return(query)
def get_image(query):
params = {
"page": "dapi",
"s": "post",
"q": "index",
"json": "1",
"tags": query,
}
response = requests.get("https://gelbooru.com/index.php", params=params)
if not response.text:
return None, None
result_list = response.json()
if not result_list:
post = random.choice(result_list)
direct_link, page_link = post.get("file_url"), "https://gelbooru.com/index.php?page=post&s=view&id="+str(post.get("id"))
return direct_link, page_link