117 lines
3.2 KiB
Python
117 lines
3.2 KiB
Python
|
import os
|
||
|
import json
|
||
|
import requests
|
||
|
import glob
|
||
|
import exifread
|
||
|
|
||
|
from PIL import Image, ExifTags
|
||
|
from PIL.ExifTags import TAGS
|
||
|
from PIL.PngImagePlugin import PngImageFile, PngInfo
|
||
|
|
||
|
def load_config(file_path):
|
||
|
with open(file_path, "r") as config_file:
|
||
|
config_data = json.load(config_file)
|
||
|
return config_data
|
||
|
|
||
|
def getTags(file_path):
|
||
|
try:
|
||
|
file = open(file_path,'r')
|
||
|
fileContent = file.read()
|
||
|
file.close
|
||
|
except:
|
||
|
fileContent = ''
|
||
|
return fileContent
|
||
|
|
||
|
def getImages(dir):
|
||
|
result = glob.glob(dir + '/*.jpg')
|
||
|
return result
|
||
|
|
||
|
def getItemDescription(filename):
|
||
|
type = Image.open(filename)
|
||
|
|
||
|
exif_tags = open(filename, 'rb')
|
||
|
tags = exifread.process_file(exif_tags)
|
||
|
|
||
|
exif_array = []
|
||
|
|
||
|
if type.format != "PNG":
|
||
|
for i in tags:
|
||
|
compile = i, str(tags[i])
|
||
|
exif_array.append(compile)
|
||
|
|
||
|
if type.format == "PNG":
|
||
|
image = PngImageFile(filename)
|
||
|
metadata = PngInfo()
|
||
|
|
||
|
for i in image.text:
|
||
|
compile = i, str(image.text[i])
|
||
|
exif_array.append(compile)
|
||
|
|
||
|
Description = exif_array[0][1]
|
||
|
return Description
|
||
|
|
||
|
def mediaUpload(access_token, url, file):
|
||
|
api_url = url + "/api/v1/media"
|
||
|
|
||
|
headers = {
|
||
|
"Authorization": f"Bearer {access_token}"
|
||
|
}
|
||
|
|
||
|
try:
|
||
|
with open(file, "rb") as imageFile:
|
||
|
f = {"file": imageFile}
|
||
|
response = requests.post(api_url, headers=headers, files=f)
|
||
|
response.raise_for_status() # Wirft eine HTTPError-Exception, wenn der Statuscode nicht erfolgreich ist
|
||
|
|
||
|
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(access_token, url, ImageID, ImageDescription):
|
||
|
api_url = url + "/api/v1/statuses"
|
||
|
|
||
|
headers = {
|
||
|
"Authorization": f"Bearer {access_token}"
|
||
|
}
|
||
|
|
||
|
data = {
|
||
|
"status": ImageDescription,
|
||
|
"media_ids": [ImageID]
|
||
|
}
|
||
|
|
||
|
try:
|
||
|
response = requests.post(api_url, 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}")
|
||
|
|
||
|
def sendImages(Token, url, imagedir):
|
||
|
files = getImages(imagedir)
|
||
|
tags = getTags("tags.txt")
|
||
|
for f in files:
|
||
|
description = getItemDescription(f)
|
||
|
tagDescription = description + ' ' + tags
|
||
|
newFile = mediaUpload(Token, url, f)
|
||
|
newFileID = newFile.get("id")
|
||
|
createNewPost(Token, url, newFileID, tagDescription)
|
||
|
return True
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
config_file_path = "config.json"
|
||
|
config = load_config(config_file_path)
|
||
|
|
||
|
url = config.get("server_url")
|
||
|
accessToken = config.get("access_token")
|
||
|
imagedir = config.get("image_path")
|
||
|
|
||
|
sendImages(accessToken, url, imagedir)
|