alt text and post text update

This commit is contained in:
Nico Jensen 2023-11-01 16:09:32 +01:00
parent f6cc47b681
commit c54ef58e2b
2 changed files with 96 additions and 36 deletions

View File

@ -4,13 +4,21 @@
### Add ### 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
- Changed CHANGELOG.md to new Format - Changed CHANGELOG.md to new Format
- Change README for better first Start - Change README for better first Start
### Removed
- one useless / old file
## v0.3 - release ## v0.3 - release
### Changed ### Changed

View File

@ -4,10 +4,21 @@ import requests
import glob import glob
import exifread import exifread
from PIL import Image, ExifTags from PIL import Image
from PIL.ExifTags import TAGS from PIL.ExifTags import TAGS
from PIL.PngImagePlugin import PngImageFile, PngInfo 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): def load_config(file_path):
with open(file_path, "r") as config_file: with open(file_path, "r") as config_file:
config_data = json.load(config_file) config_data = json.load(config_file)
@ -27,30 +38,68 @@ def getImages(dir):
return result return result
def getItemDescription(filename): def getItemDescription(filename):
type = Image.open(filename) img = Image.open(filename)
exif_data = img._getexif()
imageDescription = ""
exif_tags = open(filename, 'rb') if exif_data:
tags = exifread.process_file(exif_tags) 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.")
exif_array = [] return imageDescription
if type.format != "PNG": def getPostText(filename):
for i in tags: postText = ""
compile = i, str(tags[i]) tags = getTags("tags.txt")
exif_array.append(compile) date = ""
cam = ""
title = ""
if type.format == "PNG": img = Image.open(filename)
image = PngImageFile(filename) exif_data = img._getexif()
metadata = PngInfo()
for i in image.text: if exif_data:
compile = i, str(image.text[i]) for tag, value in exif_data.items():
exif_array.append(compile) 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.")
Description = exif_array[0][1] if date != "":
return Description 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
def mediaUpload(access_token, url, file): return postText
def mediaUpload(access_token, url, file, itemDescription):
api_url = url + "/api/v1/media" api_url = url + "/api/v1/media"
headers = { headers = {
@ -59,15 +108,16 @@ def mediaUpload(access_token, url, file):
try: try:
with open(file, "rb") as imageFile: with open(file, "rb") as imageFile:
f = {"file": imageFile} f = {
data = { "file": (file, imageFile)
"file": (file, imageFile),
"description": "API TEST"
} }
json_data = json.dumps(data)
response = requests.post(api_url, headers=headers, files=data) data = {
response.raise_for_status() # Wirft eine HTTPError-Exception, wenn der Statuscode nicht erfolgreich ist "description": itemDescription.encode('utf-8')
}
response = requests.post(api_url, headers=headers, files=f, data=data)
response.raise_for_status()
try: try:
data = response.json() data = response.json()
@ -106,31 +156,33 @@ def sendImages(Token, url, imagedir, uploadType, deleteFile):
files = getImages(imagedir) files = getImages(imagedir)
if not files: if not files:
raise ValueError("No file found.") raise ValueError("No file found.")
tags = getTags("tags.txt") postText = ""
tagDescription = ""
newFileID = "" newFileID = ""
itemDescription = ""
postText = ""
for f in files: for f in files:
newFile = mediaUpload(Token, url, f) itemDescription = getItemDescription(f)
description = getItemDescription(f) newFile = mediaUpload(Token, url, f, itemDescription)
if uploadType == 0: if uploadType == 0:
tagDescription = description + ' ' + tags postText = getPostText(f)
newFileID = newFile.get("id") newFileID = newFile.get("id")
createNewPost(Token, url, newFileID, tagDescription) createNewPost(Token, url, newFileID, postText)
else: else:
if tagDescription == "": if postText == "":
tagDescription = description + ' ' + tags postText = getPostText(f)
if newFileID != "": if newFileID != "":
newFileID = newFileID + "," + newFile.get("id") newFileID = newFileID + "," + newFile.get("id")
else: else:
newFileID = newFile.get("id") newFileID = newFile.get("id")
if uploadType == 1: if uploadType == 1:
createNewPost(Token, url, newFileID, tagDescription) createNewPost(Token, url, newFileID, postText)
if deleteFile: if deleteFile:
for f in files: for f in files:
if os.path.exists(f): if os.path.exists(f):
os.remove(f) os.remove(f)
return True return True
if __name__ == "__main__": if __name__ == "__main__":