Add new Upload Type

This commit is contained in:
Nico Jensen 2023-10-29 03:25:04 +01:00
parent fe62d71067
commit ed6552fe41
2 changed files with 140 additions and 2 deletions

132
PixelfedImporter.py Normal file
View File

@ -0,0 +1,132 @@
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"
ImageIDs = ImageID.split(",")
ImageIDsInt = list(map(int, ImageIDs))
headers = {
"Authorization": f"Bearer {access_token}"
}
data = {
"status": ImageDescription,
"media_ids": ImageIDsInt
}
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, uploadType):
files = getImages(imagedir)
tags = getTags("tags.txt")
tagDescription = ""
newFileID = ""
for f in files:
newFile = mediaUpload(Token, url, f)
description = getItemDescription(f)
if uploadType == 0:
tagDescription = description + ' ' + tags
newFileID = newFile.get("id")
createNewPost(Token, url, newFileID, tagDescription)
else:
if tagDescription == "":
tagDescription = description + ' ' + tags
if newFileID != "":
newFileID = newFileID + "," + newFile.get("id")
else:
newFileID = newFile.get("id")
if uploadType == 1:
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")
uploadType = config.get("upload_type")
sendImages(accessToken, url, imagedir, uploadType)

View File

@ -1,6 +1,7 @@
# Pixelfed Importer # Pixelfed Importer
a Python app to publish all images from a folder to Pixelfed a Python app to publish all images from a folder to Pixelfed.
This script use Image Meta Tag Description fot the Post Description.
## Config ## Config
@ -10,10 +11,15 @@ To use this you need a config.json if this cannot be created automatically, here
{ {
"server_url": "URL TO YOUR PIXELFED", "server_url": "URL TO YOUR PIXELFED",
"access_token": "YOUR ACCESS TOKEN", "access_token": "YOUR ACCESS TOKEN",
"image_path": "PATH TO YOUR IMAGES" "image_path": "PATH TO YOUR IMAGES",
"upload_type": 0|1
} }
``` ```
upload_type 0 creates 1 post for each image from your folder (image_path)
upload_type 1 creates 1 post with all images from your folder (image_path)
Uploads at Type 1 only use Image Meta Tag Description from the first image for the post description.
## tags ## 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. You can add a tags.txt, in which standard tags can be stored, which will always be added at the end of a post.