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 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()

View file

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

View file

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