imagebot/main.py
2025-03-23 11:08:39 +02:00

77 lines
2 KiB
Python

import os
import json
import requests
import image_source ## image source needs to have a `main` function that returns
# a string which is a link to the source of the image. this function also needs to
# refresh the `out.jpg` to the new image.
baseurl = os.getenv("baseurl")
def post(text, id):
path = "/notes/create"
url = baseurl + path
data = {
# "visibility":"specified",
"text": text,
"mediaIds": [id],
"appSecret": os.getenv("secret"),
}
headers = {
"Authorization": f"Bearer {os.getenv('api_key')}",
"Content-Type": "application/json",
}
response = requests.post(url, json=data, headers=headers)
return response
def upload_url(fileurl, comment):
path = "/drive/files/upload-from-url"
url = baseurl + path
data = {"url": fileurl, "appSecret": os.getenv("secret"), "comment": comment}
headers = {
"Authorization": f"Bearer {os.getenv('api_key')}",
"Content-Type": "application/json",
}
response = requests.post(url, json=data, headers=headers)
return response
def get_id(filename):
path = "/drive/files/find"
url = baseurl + path
data = {"name": filename, "appSecret": os.getenv("secret")}
headers = {
"Authorization": f"Bearer {os.getenv('api_key')}",
"Content-Type": "application/json",
}
response = requests.post(url, json=data, headers=headers)
json_data = str(response.text)
try:
return json.loads(json_data)[0]["id"]
except IndexError:
return get_id(filename)
def main():
try:
posturl, url, artists, characters = image_source.main()
filename = url.split("/")[len(url.split("/")) - 1]
upload_url(url, f"SFW yaoi art of the following characters : {characters}")
post(f"Artist(s): {artists}\nSource: {posturl}", get_id(filename))
except:
main()
if __name__ == "__main__":
main()