From 8a90f70eb3d70235cd1c968374ac5b870bf56322 Mon Sep 17 00:00:00 2001 From: b3yond Date: Fri, 23 Mar 2018 18:00:05 +0100 Subject: [PATCH] changed trigger to work with db --- backend.py | 4 +--- trigger.py | 48 +++++++++++++++++++++--------------------------- user.py | 4 ++++ 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/backend.py b/backend.py index 917bb39..0578bc9 100755 --- a/backend.py +++ b/backend.py @@ -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) diff --git a/trigger.py b/trigger.py index db57f81..0b91247 100644 --- a/trigger.py +++ b/trigger.py @@ -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) +""" \ No newline at end of file diff --git a/user.py b/user.py index 84fcc53..3b2e82f 100644 --- a/user.py +++ b/user.py @@ -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')