I need to do more cleanup/testing, but this looks to be working.

This commit is contained in:
Alex Huddleston 2020-04-29 16:59:01 +00:00
parent 97a80494c8
commit f40203878a
3 changed files with 125 additions and 0 deletions

View file

@ -3,6 +3,7 @@
from tweepy import StreamListener
from lib.archival import archive_media_status
from lib.echo_nextcloud import nextcloud_upload_media
from lib.echo_mastodon import mastodon_upload_media
import logging
@ -18,4 +19,9 @@ class TweetStreamer(StreamListener):
except Exception as e:
print('Was unsuccessful in uploading the file.')
print(e)
try:
mastodon_upload_media(archive_filenames, status.text)
except Exception as e:
print('Was unsuccessful echoing this post to Mastodon.')
print(e)
return super().on_status(status)

View file

@ -2,6 +2,7 @@
from tweepy import OAuthHandler, API
from webdav3.client import Client
from mastodon import Mastodon as masto
def authenticate_twitter(config):
@ -25,3 +26,14 @@ def authenticate_nextcloud(config):
print('There was some error attempting to authenticate through WebDAV')
print(f'\t{e}')
exit(1)
def authenticate_mastodon(config):
try:
client = masto(access_token=config['mastodon']['access_token'],
api_base_url=config['mastodon']['api_base_url'])
return client
except Exception as e:
print('There was some error attempting to authenticate with Mastodon API')
print(f'\t{e}')
exit(1)

View file

@ -0,0 +1,107 @@
#!/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']):
client = authenticate_mastodon(config)
else:
print('Mastodon API information missing/incomplete, skipping...')
return
return client
def mastodon_upload_media(media_files, body):
"""
Upload the archived media and original body text as a Mastodon status
Arguments:
media_files {str} -- Filenames for the uploaded media
body {str} -- String describing the original body text of the Tweet
"""
mastodon = setup_mastodon_data()
media_id_list = []
for media in media_files:
media_id_list.append(mastodon.media_post(
media_file=f'data/{media}', description='Sorry, this was a Twitter echo so there\'s no description :(', focus=(0, 0)))
try:
output = mastodon.status_post(
body, spoiler_text='Automated Twitter Repost', media_ids=media_id_list, visibility='public')
print(output)
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()