This commit is contained in:
Nico Jensen 2023-08-15 23:57:22 +02:00
commit 691c2f2b99
5 changed files with 147 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.vscode/
.DS_store
config.json
tags.txt

8
CHANGELOG.md Normal file
View File

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

17
README.md Normal file
View File

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

1
apikey.md Normal file
View File

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

117
main.py Normal file
View File

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