GloryBot/modules/booruGet.py

112 lines
3.8 KiB
Python
Raw Normal View History

2023-04-10 23:32:21 +03:00
import json
import traceback
import requests
import webbrowser
import random
import asyncio
import xml.etree.ElementTree as ET
async def booruGet(booru, tags, page=None, limit=30):
try:
booru_list = ["realbooru", "yandere", "danbooru", "konachan", "rule34"]
real_list = ["realbooru"]
anime_list = ["yandere", "danbooru", "konachan"]
thumbnail = ""
if page is None:
page = random.randint(0, 30)
if booru == "2d":
booru = random.choice(anime_list)
elif booru == "3d":
booru = random.choice(real_list)
elif booru == "random" or booru not in booru_list:
booru = random.choice(booru_list)
if booru == "realbooru":
url = f"https://realbooru.com/index.php?page=dapi&s=post&q=index&limit={limit}&pid={page}&tags={tags}"
base_url = "https://realbooru.com/images/"
thumbnail_url = "https://realbooru.com/thumbnails/"
post = random.choice(ET.fromstring(requests.get(url).text))
file_format = post.get("file_url").split(".")[-1]
thumb_format = post.get("preview_url").split(".")[-1]
md5 = post.get("md5")
output = f"{md5[:2]}/{md5[2:4]}/{md5}.{file_format}"
output_thumb = f"{md5[:2]}/{md5[2:4]}/thumbnail_{md5}.{thumb_format}"
image = source = base_url+output
thumbnail = thumbnail_url+output_thumb
tags = post.get("tags")
elif booru == "yandere":
url = f"https://yande.re/post.json?limit={limit}&tags={tags}&page={page}"
output = random.choice(json.loads(requests.get(url).text))
image = output["sample_url"]
source = output["file_url"]
tags = output["tags"]
elif booru == "danbooru":
url = f"https://danbooru.donmai.us/posts.json?&page={page}&tags={tags}&limit={limit}"
output = random.choice(json.loads(requests.get(url).text))
image = output["large_file_url"]
source = output["file_url"]
tags = output["tag_string"]
elif booru == "konachan":
url = f"https://konachan.com/post.json?&page={page}&tags={tags}&limit={limit}"
output = random.choice(json.loads(requests.get(url).text))
image = output["sample_url"]
source = output["file_url"]
tags = output["tags"]
elif booru == "rule34":
url = f"https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&limit={limit}&pid={page}&tags={tags}&json=1"
print(url)
print(requests.get(url).json)
output = random.choice(json.loads(requests.get(url).json))
image = output["sample_url"]
source = output["file_url"]
tags = output["tags"]
if (".jpg" in image) or (".png" in image) or (".webp" in image) or (".jpeg" in image) or (".gif" in image):
kind = "image"
else:
kind = "video"
return {"image": image, "thumbnail": thumbnail, "source": source, "kind": kind, "tags": tags.strip(), "error": ""}
except IndexError:
return {"image": "http://res.end-play.xyz/discord/invalid.jpg", "thumbnail": "", "source": "N/A", "kind": "image", "tags": "", "error": "Found page with such tags is empty.\nDecrease images limit or page number and try again."}
except Exception as exp:
return {"image": "http://res.end-play.xyz/discord/invalid.jpg", "thumbnail": "", "source": "N/A", "kind": "image", "tags": "", "error": str(traceback.format_exc())}
if __name__ == "__main__":
booru = input("Input booru: ")
tags = input("Input tags: ")
url = asyncio.run(booruGet(booru, tags))
print(url)
webbrowser.open(url["image"])