yuri/post.py
Ashley Graves 0909a7742f oops
2024-10-04 10:45:37 +00:00

43 lines
1.4 KiB
Python

import os
import anyio
from mastodon import Mastodon
from pygelbooru import Gelbooru
from dotenv import dotenv_values
from urllib.request import Request, urlopen
config = dotenv_values(".env")
gelbooru = Gelbooru(config["GELBOORU_API_KEY"], config["GELBOORU_API_UID"])
include_tags = config["TAGS_INCLUDE"].split(",")
exclude_tags = config["TAGS_EXCLUDE"].split(",")
preset_tags=["sort:random", "-rating:e"]
async def main():
masto = Mastodon(api_base_url = config["PLEROMA_INSTANCE"], access_token = config["PLEROMA_ACCESS_TOKEN"], feature_set = "pleroma")
try:
posts = await gelbooru.search_posts(tags=preset_tags + include_tags, exclude_tags=exclude_tags)
result = posts[0]
req = Request(url=result.file_url, headers={"User-Agent": "Mozilla/5.0"})
data = urlopen(req).read()
file = open("/tmp/" + result.filename, "wb")
file.write(data)
file.close()
tags = " ".join(result.tags)
medias = masto.media_post("/tmp/" + result.filename, description=tags)
os.remove("/tmp/" + result.filename)
source = ""
if result.source != None:
source = "[source](" + result.source + ")"
masto.status_post(source, content_type="text/markdown", visibility="unlisted", media_ids=medias, spoiler_text="Automated Yuri Posting (May be NSFW)")
except Exception:
import traceback
masto.status_post("@ashley@incest.world\n\n" + traceback.format_exc(), visibility="direct", spoiler_text="error!!!")
anyio.run(main)