Neuüberarbeitung nun mit Frontend
This commit is contained in:
parent
8702e38b6c
commit
ae1eb8076e
335
main.py
335
main.py
@ -1,117 +1,270 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
|
||||||
import requests
|
|
||||||
import glob
|
|
||||||
import exifread
|
import exifread
|
||||||
|
import json
|
||||||
from PIL import Image, ExifTags
|
import gettext
|
||||||
|
import tkinter as tk
|
||||||
|
from PixelfedAPI import *
|
||||||
|
from tkinter import filedialog, Menu, Toplevel
|
||||||
|
from PIL import Image, ImageTk, ExifTags
|
||||||
from PIL.ExifTags import TAGS
|
from PIL.ExifTags import TAGS
|
||||||
from PIL.PngImagePlugin import PngImageFile, PngInfo
|
from PIL.PngImagePlugin import PngImageFile, PngInfo
|
||||||
|
|
||||||
def load_config(file_path):
|
class ImageUploaderApp:
|
||||||
with open(file_path, "r") as config_file:
|
def __init__(self, root):
|
||||||
config_data = json.load(config_file)
|
serverURL = self.get_setting_serverURL
|
||||||
return config_data
|
serverAccesskey = self.get_setting_accessKey
|
||||||
|
self.Pixelfed = PixelfedAPI(serverURL, serverAccesskey)
|
||||||
|
|
||||||
def getTags(file_path):
|
self.root = root
|
||||||
try:
|
self.root.title("Pixelfed Uploader")
|
||||||
file = open(file_path,'r')
|
|
||||||
fileContent = file.read()
|
|
||||||
file.close
|
|
||||||
except:
|
|
||||||
fileContent = ''
|
|
||||||
return fileContent
|
|
||||||
|
|
||||||
def getImages(dir):
|
self.image_paths = []
|
||||||
result = glob.glob(dir + '/*.jpg')
|
self.current_index = 0
|
||||||
return result
|
|
||||||
|
|
||||||
def getItemDescription(filename):
|
self.create_menu()
|
||||||
type = Image.open(filename)
|
|
||||||
|
|
||||||
exif_tags = open(filename, 'rb')
|
self.button_frame = tk.Frame(self.root)
|
||||||
tags = exifread.process_file(exif_tags)
|
self.prev_button = tk.Button(self.button_frame, text="Previous", command=self.show_previous_image)
|
||||||
|
self.prev_button.pack(side=tk.LEFT)
|
||||||
|
self.canvas = tk.Canvas(self.button_frame, width=400, height=400)
|
||||||
|
self.canvas.pack(side=tk.LEFT)
|
||||||
|
self.next_button = tk.Button(self.button_frame, text="Next", command=self.show_next_image)
|
||||||
|
self.next_button.pack(side=tk.RIGHT)
|
||||||
|
self.button_frame.pack()
|
||||||
|
|
||||||
exif_array = []
|
self.description_label = tk.Label(self.root, text="Post Content:")
|
||||||
|
self.description_label.pack()
|
||||||
|
|
||||||
if type.format != "PNG":
|
self.description_text = tk.Text(self.root, height=10, wrap=tk.WORD)
|
||||||
for i in tags:
|
self.description_text.pack()
|
||||||
compile = i, str(tags[i])
|
|
||||||
exif_array.append(compile)
|
|
||||||
|
|
||||||
if type.format == "PNG":
|
self.description_length_var = tk.StringVar()
|
||||||
image = PngImageFile(filename)
|
self.description_length_label = tk.Label(self.root, textvariable=self.description_length_var)
|
||||||
metadata = PngInfo()
|
self.description_length_label.pack()
|
||||||
|
|
||||||
for i in image.text:
|
self.description_text.bind("<KeyRelease>", self.update_description_length)
|
||||||
compile = i, str(image.text[i])
|
|
||||||
exif_array.append(compile)
|
|
||||||
|
|
||||||
Description = exif_array[0][1]
|
self.tag_text_label = tk.Label(self.root, text="Tags:")
|
||||||
return Description
|
self.tag_text_label.pack()
|
||||||
|
|
||||||
def mediaUpload(access_token, url, file):
|
self.tag_text = tk.Text(self.root, height=5, wrap=tk.WORD)
|
||||||
api_url = url + "/api/v1/media"
|
self.tag_text.pack()
|
||||||
|
|
||||||
headers = {
|
defaultTags = self.getTags()
|
||||||
"Authorization": f"Bearer {access_token}"
|
self.tag_text.delete("1.0", tk.END)
|
||||||
}
|
self.tag_text.insert(tk.END, defaultTags)
|
||||||
|
|
||||||
try:
|
#self.alt_text_label = tk.Label(self.root, text="Alt-Text:")
|
||||||
with open(file, "rb") as imageFile:
|
#self.alt_text_label.pack()
|
||||||
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:
|
#self.alt_text = tk.Text(self.root, height=10, wrap=tk.WORD)
|
||||||
data = response.json()
|
#self.alt_text.pack()
|
||||||
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):
|
#self.alt_length_var = tk.StringVar()
|
||||||
api_url = url + "/api/v1/statuses"
|
#self.alt_length_label = tk.Label(self.root, textvariable=self.alt_length_var)
|
||||||
|
#self.alt_length_label.pack()
|
||||||
|
|
||||||
headers = {
|
#self.alt_text.bind("<KeyRelease>", self.update_alt_length)
|
||||||
"Authorization": f"Bearer {access_token}"
|
|
||||||
}
|
|
||||||
|
|
||||||
data = {
|
self.upload_button = tk.Button(self.root, text="Upload", command=self.upload_image)
|
||||||
"status": ImageDescription,
|
self.upload_button.pack()
|
||||||
"media_ids": [ImageID]
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
self.open_folder()
|
||||||
response = requests.post(api_url, headers=headers, json=data)
|
|
||||||
|
def open_folder(self):
|
||||||
|
self.load_images()
|
||||||
|
|
||||||
|
def create_menu(self):
|
||||||
|
self.menu_bar = Menu(self.root)
|
||||||
|
self.root.config(menu=self.menu_bar)
|
||||||
|
|
||||||
|
self.file_menu = Menu(self.menu_bar, tearoff=0)
|
||||||
|
self.menu_bar.add_cascade(label="File", menu=self.file_menu)
|
||||||
|
self.file_menu.add_command(label="Open Folder", command=self.load_images)
|
||||||
|
self.file_menu.add_command(label="Settings", command=self.open_settings)
|
||||||
|
|
||||||
|
def open_settings(self):
|
||||||
|
settings_window = Toplevel(self.root)
|
||||||
|
settings_window.title("Settings")
|
||||||
|
settings_window.geometry("500x250")
|
||||||
|
|
||||||
|
server_url_label = tk.Label(settings_window, text="Server-URL:")
|
||||||
|
server_url_label.pack()
|
||||||
|
server_url_entry = tk.Entry(settings_window, width=50)
|
||||||
|
server_url_entry.pack()
|
||||||
|
|
||||||
|
access_key_label = tk.Label(settings_window, text="API AccessKey:")
|
||||||
|
access_key_label.pack()
|
||||||
|
access_key_entry = tk.Entry(settings_window, width=50)
|
||||||
|
access_key_entry.pack()
|
||||||
|
|
||||||
|
image_folder_label = tk.Label(settings_window, text="Default Folder:")
|
||||||
|
image_folder_label.pack()
|
||||||
|
image_folder_entry = tk.Entry(settings_window, width=50)
|
||||||
|
image_folder_entry.pack()
|
||||||
|
|
||||||
|
enableLogVar = tk.BooleanVar()
|
||||||
|
enableLog_label = tk.Label(settings_window, text="Log:")
|
||||||
|
enableLog_label.pack()
|
||||||
|
enableLog = tk.Checkbutton(settings_window, variable=enableLogVar)
|
||||||
|
enableLog.pack()
|
||||||
|
|
||||||
|
save_button = tk.Button(settings_window, text="Speichern", command=lambda: self.save_settings(server_url_entry.get(), access_key_entry.get(), image_folder_entry.get(), enableLogVar.get()))
|
||||||
|
save_button.pack()
|
||||||
|
|
||||||
|
config = self.load_config()
|
||||||
|
if config:
|
||||||
|
server_url_entry.insert(0, config.get("server_url", ""))
|
||||||
|
access_key_entry.insert(0, config.get("access_token", ""))
|
||||||
|
image_folder_entry.insert(0, config.get("image_path", ""))
|
||||||
|
|
||||||
|
def load_config(self):
|
||||||
try:
|
try:
|
||||||
responsedata = response.json()
|
with open("config.json", "r") as config_file:
|
||||||
return(responsedata)
|
settings = json.load(config_file)
|
||||||
except json.decoder.JSONDecodeError:
|
return settings
|
||||||
print("API response contains non-valid JSON data:")
|
except FileNotFoundError:
|
||||||
print(response.text)
|
return {}
|
||||||
except requests.exceptions.RequestException as e:
|
|
||||||
print(f"An error occurred during the API call: {e}")
|
|
||||||
|
|
||||||
def sendImages(Token, url, imagedir):
|
def get_config_File(self):
|
||||||
files = getImages(imagedir)
|
config = self.load_config()
|
||||||
tags = getTags("tags.txt")
|
|
||||||
for f in files:
|
return config
|
||||||
description = getItemDescription(f)
|
|
||||||
tagDescription = description + ' ' + tags
|
def get_setting_serverURL(self):
|
||||||
newFile = mediaUpload(Token, url, f)
|
config = self.get_config_File()
|
||||||
newFileID = newFile.get("id")
|
url = config.get("server_url")
|
||||||
createNewPost(Token, url, newFileID, tagDescription)
|
|
||||||
return True
|
return url
|
||||||
|
|
||||||
|
def get_setting_accessKey(self):
|
||||||
|
config = self.get_config_File()
|
||||||
|
accessToken = config.get("access_token")
|
||||||
|
|
||||||
|
return accessToken
|
||||||
|
|
||||||
|
def get_setting_imageFolder(self):
|
||||||
|
config = self.get_config_File()
|
||||||
|
imagedir = config.get("image_path")
|
||||||
|
|
||||||
|
return imagedir
|
||||||
|
|
||||||
|
def get_setting_log(self):
|
||||||
|
config = self.get_config_File()
|
||||||
|
isLogEnable = config.get("log")
|
||||||
|
|
||||||
|
return isLogEnable
|
||||||
|
|
||||||
|
def save_settings(self, server_url, access_key, image_path, log=False):
|
||||||
|
settings = {
|
||||||
|
"server_url": server_url,
|
||||||
|
"access_token": access_key,
|
||||||
|
"image_path": image_path,
|
||||||
|
"log": log
|
||||||
|
}
|
||||||
|
|
||||||
|
with open("config.json", "w") as config_file:
|
||||||
|
json.dump(settings, config_file)
|
||||||
|
|
||||||
|
print("Settings have been saved.")
|
||||||
|
|
||||||
|
def update_description_length(self, event):
|
||||||
|
description = self.description_text.get("1.0", tk.END)
|
||||||
|
description_length = len(description)
|
||||||
|
self.description_length_var.set(f"({description_length})")
|
||||||
|
|
||||||
|
def update_alt_length(self, event):
|
||||||
|
alt = self.alt_text.get("1.0", tk.END)
|
||||||
|
alt_length = len(alt)
|
||||||
|
self.alt_length_var.set(f"({alt_length})")
|
||||||
|
|
||||||
|
def load_images(self):
|
||||||
|
imagedir = self.get_setting_imageFolder()
|
||||||
|
if not imagedir:
|
||||||
|
folder_path = filedialog.askdirectory()
|
||||||
|
else:
|
||||||
|
folder_path = imagedir
|
||||||
|
|
||||||
|
if folder_path:
|
||||||
|
self.image_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path) if filename.lower().endswith(('.jpg', '.jpeg'))]
|
||||||
|
self.current_index = 0
|
||||||
|
self.show_current_image()
|
||||||
|
|
||||||
|
def show_current_image(self):
|
||||||
|
if self.image_paths:
|
||||||
|
image_path = self.image_paths[self.current_index]
|
||||||
|
image = Image.open(image_path)
|
||||||
|
image.thumbnail((400, 400))
|
||||||
|
photo = ImageTk.PhotoImage(image)
|
||||||
|
self.canvas.create_image(0, 0, anchor=tk.NW, image=photo)
|
||||||
|
self.canvas.image = photo
|
||||||
|
|
||||||
|
description = self.getItemDescription(image_path)
|
||||||
|
self.description_text.delete("1.0", tk.END)
|
||||||
|
self.description_text.insert(tk.END, description)
|
||||||
|
|
||||||
|
def getItemDescription(self, 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 getTags(self):
|
||||||
|
try:
|
||||||
|
file = open('tags.txt','r')
|
||||||
|
fileContent = file.read()
|
||||||
|
file.close
|
||||||
|
return fileContent
|
||||||
|
except Exception as e:
|
||||||
|
print("Error reading File:", e)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def show_previous_image(self):
|
||||||
|
if self.image_paths:
|
||||||
|
self.current_index = (self.current_index - 1) % len(self.image_paths)
|
||||||
|
self.show_current_image()
|
||||||
|
|
||||||
|
def show_next_image(self):
|
||||||
|
if self.image_paths:
|
||||||
|
self.current_index = (self.current_index + 1) % len(self.image_paths)
|
||||||
|
self.show_current_image()
|
||||||
|
|
||||||
|
def upload_image(self):
|
||||||
|
description = self.description_text.get("1.0", tk.END)
|
||||||
|
description = description + self.tag_text.get("1.0", tk.END)
|
||||||
|
#alt_text = self.alt_text.get("1.0", tk.END)
|
||||||
|
selected_image_path = self.image_paths[self.current_index]
|
||||||
|
|
||||||
|
media = self.Pixelfed.mediaUpload(selected_image_path)
|
||||||
|
if media:
|
||||||
|
mediaID = media.get("id")
|
||||||
|
#self.Pixelfed.createNewPost(mediaID, description, alt_text)
|
||||||
|
self.Pixelfed.createNewPost(mediaID, description)
|
||||||
|
|
||||||
|
print("Post created:")
|
||||||
|
print("Content:", description)
|
||||||
|
#print("Alt-Text:", alt_text)
|
||||||
|
print("Image:", selected_image_path)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
config_file_path = "config.json"
|
root = tk.Tk()
|
||||||
config = load_config(config_file_path)
|
app = ImageUploaderApp(root)
|
||||||
|
root.mainloop()
|
||||||
url = config.get("server_url")
|
|
||||||
accessToken = config.get("access_token")
|
|
||||||
imagedir = config.get("image_path")
|
|
||||||
|
|
||||||
sendImages(accessToken, url, imagedir)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user