2018-10-08 19:11:51 -06: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
|
2019-07-01 01:23:18 -06:00
|
|
|
import argparse, sys, traceback, json, re
|
2019-01-11 05:47:42 -07:00
|
|
|
import functions
|
2018-10-08 19:11:51 -06:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Generate and post a toot.')
|
|
|
|
parser.add_argument('-s', '--simulate', dest='simulate', action='store_true',
|
2019-01-11 05:47:42 -07:00
|
|
|
help="Print the toot without actually posting it. Use this to make sure your bot's actually working.")
|
2018-10-08 19:11:51 -06:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-10-25 08:35:25 -06:00
|
|
|
cfg = json.load(open('config.json'))
|
2018-10-08 19:11:51 -06:00
|
|
|
|
2019-05-11 08:54:27 -06:00
|
|
|
client = None
|
|
|
|
|
|
|
|
if not args.simulate:
|
|
|
|
client = Mastodon(
|
|
|
|
client_id=cfg['client']['id'],
|
2019-05-19 07:06:31 -06:00
|
|
|
client_secret=cfg['client']['secret'],
|
|
|
|
access_token=cfg['secret'],
|
2019-05-11 08:54:27 -06:00
|
|
|
api_base_url=cfg['site'])
|
2018-10-08 19:11:51 -06:00
|
|
|
|
2018-10-31 23:27:03 -06:00
|
|
|
if __name__ == '__main__':
|
2019-01-11 05:47:42 -07:00
|
|
|
toot = functions.make_toot()
|
2019-07-01 01:23:18 -06:00
|
|
|
if cfg['strip_paired_punctuation']:
|
|
|
|
toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
|
2018-10-31 23:27:03 -06: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 12:13:02 -07:00
|
|
|
media_ids = [mediaID], visibility = "unlisted", spoiler_text = cfg['cw'])
|
2018-10-31 23:27:03 -06:00
|
|
|
else:
|
2018-11-24 12:13:02 -07:00
|
|
|
client.status_post(toot['toot'], visibility = 'unlisted', spoiler_text = cfg['cw'])
|
2018-10-31 23:27:03 -06:00
|
|
|
except Exception as err:
|
|
|
|
toot = {
|
2019-05-19 07:06:31 -06:00
|
|
|
"toot": "An error occurred while submitting the generated post. Contact lynnesbian@fedi.lynnesbian.space for assistance."
|
2018-10-31 23:27:03 -06:00
|
|
|
}
|
|
|
|
client.status_post(toot['toot'], visibility = 'unlisted', spoiler_text = "Error!")
|
2019-02-22 17:34:22 -07:00
|
|
|
try:
|
|
|
|
print(toot['toot'])
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
print(toot['toot'].encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters
|