2023-08-23 15:43:44 +02:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
|
|
|
|
class PixelfedAPI:
|
|
|
|
def __init__(self, url, token):
|
|
|
|
self.serverurl = url
|
|
|
|
self.accesstoken = token
|
|
|
|
|
|
|
|
def mediaUpload(self, image):
|
|
|
|
serverurl = self.serverurl()
|
|
|
|
apiurl = serverurl + "/api/v1/media"
|
|
|
|
|
|
|
|
accessToken = self.accesstoken()
|
|
|
|
headers = {
|
|
|
|
"Authorization": f"Bearer {accessToken}"
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(image, "rb") as imageFile:
|
|
|
|
f = {"file": imageFile}
|
|
|
|
response = requests.post(apiurl, headers=headers, files=f)
|
|
|
|
try:
|
|
|
|
data = response.json()
|
|
|
|
return(data)
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
print("API response contains non-valid JSON data")
|
|
|
|
#print(response.text)
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
print(f"An error occurred during the API call: {e}")
|
|
|
|
|
|
|
|
def createNewPost(self, ImageID, ImageDescription, ImageAltDescription = ''):
|
|
|
|
serverurl = self.serverurl()
|
|
|
|
apiurl = serverurl + "/api/v1/statuses"
|
|
|
|
|
|
|
|
print(apiurl)
|
|
|
|
|
|
|
|
accessToken = self.accesstoken()
|
|
|
|
headers = {
|
|
|
|
"Authorization": f"Bearer {accessToken}"
|
|
|
|
}
|
|
|
|
data = {
|
|
|
|
"status": ImageDescription,
|
2023-08-23 16:22:36 +02:00
|
|
|
"media_ids": [ImageID]
|
2023-08-23 15:43:44 +02:00
|
|
|
}
|
|
|
|
try:
|
|
|
|
response = requests.post(apiurl, headers=headers, json=data)
|
|
|
|
try:
|
|
|
|
responsedata = response.json()
|
|
|
|
return(responsedata)
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
print("API response contains non-valid JSON data")
|
|
|
|
#print(response.text)
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
print(f"An error occurred during the API call: {e}")
|