From c54ef58e2b46d4e5074101f81cfcf1505271f921 Mon Sep 17 00:00:00 2001 From: Nico Jensen Date: Wed, 1 Nov 2023 16:09:32 +0100 Subject: [PATCH] alt text and post text update --- CHANGELOG.md | 10 +++- PixelfedImporter.py | 122 +++++++++++++++++++++++++++++++------------- 2 files changed, 96 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ede8af..ce72daa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,21 @@ ### Add -- config.json.example +- config.json.example. +- It's now possible to add a Item Description as ALT text for Images. +-- For this I use the Item Description from the META Tags. +- For me its not possible to read the special LR META Tags so its now possible to add a Post text while upload it. +- New Post text Design. (Date // Camera // Title(Text) default Tags) ### Changed - Changed CHANGELOG.md to new Format - Change README for better first Start +### Removed + +- one useless / old file + ## v0.3 - release ### Changed diff --git a/PixelfedImporter.py b/PixelfedImporter.py index 9384eb2..82d6f24 100644 --- a/PixelfedImporter.py +++ b/PixelfedImporter.py @@ -4,10 +4,21 @@ import requests import glob import exifread -from PIL import Image, ExifTags +from PIL import Image from PIL.ExifTags import TAGS from PIL.PngImagePlugin import PngImageFile, PngInfo +def confirm(): + confirmMsg = input("[y]es or [n]o ") + if confirmMsg == 'y' or confirmMsg == 'yes': + return True + elif confirmMsg == 'n' or confirmMsg == 'no': + return False + else: + print("\n Invalid Option. Please Enter a Valid Option.") + return confirm() + return False + def load_config(file_path): with open(file_path, "r") as config_file: config_data = json.load(config_file) @@ -27,30 +38,68 @@ def getImages(dir): return result def getItemDescription(filename): - type = Image.open(filename) + img = Image.open(filename) + exif_data = img._getexif() + imageDescription = "" - exif_tags = open(filename, 'rb') - tags = exifread.process_file(exif_tags) + if exif_data: + for tag, value in exif_data.items(): + tag_name = TAGS.get(tag, tag) + if tag_name == "ImageDescription": + imageDescription = value + else: + imageDescription = "This image has no description. Please let me know so that I can update it." + else: + raise ValueError("No EXIF data found.") + + return imageDescription - exif_array = [] +def getPostText(filename): + postText = "" + tags = getTags("tags.txt") + date = "" + cam = "" + title = "" - if type.format != "PNG": - for i in tags: - compile = i, str(tags[i]) - exif_array.append(compile) + img = Image.open(filename) + exif_data = img._getexif() - if type.format == "PNG": - image = PngImageFile(filename) - metadata = PngInfo() + if exif_data: + for tag, value in exif_data.items(): + tag_name = TAGS.get(tag, tag) + if tag_name == "DateTimeOriginal": + dateTime = value + year = dateTime[:4] + month = dateTime[5:7] + date = "🗓 " + year + "-" + month + if tag_name == "Make": + cam += "📸 " + value + if cam != "" and tag_name == "Model": + cam += " " + value + if tag_name == "Object Name": + title = value + else: + raise ValueError("No EXIF data found.") + + if date != "": + postText = date + if cam != "": + if postText != "": + postText += " || " + postText += cam + if title != "": + postText += "\n\r\n\r" + title + elif title == "": + print(f"No post Text found. Do you want to add a post text for file " + os.path.basename(filename) + "?") + if confirm(): + ownPostText = input("Post text:") + ownPostText.encode('utf-8') + postText += "\n\r\n\r" + ownPostText + postText += "\n\r\n\r" + tags - for i in image.text: - compile = i, str(image.text[i]) - exif_array.append(compile) + return postText - Description = exif_array[0][1] - return Description - -def mediaUpload(access_token, url, file): +def mediaUpload(access_token, url, file, itemDescription): api_url = url + "/api/v1/media" headers = { @@ -59,15 +108,16 @@ def mediaUpload(access_token, url, file): try: with open(file, "rb") as imageFile: - f = {"file": imageFile} - data = { - "file": (file, imageFile), - "description": "API TEST" + f = { + "file": (file, imageFile) + } + + data = { + "description": itemDescription.encode('utf-8') } - json_data = json.dumps(data) - response = requests.post(api_url, headers=headers, files=data) - response.raise_for_status() # Wirft eine HTTPError-Exception, wenn der Statuscode nicht erfolgreich ist + response = requests.post(api_url, headers=headers, files=f, data=data) + response.raise_for_status() try: data = response.json() @@ -106,31 +156,33 @@ def sendImages(Token, url, imagedir, uploadType, deleteFile): files = getImages(imagedir) if not files: raise ValueError("No file found.") - tags = getTags("tags.txt") - tagDescription = "" + postText = "" newFileID = "" + itemDescription = "" + postText = "" for f in files: - newFile = mediaUpload(Token, url, f) - description = getItemDescription(f) + itemDescription = getItemDescription(f) + newFile = mediaUpload(Token, url, f, itemDescription) if uploadType == 0: - tagDescription = description + ' ' + tags + postText = getPostText(f) newFileID = newFile.get("id") - createNewPost(Token, url, newFileID, tagDescription) + createNewPost(Token, url, newFileID, postText) else: - if tagDescription == "": - tagDescription = description + ' ' + tags + if postText == "": + postText = getPostText(f) if newFileID != "": newFileID = newFileID + "," + newFile.get("id") else: newFileID = newFile.get("id") if uploadType == 1: - createNewPost(Token, url, newFileID, tagDescription) + createNewPost(Token, url, newFileID, postText) if deleteFile: for f in files: if os.path.exists(f): os.remove(f) + return True if __name__ == "__main__":