From e171b8a5369b54b0e0cd2ed30929daf9d0687e00 Mon Sep 17 00:00:00 2001 From: Thomas L Date: Sun, 25 Jun 2017 20:22:11 +0200 Subject: [PATCH 1/4] add some words to goodlist --- goodlists/nbg_goodlist | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/goodlists/nbg_goodlist b/goodlists/nbg_goodlist index 128fc08..5a368cc 100644 --- a/goodlists/nbg_goodlist +++ b/goodlists/nbg_goodlist @@ -1,6 +1,10 @@ kontrolle +fahrscheinkontrolle +kontrolleur konti db +vgn +vag zivil sicherheit uniform @@ -14,9 +18,9 @@ nürnberg s1 s2 s3 +s4 u1 u2 u3 -s4 u21 u11 From ca07f70aa67373ca5073e0c105e75f6f78d1d095 Mon Sep 17 00:00:00 2001 From: Thomas L Date: Sun, 25 Jun 2017 20:23:46 +0200 Subject: [PATCH 2/4] fix example config file --- ticketfrei.cfg.example | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ticketfrei.cfg.example b/ticketfrei.cfg.example index e95e468..5f60104 100644 --- a/ticketfrei.cfg.example +++ b/ticketfrei.cfg.example @@ -10,13 +10,13 @@ server = 'yourmastodoninstance' consumer_key = "yourconsumerkey" consumer_secret = yourconsumersecret" +# shutdown_contact_userid = 012345 +# shutdown_contact_screen_name = 'yourscreenname' + [tuser] access_token_key = "youraccesstokenkey" access_token_secret = "youraccesstokensecret" -# shutdown_contact_userid = 012345 -# shutdown_contact_screen_name = 'yourscreenname' - # [trigger] # goodlist_path = 'goodlists' # blacklist_path = 'blacklists' From 93fbedadd51bfd3dd093cd80b3e4abedb0637534 Mon Sep 17 00:00:00 2001 From: Thomas L Date: Sun, 25 Jun 2017 21:12:26 +0200 Subject: [PATCH 3/4] change goodlist to regex patterns --- goodlists/nbg_goodlist | 15 +++--------- ticketfrei.cfg.example | 4 ++++ trigger.py | 52 +++++++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/goodlists/nbg_goodlist b/goodlists/nbg_goodlist index 5a368cc..08c885f 100644 --- a/goodlists/nbg_goodlist +++ b/goodlists/nbg_goodlist @@ -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? diff --git a/ticketfrei.cfg.example b/ticketfrei.cfg.example index 5f60104..4c0ec36 100644 --- a/ticketfrei.cfg.example +++ b/ticketfrei.cfg.example @@ -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' diff --git a/trigger.py b/trigger.py index c5715c7..a88993e 100644 --- a/trigger.py +++ b/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__": From b46247c9ad0e859acebba7d5dd73df920e00d9d5 Mon Sep 17 00:00:00 2001 From: Thomas L Date: Sun, 25 Jun 2017 21:42:06 +0200 Subject: [PATCH 4/4] fix exception handling --- retweetbot.py | 13 ++++++------- ticketfrei.py | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/retweetbot.py b/retweetbot.py index 94e608d..a0b17f8 100644 --- a/retweetbot.py +++ b/retweetbot.py @@ -191,10 +191,9 @@ if __name__ == "__main__": trigger = trigger.Trigger(config) bot = RetweetBot(trigger, config) - while True: - bot.flow() - try: - pass - except: - bot.shutdown() - sleep(1) + try: + while True: + bot.flow() + sleep(1) + except: + bot.shutdown() diff --git a/ticketfrei.py b/ticketfrei.py index 452eec4..bc8a49a 100644 --- a/ticketfrei.py +++ b/ticketfrei.py @@ -20,7 +20,7 @@ if __name__ == '__main__': statuses = [] while True: statuses = mbot.retoot(statuses) - statuses = tbot.flow(statuses) # XXX not implemented in RetweetBot + statuses = tbot.flow(statuses) time.sleep(1) except: tbot.shutdown()