2017-06-17 19:35:47 +00:00
|
|
|
#!/usr/bin/env python
|
2017-06-25 19:12:26 +00:00
|
|
|
import re
|
2018-03-23 17:00:05 +00:00
|
|
|
from user import User
|
|
|
|
|
2017-06-17 19:35:47 +00:00
|
|
|
|
|
|
|
class Trigger(object):
|
|
|
|
"""
|
|
|
|
This class provides a filter to test a string against.
|
|
|
|
"""
|
2018-03-23 17:00:05 +00:00
|
|
|
def __init__(self, config, uid, db):
|
2017-06-17 23:33:47 +00:00
|
|
|
self.config = config
|
2018-03-23 17:00:05 +00:00
|
|
|
self.db = db
|
|
|
|
self.user = User(db, uid)
|
2017-06-25 19:12:26 +00:00
|
|
|
|
|
|
|
# load goodlists
|
|
|
|
self.goodlist = []
|
2018-03-23 17:00:05 +00:00
|
|
|
raw = self.user.get_trigger_words("trigger_good")
|
|
|
|
print(raw)
|
|
|
|
print(type(raw))
|
|
|
|
for pattern in raw:
|
|
|
|
pattern = pattern.strip()
|
|
|
|
if pattern:
|
|
|
|
self.goodlist.append(re.compile(pattern, re.IGNORECASE))
|
2017-06-17 23:01:11 +00:00
|
|
|
|
2017-06-25 19:12:26 +00:00
|
|
|
# load blacklists
|
|
|
|
self.blacklist = set()
|
2018-03-23 17:00:05 +00:00
|
|
|
raw = self.user.get_trigger_words("trigger_bad")
|
|
|
|
for word in raw:
|
|
|
|
word = word.strip()
|
|
|
|
if word:
|
|
|
|
self.blacklist.add(word)
|
2017-06-25 16:06:33 +00:00
|
|
|
|
2017-06-25 17:15:38 +00:00
|
|
|
def is_ok(self, message):
|
2017-06-17 19:35:47 +00:00
|
|
|
"""
|
|
|
|
checks if a string contains no bad words and at least 1 good word.
|
|
|
|
|
2017-06-25 17:15:38 +00:00
|
|
|
:param message: A given string. Tweet or Toot, cleaned from html.
|
2017-06-17 19:35:47 +00:00
|
|
|
:return: If the string passes the test
|
|
|
|
"""
|
2017-06-25 19:12:26 +00:00
|
|
|
for pattern in self.goodlist:
|
2017-06-25 21:50:58 +00:00
|
|
|
if pattern.search(message) is not None:
|
2017-06-25 19:12:26 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
# no pattern matched
|
|
|
|
return False
|
2017-06-25 17:15:38 +00:00
|
|
|
for word in message.lower().split():
|
|
|
|
if word in self.blacklist:
|
|
|
|
return False
|
2017-06-25 19:12:26 +00:00
|
|
|
return True
|
2017-06-17 19:35:47 +00:00
|
|
|
|
2018-03-23 17:00:05 +00:00
|
|
|
"""
|
2017-06-17 20:32:20 +00:00
|
|
|
if __name__ == "__main__":
|
2018-03-23 17:00:05 +00:00
|
|
|
import prepare
|
|
|
|
config = prepare.get_config()
|
2017-06-25 16:06:33 +00:00
|
|
|
|
2017-06-25 17:15:38 +00:00
|
|
|
print("testing the trigger")
|
2017-06-25 16:06:33 +00:00
|
|
|
trigger = Trigger(config)
|
|
|
|
|
2017-06-25 17:15:38 +00:00
|
|
|
print("Printing words which trigger the bot:")
|
2017-06-25 16:06:33 +00:00
|
|
|
for i in trigger.goodlist:
|
2017-06-25 17:15:38 +00:00
|
|
|
print(i)
|
|
|
|
print()
|
2017-06-25 16:06:33 +00:00
|
|
|
|
2017-06-25 17:15:38 +00:00
|
|
|
print("Printing words which block a bot:")
|
2017-06-25 16:06:33 +00:00
|
|
|
for i in trigger.blacklist:
|
2017-06-25 17:15:38 +00:00
|
|
|
print(i)
|
2018-03-23 17:00:05 +00:00
|
|
|
"""
|