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

14
gen.py
View File

@ -34,11 +34,23 @@ if __name__ == '__main__':
toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
if not args.simulate:
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'])
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

View File

@ -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']