rewrite for anyio+aiohttp

This commit is contained in:
io 2021-06-16 01:59:57 +00:00
parent 93095f62f3
commit 5d1c3397b6
8 changed files with 361 additions and 266 deletions

55
gen.py
View file

@ -1,42 +1,43 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: EUPL-1.2
from mastodon import Mastodon
import argparse, json, re
import re
import functions
from pleroma import Pleroma
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',
help="Print the toot without actually posting it. Use this to make sure your bot's actually working.")
def parse_args():
parser = functions.arg_parser_factory(description='Generate and post a toot.')
parser.add_argument(
'-s', '--simulate', dest='simulate', action='store_true',
help="Print the toot without actually posting it. Use this to make sure your bot's actually working.")
return parser.parse_args()
args = parser.parse_args()
async def main():
args = parse_args()
cfg = functions.load_config(args.cfg)
cfg = json.load(open(args.cfg))
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'])
if __name__ == '__main__':
toot = functions.make_toot(cfg)
if cfg['strip_paired_punctuation']:
toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
if not args.simulate:
try:
client.status_post(toot, visibility='unlisted', spoiler_text=cfg['cw'])
except Exception:
toot = "An error occurred while submitting the generated post. Contact lynnesbian@fedi.lynnesbian.space for assistance."
client.status_post(toot, visibility='unlisted', spoiler_text="Error!")
async with Pleroma(api_base_url=cfg['site'], access_token=cfg['access_token']) as pl:
try:
await pl.post(toot, visibility='unlisted', cw=cfg['cw'])
except Exception:
import traceback
toot = (
'An error occurred while submitting the generated post. '
'Contact io@csdisaster.club for assistance. Full traceback:\n\n'
+ traceback.format_exc()
)
await pl.status_post(toot, visibility='unlisted', cw='Error!')
raise
try:
print(toot)
except UnicodeEncodeError:
print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters
if __name__ == '__main__':
import anyio
anyio.run(main)