social-photos/social_photos/publish_pixelfed.py

35 lines
1.4 KiB
Python
Raw Normal View History

import load_config
from mastodon import Mastodon
2023-11-05 23:58:53 +00:00
import datetime, pytz
2023-11-05 21:13:57 +00:00
m = Mastodon(access_token=load_config.config['pixelfed']['token'], api_base_url=load_config.config['pixelfed']['server'])
2023-11-05 19:20:03 +00:00
def upload(file_data: list):
for file in file_data:
print("Uploading image to Pixelfed")
2023-11-05 23:58:53 +00:00
media_upload = m.media_post(file['path'], description=file['alt'])
2023-11-05 19:20:03 +00:00
print("Posting to Pixelfed")
2023-11-05 21:51:57 +00:00
post_upload = m.status_post(formatPost(file), media_ids = media_upload['id'], visibility="public")
2023-11-05 23:58:53 +00:00
print(post_upload)
2023-11-05 21:13:57 +00:00
return post_upload['url']
2023-11-05 19:20:03 +00:00
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']
2023-11-06 00:05:44 +00:00
return f"{file['title']} | {file['description']} \n {formatDate(file['create'])} \n {tag_string}"
2023-11-05 19:20:03 +00:00
def formatTags(tags: list):
formatted_tags = ['#' + tag.title().replace(' ', '') for tag in tags]
formatted_tags_str = ' '.join(formatted_tags)
2023-11-05 23:58:53 +00:00
return formatted_tags_str
def formatDate(input_date_str: str):
print(input_date_str)
2023-11-05 23:58:53 +00:00
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