pleroma-ebooks/gen.py

47 lines
1.4 KiB
Python
Raw Normal View History

2018-10-08 18:11:51 -07:00
#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-only
2018-10-08 18:11:51 -07:00
2021-06-15 18:59:57 -07:00
import re
from third_party import utils
2021-06-15 18:59:57 -07:00
from pleroma import Pleroma
2018-10-08 18:11:51 -07:00
2021-06-15 18:59:57 -07:00
def parse_args():
parser = utils.arg_parser_factory(description='Generate and post a toot.')
2021-06-15 18:59:57 -07:00
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.",
)
parser.add_argument(
'-m', '--mode',
default='markov',
help='Pass one of these: ' + ', '.join(utils.TextGenerationMode.__members__),
)
2021-06-15 18:59:57 -07:00
return parser.parse_args()
2018-10-08 18:11:51 -07:00
2021-08-16 22:55:29 -07:00
PAIRED_PUNCTUATION = re.compile(r"[{}]".format(re.escape('[](){}"‘’“”«»„')))
2021-06-15 18:59:57 -07:00
async def main():
args = parse_args()
cfg = utils.load_config(args.cfg)
2018-10-08 18:11:51 -07:00
2021-07-25 23:29:20 -07:00
toot = await utils.make_post(cfg, mode=utils.TextGenerationMode.__members__[args.mode])
2019-07-01 00:23:18 -07:00
if cfg['strip_paired_punctuation']:
2021-08-16 22:55:29 -07:00
toot = PAIRED_PUNCTUATION.sub("", toot)
toot = toot.replace("@", "@\u200b") # sanitize mentions
toot = utils.remove_mentions(cfg, toot)
try:
2019-07-02 03:43:34 -07:00
print(toot)
except UnicodeEncodeError:
print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters
2021-06-15 18:59:57 -07:00
2022-08-11 17:33:34 -07:00
if args.simulate:
return
async with Pleroma(api_base_url=cfg['site'], access_token=cfg['access_token']) as pl:
await pl.post(toot, visibility='unlisted', cw=cfg['cw'])
2021-06-15 18:59:57 -07:00
if __name__ == '__main__':
import anyio
anyio.run(main)