Bot now regenerates post if it contains a filtered word #1

Merged
amber merged 1 commits from dev into master 2022-03-27 16:27:30 +00:00
3 changed files with 23 additions and 7 deletions
Showing only changes of commit 9f1163eda0 - Show all commits

View File

@ -56,13 +56,15 @@ 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:
word = re.sub("\n", "", word)
if word in sentence: if word in sentence:
sentence = sentence.replace(word, "[REDACTED]") 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

14
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:
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']) 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']