AgathaSorceress-mstdn-ebooks/gen.py

41 lines
1.4 KiB
Python
Raw Normal View History

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