2
0
mirror of https://github.com/Nick80835/microbot synced 2025-09-05 17:05:52 +00:00
Files
microbot/ubot/modules/sankaku.py

56 lines
1.5 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: GPL-2.0-or-later
from ubot.micro_bot import micro_bot
ldr = micro_bot.loader
SAN_URL = "https://capi-v2.sankakucomplex.com/posts"
@ldr.add("san(s|x|q|)(f|)")
async def sankaku(event):
safety_arg = event.pattern_match.group(1)
as_file = bool(event.pattern_match.group(2))
rating = ""
if safety_arg == "x":
rating = "rating:explicit"
elif safety_arg == "s":
rating = "rating:safe"
elif safety_arg == "q":
rating = "rating:questionable"
params = {"page": 1,
"limit": 5,
"tags": f"order:random {rating} {event.args}".strip().replace(" ", " ")}
2020-05-09 13:15:08 -04:00
async with ldr.aioclient.get(SAN_URL, params=params) as response:
if response.status == 200:
response = await response.json()
else:
await event.reply(f"`An error occurred, response code: `**{response.status}**")
return
if not response:
await event.reply(f"`No results for query: `**{event.args}**")
return
valid_urls = []
2020-05-03 20:45:35 -04:00
for post in response:
if 'file_url' in post.keys():
valid_urls.append(post['file_url'])
if not valid_urls:
await event.reply(f"`Failed to find URLs for query: `**{event.args}**")
return
for image_url in valid_urls:
try:
await event.reply(file=image_url, force_document=as_file)
return
except:
pass
await event.reply(f"`Failed to fetch media for query: `**{event.args}**")