Bot now regenerates post if it contains a filtered word

This commit is contained in:
Amber 2022-03-27 11:25:53 -05:00
parent dd503789e0
commit 9f1163eda0
3 changed files with 23 additions and 7 deletions

View File

@ -56,15 +56,17 @@ def make_sentence(output, cfg):
elif cfg['mention_handling'] == 0: elif cfg['mention_handling'] == 0:
sentence = re.sub(r"\S*@\u200B\S*\s?", "", sentence) 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: if cfg['word_filter'] == 1:
try: try:
fp = open('./filter.txt') fp = open('./filter.txt')
for word in fp: for word in fp:
if word in sentence: word = re.sub("\n", "", word)
sentence = sentence.replace(word, "[REDACTED]") if word in sentence:
sentence=""
finally: finally:
fp.close() fp.close()
output.send(sentence) output.send(sentence)
@ -81,7 +83,7 @@ def make_toot(cfg):
toot = pin.recv() toot = pin.recv()
if toot is None: if toot is None:
toot = "Toot generation failed! Contact Lynne (lynnesbian@fedi.lynnesbian.space) for assistance." toot = "post failed"
return toot return toot

16
gen.py
View File

@ -34,11 +34,23 @@ if __name__ == '__main__':
toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot) toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
if not args.simulate: if not args.simulate:
try: 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: 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!") client.status_post(toot, visibility='unlisted', spoiler_text="Error!")
try: try:
print(toot) print(toot)
if str(toot) == "":
print("Filtered")
else:
print(toot)
except UnicodeEncodeError: except UnicodeEncodeError:
print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters print(toot.encode("ascii", "ignore")) # encode as ASCII, dropping any non-ASCII characters

View File

@ -76,6 +76,8 @@ class ReplyListener(mastodon.StreamListener):
print("User is not valid") print("User is not valid")
else: else:
toot = functions.make_toot(cfg) # generate a toot 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 @ toot = acct + " " + toot # prepend the @
print(acct + " says " + mention) # logging print(acct + " says " + mention) # logging
visibility = notification['status']['visibility'] visibility = notification['status']['visibility']