social-photos/social_photos/publish_pixelfed.py

35 lines
1.4 KiB
Python

import load_config
from mastodon import Mastodon
import datetime, pytz
m = Mastodon(access_token=load_config.config['pixelfed']['token'], api_base_url=load_config.config['pixelfed']['server'])
def upload(file_data: list):
for file in file_data:
print("Uploading image to Pixelfed")
media_upload = m.media_post(file['path'], description=file['alt'])
print("Posting to Pixelfed")
post_upload = m.status_post(formatPost(file), media_ids = media_upload['id'], visibility="public")
print(post_upload)
return post_upload['url']
def formatPost(file: dict):
tag_string = formatTags(file['tags'])
if load_config.config['pixelfed']['add_to_description']:
file['description'] = file['description'] + load_config.config['pixelfed']['add_to_description']
return f"{file['title']} | {file['description']} \n {formatDate(file['create'])} \n {tag_string}"
def formatTags(tags: list):
formatted_tags = ['#' + tag.title().replace(' ', '') for tag in tags]
formatted_tags_str = ' '.join(formatted_tags)
return formatted_tags_str
def formatDate(input_date_str: str):
print(input_date_str)
input_datetime = datetime.datetime.fromisoformat(input_date_str)
london_tz = pytz.timezone('Europe/London')
london_datetime = input_datetime.replace(tzinfo=pytz.utc).astimezone(london_tz)
output = london_datetime.strftime("%d/%m/%Y")
print(output)
return output