alt text and post text update

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

View File

@ -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

View File

@ -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.")
exif_array = []
return imageDescription
if type.format != "PNG":
for i in tags:
compile = i, str(tags[i])
exif_array.append(compile)
def getPostText(filename):
postText = ""
tags = getTags("tags.txt")
date = ""
cam = ""
title = ""
if type.format == "PNG":
image = PngImageFile(filename)
metadata = PngInfo()
img = Image.open(filename)
exif_data = img._getexif()
for i in image.text:
compile = i, str(image.text[i])
exif_array.append(compile)
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.")
Description = exif_array[0][1]
return Description
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
def mediaUpload(access_token, url, file):
return postText
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)
}
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
data = {
"description": itemDescription.encode('utf-8')
}
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__":