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

View File

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

16
gen.py
View File

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

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