From 0b7d7fe58e2872b07e2b9f21db91890242a322c1 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sun, 5 Nov 2023 19:20:03 +0000 Subject: [PATCH] Complete Pixelfed/Mastodon publishing --- image_processing.py | 2 +- main.py | 1 + publish_flickr.py | 2 +- publish_pixelfed.py | 21 ++++++++++++++++++--- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/image_processing.py b/image_processing.py index d56164d..18a4f9a 100644 --- a/image_processing.py +++ b/image_processing.py @@ -19,4 +19,4 @@ def get_image_data(path: str): 'alt': alt_text, 'tags': tags } - return image_data + return image_data \ No newline at end of file diff --git a/main.py b/main.py index 47aaf90..bb339ac 100644 --- a/main.py +++ b/main.py @@ -44,6 +44,7 @@ def main(): if load_config.config['pixelfed']['enable']: print("Pixelfed publishing enabled") import publish_pixelfed + publish_pixelfed.upload(file_data) def list_files_in_directory(directory): top_level_items = os.listdir(directory) diff --git a/publish_flickr.py b/publish_flickr.py index b7f29fd..953a9de 100644 --- a/publish_flickr.py +++ b/publish_flickr.py @@ -10,7 +10,7 @@ def upload(file_data: list): flickr.upload( filename = file['path'], title = file['title'], - description = file['description'], + description = file['description'] + load_config.config['flickr']['add_to_description'], tags = prepareTags(file['tags']), is_public = 1, format = 'rest' diff --git a/publish_pixelfed.py b/publish_pixelfed.py index 51398a1..d78edab 100644 --- a/publish_pixelfed.py +++ b/publish_pixelfed.py @@ -2,7 +2,22 @@ import load_config from mastodon import Mastodon -Mastodon.create_app("social_photos", api_base_url=load_config.config['pixelfed']['server'], user_agent="social-photos") -Mastodon.log_in(code=load_config['pixelfed']['token']) +m = Mastodon(access_token=load_config.config['pixelfed']['token'], api_base_url="https://pixelfed.scot") -mastodon = Mastodon(access_token = 'pytooter_usercred.secret') \ No newline at end of file +def upload(file_data: list): + for file in file_data: + print("Uploading image to Pixelfed") + media_upload = m.media_post(file['path'], "image/jpeg") + print("Posting to Pixelfed") + post_upload = m.status_post(formatPost(file), media_ids = media_upload['id']) + +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{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 \ No newline at end of file