2018-10-09 11:11:51 +10:00
|
|
|
#!/usr/bin/env python3
|
2021-06-16 01:29:53 +00:00
|
|
|
# SPDX-License-Identifier: EUPL-1.2
|
2018-10-09 11:11:51 +10:00
|
|
|
|
|
|
|
from mastodon import Mastodon
|
2019-07-02 20:44:48 +10:00
|
|
|
import argparse, json, re
|
2019-01-11 22:47:42 +10:00
|
|
|
import functions
|
2018-10-09 11:11:51 +10:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Generate and post a toot.')
|
2021-06-05 00:38:36 +03:00
|
|
|
parser.add_argument(
|
|
|
|
'-c', '--cfg', dest='cfg', default='config.json', nargs='?',
|
2019-08-07 13:46:57 +10:00
|
|
|
help="Specify a custom location for config.json.")
|
2021-06-05 00:38:36 +03:00
|
|
|
parser.add_argument(
|
|
|
|
'-s', '--simulate', dest='simulate', action='store_true',
|
2019-01-11 22:47:42 +10:00
|
|
|
help="Print the toot without actually posting it. Use this to make sure your bot's actually working.")
|
2018-10-09 11:11:51 +10:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-08-07 13:46:57 +10:00
|
|
|
cfg = json.load(open(args.cfg))
|
2018-10-09 11:11:51 +10:00
|
|
|
|
2019-05-12 00:54:27 +10:00
|
|
|
client = None
|
|
|
|
|
|
|
|
if not args.simulate:
|
|
|
|
client = Mastodon(
|
2021-06-05 00:38:36 +03:00
|
|
|
client_id=cfg['client']['id'],
|
|
|
|
client_secret=cfg['client']['secret'],
|
|
|
|
access_token=cfg['secret'],
|
|
|
|
api_base_url=cfg['site'])
|
2018-10-09 11:11:51 +10:00
|
|
|
|
2018-11-01 15:27:03 +10:00
|
|
|
if __name__ == '__main__':
|
2019-08-07 13:46:57 +10:00
|
|
|
toot = functions.make_toot(cfg)
|
2019-07-01 17:23:18 +10:00
|
|
|
if cfg['strip_paired_punctuation']:
|
|
|
|
toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
|
2018-11-01 15:27:03 +10:00
|
|
|
if not args.simulate:
|
|
|
|
try:
|
2021-06-05 00:38:36 +03:00
|
|
|
client.status_post(toot, visibility='unlisted', spoiler_text=cfg['cw'])
|
|
|
|
except Exception:
|
2019-07-02 20:43:34 +10:00
|
|
|
toot = "An error occurred while submitting the generated post. Contact lynnesbian@fedi.lynnesbian.space for assistance."
|
2021-06-05 00:38:36 +03:00
|
|
|
client.status_post(toot, visibility='unlisted', spoiler_text="Error!")
|
2019-02-23 10:34:22 +10:00
|
|
|
try:
|
2019-07-02 20:43:34 +10:00
|
|
|
print(toot)
|
2019-02-23 10:34:22 +10:00
|
|
|
except UnicodeEncodeError:
|
2021-06-05 00:38:36 +03:00
|
|
|
print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters
|