2018-10-08 21:11:51 -04:00
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from mastodon import Mastodon
2018-10-14 02:58:58 -04:00
import argparse , sys , traceback , json
2019-01-11 07:47:42 -05:00
import functions
2018-10-08 21:11:51 -04:00
parser = argparse . ArgumentParser ( description = ' Generate and post a toot. ' )
parser . add_argument ( ' -s ' , ' --simulate ' , dest = ' simulate ' , action = ' store_true ' ,
2019-01-11 07:47:42 -05:00
help = " Print the toot without actually posting it. Use this to make sure your bot ' s actually working. " )
2018-10-08 21:11:51 -04:00
args = parser . parse_args ( )
2018-10-25 10:35:25 -04:00
cfg = json . load ( open ( ' config.json ' ) )
2018-10-08 21:11:51 -04:00
client = Mastodon (
2018-10-25 10:35:25 -04:00
client_id = cfg [ ' client ' ] [ ' id ' ] ,
client_secret = cfg [ ' client ' ] [ ' secret ' ] ,
access_token = cfg [ ' secret ' ] ,
api_base_url = cfg [ ' site ' ] )
2018-10-08 21:11:51 -04:00
2018-11-01 01:27:03 -04:00
if __name__ == ' __main__ ' :
2019-01-11 07:47:42 -05:00
toot = functions . make_toot ( )
2018-11-01 01:27:03 -04:00
if not args . simulate :
try :
if toot [ ' media ' ] != None :
mediaID = client . media_post ( toot [ ' media ' ] , description = toot [ ' toot ' ] )
client . status_post ( toot [ ' toot ' ] . replace ( " \n " , " " ) ,
2018-11-24 14:13:02 -05:00
media_ids = [ mediaID ] , visibility = " unlisted " , spoiler_text = cfg [ ' cw ' ] )
2018-11-01 01:27:03 -04:00
else :
2018-11-24 14:13:02 -05:00
client . status_post ( toot [ ' toot ' ] , visibility = ' unlisted ' , spoiler_text = cfg [ ' cw ' ] )
2018-11-01 01:27:03 -04:00
except Exception as err :
toot = {
2019-01-11 07:47:42 -05:00
" toot " : " An unknown error that should never happen occurred. Maybe it ' s because of the spoiler text, which is {} . If not, I have no idea what went wrong. This is an error message -- contact lynnesbian@fedi.lynnesbian.space for assistance. " . format ( cfg [ ' cw ' ] )
2018-11-01 01:27:03 -04:00
}
client . status_post ( toot [ ' toot ' ] , visibility = ' unlisted ' , spoiler_text = " Error! " )
print ( toot [ ' toot ' ] )