diff --git a/config/template_config.json b/config/template_config.json index f6189c2..0ce38da 100644 --- a/config/template_config.json +++ b/config/template_config.json @@ -12,8 +12,8 @@ }, "nextcloud_upload_path": "", "mastodon": { - "api_key": "", - "api_key_secret": "" + "access_token": "", + "api_base_url": "" }, "discord": { "api_key": "", diff --git a/lib/TweetStreamer.py b/lib/TweetStreamer.py index 8c6595a..73a39e7 100644 --- a/lib/TweetStreamer.py +++ b/lib/TweetStreamer.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 from tweepy import StreamListener +from lib.setup import import_config_file from lib.archival import archive_media_status from lib.echo_nextcloud import nextcloud_upload_media from lib.echo_mastodon import mastodon_upload_media @@ -12,15 +13,18 @@ class TweetStreamer(StreamListener): print(status._json) archive_filenames = archive_media_status(status) if(archive_filenames): + config = import_config_file() for filename in archive_filenames: print(filename) try: - nextcloud_upload_media(filename, status.timestamp_ms) + nextcloud_upload_media( + config['nextcloud'], config['nextcloud_upload_path'], filename, status.timestamp_ms) except Exception as e: print('Was unsuccessful in uploading the file.') print(e) try: - mastodon_upload_media(archive_filenames, status.text) + mastodon_upload_media( + config['mastodon'], archive_filenames, status.text) except Exception as e: print('Was unsuccessful echoing this post to Mastodon.') print(e) diff --git a/lib/authentication.py b/lib/authentication.py index 9bf0772..aa5ff89 100644 --- a/lib/authentication.py +++ b/lib/authentication.py @@ -30,8 +30,8 @@ def authenticate_nextcloud(config): def authenticate_mastodon(config): try: - client = masto(access_token=config['mastodon']['access_token'], - api_base_url=config['mastodon']['api_base_url']) + client = masto(access_token=config['access_token'], + api_base_url=config['api_base_url']) return client except Exception as e: print('There was some error attempting to authenticate with Mastodon API') diff --git a/lib/echo_mastodon.py b/lib/echo_mastodon.py index 4f90131..f57ce52 100644 --- a/lib/echo_mastodon.py +++ b/lib/echo_mastodon.py @@ -1,15 +1,16 @@ #!/usr/bin/python3 -from lib.setup import import_config_file from lib.authentication import authenticate_mastodon -#from mastodon import Mastodon as masto -# from sys import argv -# from os.path import expanduser -def setup_mastodon_data(): - config = import_config_file() - if(config['mastodon']['access_token'] and config['mastodon']['api_base_url']): +def setup_mastodon_data(config): + """ + Setup the config import and client + + Returns: + obj -- Mastodon client object + """ + if(config['access_token'] and config['api_base_url']): client = authenticate_mastodon(config) else: print('Mastodon API information missing/incomplete, skipping...') @@ -17,7 +18,7 @@ def setup_mastodon_data(): return client -def mastodon_upload_media(media_files, body): +def mastodon_upload_media(config, media_files, body): """ Upload the archived media and original body text as a Mastodon status @@ -25,7 +26,7 @@ def mastodon_upload_media(media_files, body): media_files {str} -- Filenames for the uploaded media body {str} -- String describing the original body text of the Tweet """ - mastodon = setup_mastodon_data() + mastodon = setup_mastodon_data(config) media_id_list = [] for media in media_files: media_id_list.append(mastodon.media_post( @@ -37,71 +38,3 @@ def mastodon_upload_media(media_files, body): except Exception as e: print('There was some issue posting to Mastodon.') print(e) - - - -# def strip_quotes(input): -# """ -# Strip quotes from input arguments - -# Arguments: -# input {str} -- Input string that has quotes needing to be stripped or checked to be stripped - -# Returns: -# str -- Resulting string -# """ -# if(input[0] == '\"' and input[len(input) - 1] == '\"'): -# return input[1:len(input) - 1] -# else: -# return input - - -# def make_post(mastodon, body, cw=''): -# """ -# Post to Mastodon, using the initialized Mastodon.py object - -# Arguments: -# mastodon {obj} -- Mastodon client object -# body {str} -- String describing the body of the message - -# Keyword Arguments: -# cw {str} -- String describing the content warning of the message (default: {''}) -# """ -# try: -# output = '' -# if(cw): -# output = mastodon.status_post(status=body, spoiler_text=cw) -# else: -# output = mastodon.toot(body) -# print("Posted successfully.\nContent Warning: {0}\nContent: {1}\nCreated at: {2}\nURL: {3}".format( -# output['spoiler_text'], output['content'], output['created_at'], output['url'])) -# except Exception as e: -# print(str(e)) -# print('There was an issue.') - -# def main(): -# """ -# Main function that causes the script to post. -# """ - -# # Authorize Mastodon, set api base -# # TODO: Modularize this -# mastodon = masto( -# access_token='/home/shadow8t4/scripts/python/post-mastodon/.secrets/mastodon_api_access_token.secret', -# api_base_url='https://masto.werefox.dev' -# ) - -# cw = '' -# body = '' - -# if(len(argv) > 2): -# cw = strip_quotes(argv[1]) -# body = strip_quotes(argv[2]) -# else: -# body = strip_quotes(argv[1]) - -# make_post(mastodon, body, cw) - - -# if(__name__ == '__main__'): -# main() diff --git a/lib/echo_nextcloud.py b/lib/echo_nextcloud.py index 047e476..4bfbab8 100644 --- a/lib/echo_nextcloud.py +++ b/lib/echo_nextcloud.py @@ -5,17 +5,16 @@ from lib.setup import import_config_file from lib.authentication import authenticate_nextcloud -def setup_archive_data(timestamp): - config = import_config_file() - if(config['nextcloud']['webdav_hostname'] and config['nextcloud']['webdav_login'] and config['nextcloud']['webdav_password']): - client = authenticate_nextcloud(config['nextcloud']) +def setup_archive_data(config, timestamp): + if(config['webdav_hostname'] and config['webdav_login'] and config['webdav_password']): + client = authenticate_nextcloud(config) else: print('Nextcloud WebDAV information missing/incomplete, skipping...') return ts = dt.fromtimestamp(int(int(timestamp)/1000)) date_ts = ts.strftime('%Y_%m_%d') time_ts = ts.strftime('%H_%M_%S') - return client, config['nextcloud_upload_path'], date_ts, time_ts + return client, date_ts, time_ts def setup_archive_dir(client, upload_path, date_ts): @@ -39,8 +38,8 @@ def attempt_upload_media(client, upload_dir, upload_filename, upload_filetype, a count += 1 -def nextcloud_upload_media(archive_filename, timestamp): - client, upload_path, date_ts, time_ts = setup_archive_data(timestamp) +def nextcloud_upload_media(config, upload_path, archive_filename, timestamp): + client, date_ts, time_ts = setup_archive_data(config, timestamp) setup_archive_dir(client, upload_path, date_ts) attempt_upload_media(client, f'{upload_path}/{date_ts}', f'{date_ts}_{time_ts}', f'{archive_filename.split(".")[1]}', f'data/{archive_filename}')