Add danbooru url support
This commit is contained in:
parent
0e7d143750
commit
852bf56117
2 changed files with 16 additions and 7 deletions
18
bot.py
18
bot.py
|
@ -15,6 +15,7 @@ class Config:
|
||||||
text_cw: str
|
text_cw: str
|
||||||
text_filler: list[str]
|
text_filler: list[str]
|
||||||
type: str
|
type: str
|
||||||
|
url: str
|
||||||
|
|
||||||
def __init__(self, config_name: str):
|
def __init__(self, config_name: str):
|
||||||
with open(f"config/{config_name}.json") as file:
|
with open(f"config/{config_name}.json") as file:
|
||||||
|
@ -26,8 +27,9 @@ class Config:
|
||||||
self.text_cw = config["textCW"]
|
self.text_cw = config["textCW"]
|
||||||
self.text_filler = config["textFiller"]
|
self.text_filler = config["textFiller"]
|
||||||
self.type = config["type"]
|
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 = []
|
tags = []
|
||||||
for tag in block_tags:
|
for tag in block_tags:
|
||||||
tags.append("-" + tag.strip())
|
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"):
|
if environ.get("BOARD_API_KEY") and environ.get("BOARD_USERNAME"):
|
||||||
params["api_key"] = environ.get("BOARD_API_KEY")
|
params["api_key"] = environ.get("BOARD_API_KEY")
|
||||||
params["login"] = environ.get("BOARD_USERNAME")
|
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:
|
if resp.ok == False:
|
||||||
print(resp.json())
|
print(resp.json())
|
||||||
|
@ -74,10 +76,14 @@ def random_image_gelbooru(block_tags: list[str], search_tags: list[str]) -> dict
|
||||||
if resp.ok == False:
|
if resp.ok == False:
|
||||||
print(resp.json())
|
print(resp.json())
|
||||||
return None
|
return None
|
||||||
elif resp.json()["post"] == []:
|
|
||||||
|
post = resp.json()
|
||||||
|
|
||||||
|
if len(post["post"]) > 0:
|
||||||
|
return random.choice(resp.json()["post"])
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return random.choice(resp.json()["post"])
|
|
||||||
|
|
||||||
# Exit if config isn't specified
|
# Exit if config isn't specified
|
||||||
if len(argv) < 2:
|
if len(argv) < 2:
|
||||||
|
@ -88,7 +94,7 @@ config = Config(argv[1])
|
||||||
|
|
||||||
match config.type:
|
match config.type:
|
||||||
case "danbooru":
|
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":
|
case "gelbooru":
|
||||||
post = random_image_gelbooru(config.block_tags, config.search_tags)
|
post = random_image_gelbooru(config.block_tags, config.search_tags)
|
||||||
case _:
|
case _:
|
||||||
|
@ -155,7 +161,9 @@ match config.type:
|
||||||
tags = post["tags"]
|
tags = post["tags"]
|
||||||
case _:
|
case _:
|
||||||
tags = "unknown"
|
tags = "unknown"
|
||||||
|
print(post_content, tags)
|
||||||
|
|
||||||
|
exit()
|
||||||
media = mastodon.media_post(
|
media = mastodon.media_post(
|
||||||
description="Automatically posted image. Tagged: " + tags,
|
description="Automatically posted image. Tagged: " + tags,
|
||||||
media_file=filename
|
media_file=filename
|
||||||
|
|
|
@ -12,5 +12,6 @@
|
||||||
"textFiller": [
|
"textFiller": [
|
||||||
"Flavor text for posts"
|
"Flavor text for posts"
|
||||||
],
|
],
|
||||||
"type": "danbooru or gelbooru"
|
"type": "danbooru or gelbooru",
|
||||||
|
"url": "https://"
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue