refactor filter

This commit is contained in:
Thomas L 2017-06-25 19:15:38 +02:00
parent 3c7ff8f505
commit 8fa0ab3a8d
5 changed files with 46 additions and 62 deletions

View file

@ -7,6 +7,8 @@ import pickle
import re import re
import time import time
import trigger
class RetootBot(object): class RetootBot(object):
def __init__(self, config, filter): def __init__(self, config, filter):
@ -55,16 +57,18 @@ class RetootBot(object):
retoots = [] retoots = []
for notification in self.m.notifications(): for notification in self.m.notifications():
if (notification['type'] == 'mention' if (notification['type'] == 'mention'
and notification['status']['id'] not in self.seen_toots and notification['status']['id'] not in self.seen_toots):
and self.filter.check_string(notification['status']['content'])): 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' % ( print('Boosting toot %d from %s: %s' % (
notification['status']['id'], notification['status']['id'],
notification['status']['account']['acct'], notification['status']['account']['acct'],
notification['status']['content'])) notification['status']['content']))
self.m.status_reblog(notification['status']['id']) self.m.status_reblog(notification['status']['id'])
retoots.append(re.sub('<[^>]*>', '', retoots.append(text_content)
notification['status']['content']))
self.seen_toots.add(notification['status']['id'])
# save state # save state
with open('seen_toots.pickle.part', 'xb') as f: with open('seen_toots.pickle.part', 'xb') as f:
@ -78,7 +82,10 @@ class RetootBot(object):
if __name__ == '__main__': if __name__ == '__main__':
# read config in TOML format (https://github.com/toml-lang/toml#toml) # read config in TOML format (https://github.com/toml-lang/toml#toml)
with open('ticketfrei.cfg') as configfile: 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: while True:
bot.retoot() bot.retoot()

View file

@ -165,7 +165,7 @@ class RetweetBot(object):
for status in mentions: for status in mentions:
# Is the Text of the Tweet in the triggerlist? # 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 # Retweet status
mastodon.append(self.retweet(status)) mastodon.append(self.retweet(status))
@ -176,7 +176,7 @@ class RetweetBot(object):
def shutdown(self): def shutdown(self):
""" If something breaks, it shuts down the bot and messages the owner. """ """ 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: with open(self.historypath, "w") as f:
f.write(str(self.last_mention)) f.write(str(self.last_mention))
self.api.PostDirectMessage("Help! I broke down. restart me pls :$", self.user_id, self.screen_name) self.api.PostDirectMessage("Help! I broke down. restart me pls :$", self.user_id, self.screen_name)

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
__encoding__ = "utf-8"
import os import os
import pytoml as toml import pytoml as toml
class Trigger(object): class Trigger(object):
""" """
This class provides a filter to test a string against. This class provides a filter to test a string against.
@ -11,85 +10,63 @@ class Trigger(object):
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
try:
self.goodlistpath = config['trigger']['goodlist_path'] self.goodlistpath = config['trigger']['goodlist_path']
except KeyError:
self.goodlistpath = 'goodlists'
self.goodlist = self.get_lists(self.goodlistpath) self.goodlist = self.get_lists(self.goodlistpath)
self.goodlist = self.strings_ok(self.goodlist)
try:
self.blacklistpath = config['trigger']['blacklist_path'] self.blacklistpath = config['trigger']['blacklist_path']
except KeyError:
self.blacklistpath = 'blacklists'
self.blacklist = self.get_lists(self.blacklistpath) self.blacklist = self.get_lists(self.blacklistpath)
self.blacklist = self.strings_ok(self.blacklist)
def get_lists(self, path): 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 :param path: path to folder whose files shall be added to the set
:return: set of trigger words. :return: set of trigger words.
""" """
trigger_words = set() trigger_words = set()
for filename in os.listdir(path): for filename in os.listdir(path):
with open(path + filename, "r+") as f: with open(os.path.join(path, filename), "r+") as listfile:
[trigger_words.add(s.strip()) for s in f.readlines()] for word in listfile:
word = word.strip()
if word:
trigger_words.add(word)
return trigger_words return trigger_words
def strings_ok(self, filterlist): def is_ok(self, message):
"""
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):
""" """
checks if a string contains no bad words and at least 1 good word. 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 :return: If the string passes the test
""" """
string = unicode.decode(string) ret = False
for triggerword in self.goodlist: for word in message.lower().split():
if string.lower().find(triggerword) != -1: if word in self.goodlist:
for triggerword in self.blacklist: ret = True
if string.lower().find(triggerword) != -1: if word in self.blacklist:
return False
return True
return False 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__": if __name__ == "__main__":
with open("ticketfrei.cfg", "r") as configfile: with open("ticketfrei.cfg", "r") as configfile:
config = toml.load(configfile) config = toml.load(configfile)
print "testing the trigger" print("testing the trigger")
trigger = Trigger(config) trigger = Trigger(config)
print "Printing words which trigger the bot:" print("Printing words which trigger the bot:")
for i in trigger.goodlist: for i in trigger.goodlist:
print i print(i)
print print()
print "Printing words which block a bot:" print("Printing words which block a bot:")
for i in trigger.blacklist: for i in trigger.blacklist:
print i print(i)
print