amber-ebooks/gen.py

56 lines
1.9 KiB
Python
Raw Normal View History

2018-10-09 01:11:51 +00: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 10:44:48 +00:00
import argparse, json, re
2019-01-11 12:47:42 +00:00
import functions
2018-10-09 01:11:51 +00:00
parser = argparse.ArgumentParser(description='Generate and post a toot.')
parser.add_argument(
'-c', '--cfg', dest='cfg', default='config.json', nargs='?',
help="Specify a custom location for config.json.")
parser.add_argument(
'-s', '--simulate', dest='simulate', action='store_true',
2019-01-11 12:47:42 +00:00
help="Print the toot without actually posting it. Use this to make sure your bot's actually working.")
2018-10-09 01:11:51 +00:00
args = parser.parse_args()
cfg = json.load(open(args.cfg))
2018-10-09 01:11:51 +00:00
2019-05-11 14:54:27 +00:00
client = None
if not args.simulate:
client = Mastodon(
client_id=cfg['client']['id'],
client_secret=cfg['client']['secret'],
access_token=cfg['secret'],
api_base_url=cfg['site'])
2018-10-09 01:11:51 +00:00
if __name__ == '__main__':
toot = functions.make_toot(cfg)
2019-07-01 07:23:18 +00:00
if cfg['strip_paired_punctuation']:
toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
if not args.simulate:
try:
if toot == "":
print("Post has been filtered, or post generation has failed")
toot = functions.make_toot(cfg)
if toot == "":
client.status_post("Recusrsion is a bitch. Post generation failed.", visibility='unlisted', spoiler_text=cfg['cw'])
else:
client.status_post(toot, visibility='unlisted', spoiler_text=cfg['cw'])
else:
client.status_post(toot, visibility='unlisted', spoiler_text=cfg['cw'])
except Exception:
toot = "@amber@toot.site Something went fucky"
client.status_post(toot, visibility='unlisted', spoiler_text="Error!")
try:
if str(toot) == "":
print("Filtered")
else:
print(toot)
except UnicodeEncodeError:
print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters