changed trigger to work with db

This commit is contained in:
b3yond 2018-03-23 18:00:05 +01:00
parent 5f55eb88ff
commit aa5669b019
3 changed files with 26 additions and 30 deletions

View file

@ -22,6 +22,7 @@ def get_users(db):
def init_bots(config, logger, db, users):
for uid in users:
users[uid].append(Trigger(config, uid, db))
users[uid].append(RetootBot(config, logger, uid, db))
users[uid].append(RetweetBot(config, logger, uid, db))
users[uid].append(Mailbot(config, logger, uid, db))
@ -33,9 +34,6 @@ def run():
logger = prepare.get_logger(config)
db = DB()
# set trigger
trigger = Trigger(config)
while True:
# get a dictionary { uid : [ Bot objects ] }
users = get_users(db)

View file

@ -1,41 +1,34 @@
#!/usr/bin/env python
import os
import re
from user import User
class Trigger(object):
"""
This class provides a filter to test a string against.
"""
def __init__(self, config):
def __init__(self, config, uid, db):
self.config = config
try:
goodlistpath = config['trigger']['goodlist_path']
except KeyError:
goodlistpath = 'goodlists'
self.db = db
self.user = User(db, uid)
# 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, re.IGNORECASE))
try:
blacklistpath = config['trigger']['blacklist_path']
except KeyError:
blacklistpath = 'blacklists'
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))
# 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:
self.blacklist.add(word)
raw = self.user.get_trigger_words("trigger_bad")
for word in raw:
word = word.strip()
if word:
self.blacklist.add(word)
def is_ok(self, message):
"""
@ -55,10 +48,10 @@ class Trigger(object):
return False
return True
"""
if __name__ == "__main__":
import backend
config = backend.get_config()
import prepare
config = prepare.get_config()
print("testing the trigger")
trigger = Trigger(config)
@ -71,3 +64,4 @@ if __name__ == "__main__":
print("Printing words which block a bot:")
for i in trigger.blacklist:
print(i)
"""

View file

@ -47,6 +47,10 @@ class User(object):
self.db.cur.execute("UPDATE seen_mail SET mail_date = ? WHERE user_id = ?;",
(mail_date, self.uid))
def get_trigger_words(self, table):
self.db.cur.execute("SELECT words FROM ? WHERE user_id = ?;", (table, self.uid,))
return self.db.cur.fetchone()[0]
def state(self):
return dict(foo='bar')