imagebot/main.py
2024-07-10 08:34:43 -04:00

90 lines
1.8 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.
from dotenv import load_dotenv, dotenv_values
load_dotenv()
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):
path = "/drive/files/upload-from-url"
url = baseurl+path
data = {
"url": fileurl,
"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 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():
posturl, url, artists = image_source.main()
filename = url.split("/")[len(url.split("/"))-1]
upload_url(url)
post(f"Artist(s): {artists}\nSource: {posturl}",get_id(filename))
if __name__ == "__main__":
main()