yuri/post.py

44 lines
1.4 KiB
Python
Raw Normal View History

2024-10-04 12:42:40 +02:00
import os
2024-09-28 19:16:04 +02:00
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"])
2024-09-30 08:31:28 +02:00
include_tags = config["TAGS_INCLUDE"].split(",")
exclude_tags = config["TAGS_EXCLUDE"].split(",")
2024-10-04 12:42:40 +02:00
preset_tags=["sort:random", "-rating:e"]
2024-09-30 08:31:28 +02:00
2024-09-28 19:16:04 +02:00
async def main():
masto = Mastodon(api_base_url = config["PLEROMA_INSTANCE"], access_token = config["PLEROMA_ACCESS_TOKEN"], feature_set = "pleroma")
try:
2024-09-30 08:31:28 +02:00
posts = await gelbooru.search_posts(tags=preset_tags + include_tags, exclude_tags=exclude_tags)
2024-09-28 19:16:04 +02:00
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)
2024-10-04 12:42:40 +02:00
os.remove("/tmp/" + result.filename)
2024-09-28 19:16:04 +02:00
source = ""
if result.source != None:
source = "[source](" + result.source + ")"
2024-10-04 12:45:37 +02:00
masto.status_post(source, content_type="text/markdown", visibility="unlisted", media_ids=medias, spoiler_text="Automated Yuri Posting (May be NSFW)")
2024-09-28 19:16:04 +02:00
except Exception:
import traceback
masto.status_post("@ashley@incest.world\n\n" + traceback.format_exc(), visibility="direct", spoiler_text="error!!!")
anyio.run(main)