From 691c2f2b990b582f774dc9913e1ea4511dae196e Mon Sep 17 00:00:00 2001 From: Nico Jensen Date: Tue, 15 Aug 2023 23:57:22 +0200 Subject: [PATCH] Init --- .gitignore | 4 ++ CHANGELOG.md | 8 ++++ README.md | 17 ++++++++ apikey.md | 1 + main.py | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 README.md create mode 100644 apikey.md create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b61ca94 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode/ +.DS_store +config.json +tags.txt \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..0483c70 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +#Release Notes + +##1.0.0 + +###Added +- Import all Images from Folder +- Create new Post for each Image +- Add default Tags at the end of a Content diff --git a/README.md b/README.md new file mode 100644 index 0000000..d90b8f7 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +#Pixelfed Importer + +a Python script to publish all images from a folder to Pixelfed + +##Config +To use this you need a config.json + +```JSON +{ + "server_url": "URL TO YOUR PIXELFED", + "access_token": "YOUR ACCESS TOKEN", + "image_path": "PATH TO YOUR IMAGES" +} +``` + +##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. \ No newline at end of file diff --git a/apikey.md b/apikey.md new file mode 100644 index 0000000..8848686 --- /dev/null +++ b/apikey.md @@ -0,0 +1 @@ +eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIzIiwianRpIjoiMTU4NGNhYzc2MDVlYzUxOTNhYWNjZWI4OWUzMmJiMjM2NTM1MzkyZmUyYWNmNmE1NDg2ZWU0ZmFhYTZmYzgwZDUwMjgyZTllNmJhOTcyMjIiLCJpYXQiOjE2OTIxMTM5MjQuNDY0MDI3LCJuYmYiOjE2OTIxMTM5MjQuNDY0MDMsImV4cCI6MTcyMzczNjMyNC40NjA4ODcsInN1YiI6IjEiLCJzY29wZXMiOlsid3JpdGUiLCJwdXNoIiwicmVhZCIsImZvbGxvdyJdfQ.ZEyyJDLa1oIWe4Wk3mviaDmuzqr8yYilxWSnmkgSnwS2_iLoFOj4QaZrnoXJBZUTXiFtHugkoe7tpgrnj4RdaS8-0dFuTCXnPK96Chs7ntSTcuwCXhBmZiC3e_Jy4OghqXHezyV9DrtsQvMp5zyaI4H9OPTGxLsfba_OjLwUU6zorr6nyId759PMaZr-7I2JWYRecaP8QbRDCI1R-faVMfGkyqBUyf086fKRpMUvn0B5SnWXizHDBcbRQNL-Z49_CBGdIhcQEX1mvclLTVggk3CHxGueaFaarOV2MkSYWDw7QEbED-OloviRBz1I1UAss5jlICcs6OO1x1hFy3hl75jW2IC5HKRay6zEx_4v_tmivWAki-z8rorUf7eDQG3MiHWeFKIXE1mua0XgGvH-87uagX7jMoH0ZIGBJl8zmbyFpDzUJ6a9EuNkSTHwlkpN_-b1aQF54WZVrjoVR_JOiRxgqMilBl72i1ukmssxVqS6eT39Oe-5zDu1rZI-SEHre_S_nv2U1VwFk2XC8OGIcu7CUGpFK8g7GbqaFouKEAqpfpDuMVVXCVBYCV_ve0CQCVMDAz0afZPWRbBbn_eyjD3TYBh4jzNebrDcgOL0n2sxMDLwnrr31eZ8F_XwkYuRL9cSlacA9IxrAWpiiyUhzHvkOkAUV3m36TSUpF2Ozxw \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..64d13b1 --- /dev/null +++ b/main.py @@ -0,0 +1,117 @@ +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" + + headers = { + "Authorization": f"Bearer {access_token}" + } + + data = { + "status": ImageDescription, + "media_ids": [ImageID] + } + + 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): + files = getImages(imagedir) + tags = getTags("tags.txt") + for f in files: + description = getItemDescription(f) + tagDescription = description + ' ' + tags + newFile = mediaUpload(Token, url, f) + newFileID = newFile.get("id") + 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") + + sendImages(accessToken, url, imagedir) \ No newline at end of file