From ed6552fe41830ee0177b9e288492fda3c363c6d0 Mon Sep 17 00:00:00 2001 From: Nico Jensen Date: Sun, 29 Oct 2023 03:25:04 +0100 Subject: [PATCH] Add new Upload Type --- PixelfedImporter.py | 132 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 10 +++- 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 PixelfedImporter.py diff --git a/PixelfedImporter.py b/PixelfedImporter.py new file mode 100644 index 0000000..6dd2dff --- /dev/null +++ b/PixelfedImporter.py @@ -0,0 +1,132 @@ +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" + ImageIDs = ImageID.split(",") + ImageIDsInt = list(map(int, ImageIDs)) + headers = { + "Authorization": f"Bearer {access_token}" + } + + data = { + "status": ImageDescription, + "media_ids": ImageIDsInt + } + + 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, uploadType): + files = getImages(imagedir) + tags = getTags("tags.txt") + tagDescription = "" + newFileID = "" + + for f in files: + newFile = mediaUpload(Token, url, f) + description = getItemDescription(f) + if uploadType == 0: + tagDescription = description + ' ' + tags + newFileID = newFile.get("id") + createNewPost(Token, url, newFileID, tagDescription) + else: + if tagDescription == "": + tagDescription = description + ' ' + tags + if newFileID != "": + newFileID = newFileID + "," + newFile.get("id") + else: + newFileID = newFile.get("id") + if uploadType == 1: + 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") + uploadType = config.get("upload_type") + + sendImages(accessToken, url, imagedir, uploadType) \ No newline at end of file diff --git a/README.md b/README.md index ab05207..33ab960 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Pixelfed Importer -a Python app to publish all images from a folder to Pixelfed +a Python app to publish all images from a folder to Pixelfed. +This script use Image Meta Tag Description fot the Post Description. ## Config @@ -10,10 +11,15 @@ To use this you need a config.json if this cannot be created automatically, here { "server_url": "URL TO YOUR PIXELFED", "access_token": "YOUR ACCESS TOKEN", - "image_path": "PATH TO YOUR IMAGES" + "image_path": "PATH TO YOUR IMAGES", + "upload_type": 0|1 } ``` +upload_type 0 creates 1 post for each image from your folder (image_path) +upload_type 1 creates 1 post with all images from your folder (image_path) +Uploads at Type 1 only use Image Meta Tag Description from the first image for the post description. + ## tags You can add a tags.txt, in which standard tags can be stored, which will always be added at the end of a post.