forked from ticketfrei/ticketfrei
change goodlist to regex patterns
This commit is contained in:
parent
ca07f70aa6
commit
93fbedadd5
|
@ -1,6 +1,4 @@
|
|||
kontrolle
|
||||
fahrscheinkontrolle
|
||||
kontrolleur
|
||||
kontroll?e
|
||||
konti
|
||||
db
|
||||
vgn
|
||||
|
@ -15,12 +13,5 @@ tram
|
|||
linie
|
||||
nuernberg
|
||||
nürnberg
|
||||
s1
|
||||
s2
|
||||
s3
|
||||
s4
|
||||
u1
|
||||
u2
|
||||
u3
|
||||
u21
|
||||
u11
|
||||
s\d
|
||||
u\d\d?
|
||||
|
|
|
@ -18,5 +18,9 @@ access_token_key = "youraccesstokenkey"
|
|||
access_token_secret = "youraccesstokensecret"
|
||||
|
||||
# [trigger]
|
||||
# goodlists are one regex per line.
|
||||
# badlists are one badword per line.
|
||||
# a message musst match at least one regex in goodlist and contain none of the badwords.
|
||||
|
||||
# goodlist_path = 'goodlists'
|
||||
# blacklist_path = 'blacklists'
|
||||
|
|
52
trigger.py
52
trigger.py
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
import pytoml as toml
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
class Trigger(object):
|
||||
|
@ -11,33 +12,32 @@ class Trigger(object):
|
|||
self.config = config
|
||||
|
||||
try:
|
||||
self.goodlistpath = config['trigger']['goodlist_path']
|
||||
goodlistpath = config['trigger']['goodlist_path']
|
||||
except KeyError:
|
||||
self.goodlistpath = 'goodlists'
|
||||
self.goodlist = self.get_lists(self.goodlistpath)
|
||||
goodlistpath = 'goodlists'
|
||||
|
||||
# load goodlists
|
||||
self.goodlist = []
|
||||
for filename in os.listdir(goodlistpath):
|
||||
with open(os.path.join(goodlistpath, filename), "r+") as listfile:
|
||||
for pattern in listfile:
|
||||
pattern = pattern.strip()
|
||||
if pattern:
|
||||
self.goodlist.append(re.compile(pattern))
|
||||
|
||||
try:
|
||||
self.blacklistpath = config['trigger']['blacklist_path']
|
||||
blacklistpath = config['trigger']['blacklist_path']
|
||||
except KeyError:
|
||||
self.blacklistpath = 'blacklists'
|
||||
self.blacklist = self.get_lists(self.blacklistpath)
|
||||
blacklistpath = 'blacklists'
|
||||
|
||||
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(os.path.join(path, filename), "r+") as listfile:
|
||||
# load blacklists
|
||||
self.blacklist = set()
|
||||
for filename in os.listdir(blacklistpath):
|
||||
with open(os.path.join(blacklistpath, filename), "r+") as listfile:
|
||||
for word in listfile:
|
||||
word = word.strip()
|
||||
if word:
|
||||
trigger_words.add(word)
|
||||
return trigger_words
|
||||
self.blacklist.add(word)
|
||||
|
||||
def is_ok(self, message):
|
||||
"""
|
||||
|
@ -46,13 +46,17 @@ class Trigger(object):
|
|||
:param message: A given string. Tweet or Toot, cleaned from html.
|
||||
:return: If the string passes the test
|
||||
"""
|
||||
ret = False
|
||||
for pattern in self.goodlist:
|
||||
if pattern.match(message):
|
||||
break
|
||||
else:
|
||||
# no pattern matched
|
||||
return False
|
||||
|
||||
for word in message.lower().split():
|
||||
if word in self.goodlist:
|
||||
ret = True
|
||||
if word in self.blacklist:
|
||||
return False
|
||||
return ret
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in a new issue