Add danbooru url support

This commit is contained in:
pancakes 2024-02-22 13:49:26 +10:00
parent 0e7d143750
commit 852bf56117
2 changed files with 16 additions and 7 deletions

20
bot.py
View file

@ -15,6 +15,7 @@ class Config:
text_cw: str
text_filler: list[str]
type: str
url: str
def __init__(self, config_name: str):
with open(f"config/{config_name}.json") as file:
@ -26,8 +27,9 @@ class Config:
self.text_cw = config["textCW"]
self.text_filler = config["textFiller"]
self.type = config["type"]
self.url = config["url"]
def random_image_danbooru(block_tags: list[str], search_tags: list[str]) -> dict | None:
def random_image_danbooru(block_tags: list[str], search_tags: list[str], url: str) -> dict | None:
tags = []
for tag in block_tags:
tags.append("-" + tag.strip())
@ -42,7 +44,7 @@ def random_image_danbooru(block_tags: list[str], search_tags: list[str]) -> dict
if environ.get("BOARD_API_KEY") and environ.get("BOARD_USERNAME"):
params["api_key"] = environ.get("BOARD_API_KEY")
params["login"] = environ.get("BOARD_USERNAME")
resp = requests.get("https://danbooru.donmai.us/posts.json", params=params)
resp = requests.get(url + "/posts.json", params=params)
if resp.ok == False:
print(resp.json())
@ -74,10 +76,14 @@ def random_image_gelbooru(block_tags: list[str], search_tags: list[str]) -> dict
if resp.ok == False:
print(resp.json())
return None
elif resp.json()["post"] == []:
return None
return random.choice(resp.json()["post"])
post = resp.json()
if len(post["post"]) > 0:
return random.choice(resp.json()["post"])
return None
# Exit if config isn't specified
if len(argv) < 2:
@ -88,7 +94,7 @@ config = Config(argv[1])
match config.type:
case "danbooru":
post = random_image_danbooru(config.block_tags, config.search_tags)
post = random_image_danbooru(config.block_tags, config.search_tags, config.url)
case "gelbooru":
post = random_image_gelbooru(config.block_tags, config.search_tags)
case _:
@ -155,7 +161,9 @@ match config.type:
tags = post["tags"]
case _:
tags = "unknown"
print(post_content, tags)
exit()
media = mastodon.media_post(
description="Automatically posted image. Tagged: " + tags,
media_file=filename

View file

@ -12,5 +12,6 @@
"textFiller": [
"Flavor text for posts"
],
"type": "danbooru or gelbooru"
"type": "danbooru or gelbooru",
"url": "https://"
}