From 9f1163eda06254fdd911a45a1e54a2437c3b078e Mon Sep 17 00:00:00 2001 From: Amber Date: Sun, 27 Mar 2022 11:25:53 -0500 Subject: [PATCH] Bot now regenerates post if it contains a filtered word --- functions.py | 12 +++++++----- gen.py | 16 ++++++++++++++-- reply.py | 2 ++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/functions.py b/functions.py index f4daa6b..8fbcc22 100755 --- a/functions.py +++ b/functions.py @@ -56,15 +56,17 @@ def make_sentence(output, cfg): elif cfg['mention_handling'] == 0: sentence = re.sub(r"\S*@\u200B\S*\s?", "", sentence) - # optionally remove filtered words + # optionally regenerate the post if it has a filtered word. TODO: case-insensitivity, scuntthorpe problem if cfg['word_filter'] == 1: try: fp = open('./filter.txt') for word in fp: - if word in sentence: - sentence = sentence.replace(word, "[REDACTED]") + word = re.sub("\n", "", word) + if word in sentence: + sentence="" + finally: - fp.close() + fp.close() output.send(sentence) @@ -81,7 +83,7 @@ def make_toot(cfg): toot = pin.recv() if toot is None: - toot = "Toot generation failed! Contact Lynne (lynnesbian@fedi.lynnesbian.space) for assistance." + toot = "post failed" return toot diff --git a/gen.py b/gen.py index 5231764..17672a8 100755 --- a/gen.py +++ b/gen.py @@ -34,11 +34,23 @@ if __name__ == '__main__': toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot) if not args.simulate: try: - client.status_post(toot, visibility='unlisted', spoiler_text=cfg['cw']) + 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 = "An error occurred while submitting the generated post. Contact lynnesbian@fedi.lynnesbian.space for assistance." + toot = "@amber@toot.site Something went fucky" client.status_post(toot, visibility='unlisted', spoiler_text="Error!") try: print(toot) + if str(toot) == "": + print("Filtered") + else: + print(toot) except UnicodeEncodeError: print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters diff --git a/reply.py b/reply.py index 799313e..e14b5f1 100755 --- a/reply.py +++ b/reply.py @@ -76,6 +76,8 @@ class ReplyListener(mastodon.StreamListener): print("User is not valid") else: toot = functions.make_toot(cfg) # generate a toot + if toot == "": # Regenerate the post if it contains a blacklisted word + toot = functions.make_toot(cfg) toot = acct + " " + toot # prepend the @ print(acct + " says " + mention) # logging visibility = notification['status']['visibility']