From 852bf5611726ee57efffe129acffda4a2e49b6dd Mon Sep 17 00:00:00 2001 From: pancakes Date: Thu, 22 Feb 2024 13:49:26 +1000 Subject: [PATCH] Add danbooru url support --- bot.py | 20 ++++++++++++++------ config/example.json.example | 3 ++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index 6abd9ea..ad24663 100644 --- a/bot.py +++ b/bot.py @@ -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 diff --git a/config/example.json.example b/config/example.json.example index 72f7c69..8ead393 100644 --- a/config/example.json.example +++ b/config/example.json.example @@ -12,5 +12,6 @@ "textFiller": [ "Flavor text for posts" ], - "type": "danbooru or gelbooru" + "type": "danbooru or gelbooru", + "url": "https://" } \ No newline at end of file