2017-06-17 19:35:47 +00:00
|
|
|
#!/usr/bin/env python
|
2017-06-17 22:35:34 +00:00
|
|
|
__encoding__ = "utf-8"
|
2017-06-17 19:35:47 +00:00
|
|
|
|
2017-06-25 16:06:33 +00:00
|
|
|
import os
|
|
|
|
import pytoml as toml
|
2017-06-17 19:35:47 +00:00
|
|
|
|
|
|
|
class Trigger(object):
|
|
|
|
"""
|
|
|
|
This class provides a filter to test a string against.
|
|
|
|
"""
|
2017-06-25 16:06:33 +00:00
|
|
|
def __init__(self, config):
|
2017-06-17 23:33:47 +00:00
|
|
|
self.config = config
|
|
|
|
|
2017-06-25 16:06:33 +00:00
|
|
|
|
|
|
|
self.goodlistpath = config['trigger']['goodlist_path']
|
|
|
|
self.goodlist = self.get_lists(self.goodlistpath)
|
2017-06-17 23:01:11 +00:00
|
|
|
self.goodlist = self.strings_ok(self.goodlist)
|
2017-06-17 19:35:47 +00:00
|
|
|
|
2017-06-25 16:06:33 +00:00
|
|
|
self.blacklistpath = config['trigger']['blacklist_path']
|
|
|
|
self.blacklist = self.get_lists(self.blacklistpath)
|
2017-06-17 23:33:47 +00:00
|
|
|
self.blacklist = self.strings_ok(self.blacklist)
|
2017-06-17 23:01:11 +00:00
|
|
|
|
2017-06-25 16:06:33 +00:00
|
|
|
def get_lists(self, path):
|
|
|
|
"""
|
|
|
|
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()]
|
|
|
|
return trigger_words
|
|
|
|
|
2017-06-17 23:01:11 +00:00
|
|
|
def strings_ok(self, filterlist):
|
|
|
|
"""
|
|
|
|
Checks if an empty line is in a list and removes it.
|
2017-06-17 23:33:47 +00:00
|
|
|
:param filterlist: a good- or blacklist.
|
2017-06-17 23:01:11 +00:00
|
|
|
:return: filterlist: a corrected list.
|
|
|
|
"""
|
|
|
|
for word in filterlist:
|
|
|
|
if word == "\n":
|
|
|
|
del word
|
|
|
|
return filterlist
|
2017-06-17 19:35:47 +00:00
|
|
|
|
|
|
|
def check_string(self, string):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
:return: If the string passes the test
|
|
|
|
"""
|
2017-06-17 22:35:34 +00:00
|
|
|
string = unicode.decode(string)
|
2017-06-17 19:35:47 +00:00
|
|
|
for triggerword in self.goodlist:
|
2017-06-17 22:35:34 +00:00
|
|
|
if string.lower().find(triggerword) != -1:
|
2017-06-17 23:33:47 +00:00
|
|
|
for triggerword in self.blacklist:
|
2017-06-17 22:35:34 +00:00
|
|
|
if string.lower().find(triggerword) != -1:
|
2017-06-17 19:35:47 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2017-06-25 16:06:33 +00:00
|
|
|
def add_to_list(self, word, whichlist):
|
2017-06-17 19:35:47 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
:param word: a string of a word which should be appended to one of the lists
|
2017-06-25 16:06:33 +00:00
|
|
|
:param boolean whichlist: 0 : goodlist, 1 : blacklist.
|
2017-06-17 19:35:47 +00:00
|
|
|
"""
|
|
|
|
if whichlist:
|
|
|
|
path = self.goodlistpath
|
|
|
|
else:
|
2017-06-17 23:33:47 +00:00
|
|
|
path = self.blacklistpath
|
2017-06-17 19:35:47 +00:00
|
|
|
with open(path, "w") as f:
|
|
|
|
old = f.readlines()
|
|
|
|
old.append(word)
|
|
|
|
f.writelines(old)
|
|
|
|
|
2017-06-17 20:32:20 +00:00
|
|
|
if __name__ == "__main__":
|
2017-06-25 16:06:33 +00:00
|
|
|
with open("ticketfrei.cfg", "r") as configfile:
|
|
|
|
config = toml.load(configfile)
|
|
|
|
|
|
|
|
print "testing the trigger"
|
|
|
|
trigger = Trigger(config)
|
|
|
|
|
|
|
|
print "Printing words which trigger the bot:"
|
|
|
|
for i in trigger.goodlist:
|
|
|
|
print i
|
|
|
|
print
|
|
|
|
|
|
|
|
print "Printing words which block a bot:"
|
|
|
|
for i in trigger.blacklist:
|
|
|
|
print i
|
|
|
|
print
|
|
|
|
|
|
|
|
|