pleroma-ebooks/gen.py

44 lines
1.3 KiB
Python
Raw Normal View History

2018-10-08 19:11:51 -06:00
#!/usr/bin/env python3
2021-06-15 19:29:53 -06:00
# SPDX-License-Identifier: EUPL-1.2
2018-10-08 19:11:51 -06:00
2021-06-15 19:59:57 -06:00
import re
2019-01-11 05:47:42 -07:00
import functions
2021-06-15 19:59:57 -06:00
from pleroma import Pleroma
2018-10-08 19:11:51 -06:00
2021-06-15 19:59:57 -06:00
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()
2018-10-08 19:11:51 -06:00
2021-06-15 19:59:57 -06:00
async def main():
args = parse_args()
cfg = functions.load_config(args.cfg)
2018-10-08 19:11:51 -06:00
toot = functions.make_toot(cfg)
2019-07-01 01:23:18 -06:00
if cfg['strip_paired_punctuation']:
toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
if not args.simulate:
2021-06-15 19:59:57 -06:00
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:
2019-07-02 04:43:34 -06:00
print(toot)
except UnicodeEncodeError:
print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters
2021-06-15 19:59:57 -06:00
if __name__ == '__main__':
import anyio
anyio.run(main)