Moved config import to TweetStreamer Class definition, rather than importing for each service API.

This commit is contained in:
Alex Huddleston 2020-04-29 18:16:35 +00:00
parent f40203878a
commit 154bf4c3e4
5 changed files with 26 additions and 90 deletions

View file

@ -12,8 +12,8 @@
},
"nextcloud_upload_path": "",
"mastodon": {
"api_key": "",
"api_key_secret": ""
"access_token": "",
"api_base_url": ""
},
"discord": {
"api_key": "",

View file

@ -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)

View file

@ -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')

View file

@ -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()

View file

@ -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}')