From 8fa0ab3a8de8fb2bb11a703c9fc9323954dfb5b9 Mon Sep 17 00:00:00 2001 From: Thomas L Date: Sun, 25 Jun 2017 19:15:38 +0200 Subject: [PATCH] refactor filter --- {blacklist => blacklists}/nbg_blacklist | 0 {goodlist => goodlists}/nbg_goodlist | 0 retootbot.py | 19 ++++-- retweetbot.py | 4 +- trigger.py | 85 +++++++++---------------- 5 files changed, 46 insertions(+), 62 deletions(-) rename {blacklist => blacklists}/nbg_blacklist (100%) rename {goodlist => goodlists}/nbg_goodlist (100%) diff --git a/blacklist/nbg_blacklist b/blacklists/nbg_blacklist similarity index 100% rename from blacklist/nbg_blacklist rename to blacklists/nbg_blacklist diff --git a/goodlist/nbg_goodlist b/goodlists/nbg_goodlist similarity index 100% rename from goodlist/nbg_goodlist rename to goodlists/nbg_goodlist diff --git a/retootbot.py b/retootbot.py index b9530fb..581770c 100644 --- a/retootbot.py +++ b/retootbot.py @@ -7,6 +7,8 @@ import pickle import re import time +import trigger + class RetootBot(object): def __init__(self, config, filter): @@ -55,16 +57,18 @@ class RetootBot(object): retoots = [] for notification in self.m.notifications(): if (notification['type'] == 'mention' - and notification['status']['id'] not in self.seen_toots - and self.filter.check_string(notification['status']['content'])): + and notification['status']['id'] not in self.seen_toots): + self.seen_toots.add(notification['status']['id']) + text_content = re.sub('<[^>]*>', '', + notification['status']['content']) + if not self.filter.is_ok(text_content): + continue print('Boosting toot %d from %s: %s' % ( notification['status']['id'], notification['status']['account']['acct'], notification['status']['content'])) self.m.status_reblog(notification['status']['id']) - retoots.append(re.sub('<[^>]*>', '', - notification['status']['content'])) - self.seen_toots.add(notification['status']['id']) + retoots.append(text_content) # save state with open('seen_toots.pickle.part', 'xb') as f: @@ -78,7 +82,10 @@ class RetootBot(object): if __name__ == '__main__': # read config in TOML format (https://github.com/toml-lang/toml#toml) with open('ticketfrei.cfg') as configfile: - bot = RetootBot(toml.load(configfile)) + config = toml.load(configfile) + + filter = trigger.Trigger(config) + bot = RetootBot(config, filter) while True: bot.retoot() diff --git a/retweetbot.py b/retweetbot.py index 17b3f1a..c9b3c6e 100644 --- a/retweetbot.py +++ b/retweetbot.py @@ -165,7 +165,7 @@ class RetweetBot(object): for status in mentions: # Is the Text of the Tweet in the triggerlist? - if self.trigger.check_string(status.text): + if self.trigger.is_ok(status.text): # Retweet status mastodon.append(self.retweet(status)) @@ -176,7 +176,7 @@ class RetweetBot(object): def shutdown(self): """ If something breaks, it shuts down the bot and messages the owner. """ - print "[ERROR] Shit went wrong, closing down." + print("[ERROR] Shit went wrong, closing down.") with open(self.historypath, "w") as f: f.write(str(self.last_mention)) self.api.PostDirectMessage("Help! I broke down. restart me pls :$", self.user_id, self.screen_name) diff --git a/trigger.py b/trigger.py index 7be2246..c5715c7 100644 --- a/trigger.py +++ b/trigger.py @@ -1,9 +1,8 @@ #!/usr/bin/env python -__encoding__ = "utf-8" - import os import pytoml as toml + class Trigger(object): """ This class provides a filter to test a string against. @@ -11,85 +10,63 @@ class Trigger(object): def __init__(self, config): self.config = config - - self.goodlistpath = config['trigger']['goodlist_path'] + try: + self.goodlistpath = config['trigger']['goodlist_path'] + except KeyError: + self.goodlistpath = 'goodlists' self.goodlist = self.get_lists(self.goodlistpath) - self.goodlist = self.strings_ok(self.goodlist) - self.blacklistpath = config['trigger']['blacklist_path'] + try: + self.blacklistpath = config['trigger']['blacklist_path'] + except KeyError: + self.blacklistpath = 'blacklists' self.blacklist = self.get_lists(self.blacklistpath) - self.blacklist = self.strings_ok(self.blacklist) def get_lists(self, path): """ - pass a folder with text files in it. each line in the files becomes a filter word. + pass a folder with text files in it. each line in the files becomes a + filter word. :param path: path to folder whose files shall be added to the set :return: set of trigger words. """ trigger_words = set() for filename in os.listdir(path): - with open(path + filename, "r+") as f: - [trigger_words.add(s.strip()) for s in f.readlines()] + with open(os.path.join(path, filename), "r+") as listfile: + for word in listfile: + word = word.strip() + if word: + trigger_words.add(word) return trigger_words - def strings_ok(self, filterlist): - """ - Checks if an empty line is in a list and removes it. - :param filterlist: a good- or blacklist. - :return: filterlist: a corrected list. - """ - for word in filterlist: - if word == "\n": - del word - return filterlist - - def check_string(self, string): + def is_ok(self, message): """ checks if a string contains no bad words and at least 1 good word. - :param string: A given string. Tweet or Toot, cleaned from html. + :param message: A given string. Tweet or Toot, cleaned from html. :return: If the string passes the test """ - string = unicode.decode(string) - for triggerword in self.goodlist: - if string.lower().find(triggerword) != -1: - for triggerword in self.blacklist: - if string.lower().find(triggerword) != -1: - return False - return True - return False + ret = False + for word in message.lower().split(): + if word in self.goodlist: + ret = True + if word in self.blacklist: + return False + return ret - def add_to_list(self, word, whichlist): - """ - - :param word: a string of a word which should be appended to one of the lists - :param boolean whichlist: 0 : goodlist, 1 : blacklist. - """ - if whichlist: - path = self.goodlistpath - else: - path = self.blacklistpath - with open(path, "w") as f: - old = f.readlines() - old.append(word) - f.writelines(old) if __name__ == "__main__": with open("ticketfrei.cfg", "r") as configfile: config = toml.load(configfile) - print "testing the trigger" + print("testing the trigger") trigger = Trigger(config) - print "Printing words which trigger the bot:" + print("Printing words which trigger the bot:") for i in trigger.goodlist: - print i - print + print(i) + print() - print "Printing words which block a bot:" + print("Printing words which block a bot:") for i in trigger.blacklist: - print i - print - - + print(i)