import load_config

from mastodon import Mastodon

m = Mastodon(access_token=load_config.config['pixelfed']['token'], api_base_url="https://pixelfed.scot")

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