2018-10-09 03:11:51 +02:00
|
|
|
#!/usr/bin/env python3
|
2021-07-26 06:52:13 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2018-10-09 03:11:51 +02:00
|
|
|
|
2021-06-16 03:59:57 +02:00
|
|
|
import re
|
2021-07-26 06:52:13 +02:00
|
|
|
from third_party import utils
|
2021-06-16 03:59:57 +02:00
|
|
|
from pleroma import Pleroma
|
2018-10-09 03:11:51 +02:00
|
|
|
|
2021-06-16 03:59:57 +02:00
|
|
|
def parse_args():
|
2021-07-26 06:52:13 +02:00
|
|
|
parser = utils.arg_parser_factory(description='Generate and post a toot.')
|
2021-06-16 03:59:57 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'-s', '--simulate', dest='simulate', action='store_true',
|
2021-07-26 06:52:13 +02:00
|
|
|
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-16 03:59:57 +02:00
|
|
|
return parser.parse_args()
|
2018-10-09 03:11:51 +02:00
|
|
|
|
2021-08-17 07:55:29 +02:00
|
|
|
PAIRED_PUNCTUATION = re.compile(r"[{}]".format(re.escape('[](){}"‘’“”«»„')))
|
|
|
|
|
2021-06-16 03:59:57 +02:00
|
|
|
async def main():
|
|
|
|
args = parse_args()
|
2021-07-26 06:52:13 +02:00
|
|
|
cfg = utils.load_config(args.cfg)
|
2018-10-09 03:11:51 +02:00
|
|
|
|
2021-07-26 08:29:20 +02:00
|
|
|
toot = await utils.make_post(cfg, mode=utils.TextGenerationMode.__members__[args.mode])
|
2019-07-01 09:23:18 +02:00
|
|
|
if cfg['strip_paired_punctuation']:
|
2021-08-17 07:55:29 +02:00
|
|
|
toot = PAIRED_PUNCTUATION.sub("", toot)
|
|
|
|
toot = toot.replace("@", "@\u200b") # sanitize mentions
|
|
|
|
toot = utils.remove_mentions(cfg, toot)
|
|
|
|
|
2018-11-01 06:27:03 +01:00
|
|
|
if not args.simulate:
|
2021-06-16 03:59:57 +02: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()
|
|
|
|
)
|
2021-09-21 18:55:45 +02:00
|
|
|
await pl.post(toot, visibility='unlisted', cw='Error!')
|
2021-06-16 03:59:57 +02:00
|
|
|
raise
|
|
|
|
|
2019-02-23 01:34:22 +01:00
|
|
|
try:
|
2019-07-02 12:43:34 +02:00
|
|
|
print(toot)
|
2019-02-23 01:34:22 +01:00
|
|
|
except UnicodeEncodeError:
|
2021-06-04 23:38:36 +02:00
|
|
|
print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters
|
2021-06-16 03:59:57 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import anyio
|
|
|
|
anyio.run(main)
|