From da9f0def5a4865dccd689bf019e52a6a46f7a12b Mon Sep 17 00:00:00 2001 From: b3yond Date: Sun, 18 Jun 2017 01:33:47 +0200 Subject: [PATCH] trying to introduce toml config to trigger & retweetbot. --- badlist => blacklist | 0 retweetbot.py | 22 +++++++++++++++------- ticketfrei.cfg.example | 9 +++++++++ ticketfrei.py | 2 +- trigger.py | 22 ++++++++++++++-------- trigger.pyc | Bin 2435 -> 2570 bytes 6 files changed, 39 insertions(+), 16 deletions(-) rename badlist => blacklist (100%) diff --git a/badlist b/blacklist similarity index 100% rename from badlist rename to blacklist diff --git a/retweetbot.py b/retweetbot.py index f0e7ddc..0463d62 100644 --- a/retweetbot.py +++ b/retweetbot.py @@ -1,11 +1,13 @@ #!/usr/bin/env python -__encoding__ = "utf-8" import twitter import requests +import pytoml as toml import trigger from time import sleep +__encoding__ = "utf-8" + class RetweetBot(object): """ @@ -18,7 +20,7 @@ class RetweetBot(object): last_mention: the ID of the last tweet which mentioned you """ - def __init__(self, trigger=trigger.Trigger(), + def __init__(self, trigger, keypath="appkeys/ticketfrei@twitter.com", historypath="last_mention", triggerpath="goodlist", @@ -182,9 +184,15 @@ class RetweetBot(object): if __name__ == "__main__": # create an Api object - bot = RetweetBot() + with open('ticketfrei.cfg') as configfile: + config = toml.load(configfile) + + trigger = trigger.Trigger(config) + + bot = RetweetBot(trigger=trigger) while True: - bot.flow() - sleep(10) - #except: - # bot.shutdown() + try: + bot.flow() + except: + bot.shutdown() + sleep(1) diff --git a/ticketfrei.cfg.example b/ticketfrei.cfg.example index 0fa4f05..f77e6b2 100644 --- a/ticketfrei.cfg.example +++ b/ticketfrei.cfg.example @@ -22,4 +22,13 @@ consumer_secret = "09muvs098u08m9vsum098mu" access_token_key = "u098umgfres09ug-f7n60cwhxm12" access_token_secret = "8708mj9ßc298m343333333tex" +[goodlist] +asdf +oigna +bahn +zu spät + +[blacklist] +insult +slur diff --git a/ticketfrei.py b/ticketfrei.py index cbdab0a..e6109c8 100644 --- a/ticketfrei.py +++ b/ticketfrei.py @@ -11,7 +11,7 @@ if __name__ == '__main__': with open('ticketfrei.cfg') as configfile: config = toml.load(configfile) - trigger = Trigger() + trigger = Trigger(config) mbot = RetootBot(config) tbot = RetweetBot(config, trigger=trigger) diff --git a/trigger.py b/trigger.py index 0109ca5..3a4fe2b 100644 --- a/trigger.py +++ b/trigger.py @@ -6,21 +6,27 @@ class Trigger(object): """ This class provides a filter to test a string against. """ - def __init__(self, goodlistpath="goodlist", badlistpath="badlist"): + def __init__(self, config, goodlistpath="goodlist", blacklistpath="blacklist"): + self.config = config + self.goodlistpath = goodlistpath with open(goodlistpath, "r+") as f: self.goodlist = [s.strip() for s in f.readlines()] + for word in config["goodlist"]: + self.goodlist.append(word) self.goodlist = self.strings_ok(self.goodlist) - self.badlistpath = badlistpath - with open(badlistpath, "r+") as f: - self.badlist = [s.strip() for s in f.readlines()] - self.badlist = self.strings_ok(self.badlist) + self.blacklistpath = blacklistpath + with open(blacklistpath, "r+") as f: + self.blacklist = [s.strip() for s in f.readlines()] + for word in config["blacklist"]: + self.blacklist.append(word) + self.blacklist = self.strings_ok(self.blacklist) def strings_ok(self, filterlist): """ Checks if an empty line is in a list and removes it. - :param filterlist: a good- or badlist. + :param filterlist: a good- or blacklist. :return: filterlist: a corrected list. """ for word in filterlist: @@ -38,7 +44,7 @@ class Trigger(object): string = unicode.decode(string) for triggerword in self.goodlist: if string.lower().find(triggerword) != -1: - for triggerword in self.badlist: + for triggerword in self.blacklist: if string.lower().find(triggerword) != -1: return False return True @@ -53,7 +59,7 @@ class Trigger(object): if whichlist: path = self.goodlistpath else: - path = self.badlistpath + path = self.blacklistpath with open(path, "w") as f: old = f.readlines() old.append(word) diff --git a/trigger.pyc b/trigger.pyc index 301bc27d6ce7b69dd0e1532c32e9a8691978e428..192aab0e0ff1c82a2c298886ed04655014a8b382 100644 GIT binary patch delta 657 zcmb7CyKYlK5IwVdUz-OICq_6D!a_od6e*A*5owYFH#CvF6oJB6#@BZ2SdQbnMG9Lf z+@+z|nwCCFgeds|S{fR93JMB3#LNW+Q&P<6oSny>o!RN~`AVd}FV1bhzlah#GXP%o z8_lDBH*?K^Sk12^bT`K%Ifg0l_g)NC{Jg*AFoX(BWu+p2ifY)C!n{7>DQu z!e1(!Z040A4pN}{{PeywO%+OKmD+7gB8m1mpFxBvflg{TkhAztgwI3+chxyaat->w zG+gDC<{u69RHHPb!OUv-GONLeF417XZ+NyQquS3D`_a1PDZvU}n`7r9cFp5KyW8=k z9={OU7!F&5)JZsVqwb)S)sK(dBGc>X4;$|AcN1BGlMAv|zil-GE6nE2<*v39pZTwP z&%aZJyQ|DVMj0y51})JF75`%YSykw*FYCiV7VXdVesP}jb2061-?c`ve0#%hn4QSV zk668Y6nrF__dmlHJ-AGxUAl9vz=oV*g#kOkv(P3=^e)MrA&I#qHc^p)g%%e3 z3R!%5zuB4j=I&L|&j}BZ zxFoA2$rGtALKk2cKoLL)5PA%*=HZw_mAo8c20egcDGSE}x&mTu>>UP&p3M}n6##5( z6H7c`eqsT=FH!-gU1ozEOV9pAe{M5Vf9O*`^eLdin;xSn))S-KGoG1E4Uwugn>4lV zu-UaVnTcvgjcT&nJWzvVey^leRyE&bdS)+5n82`J@y(`uONE3gv>I~gGqI?bc{Z0J z3s7Pf=WvACSI4}F*ZP*%SUJ$+Vm&7*Mw!Z$sIJ1cIe`9P1;;lR$@d?{GosX&PKcBJ zH)jPGerx)s4c|885GuD?VI#0qN9{<3^{5dhp7Honm)t2#>l1ea=k<$Q%$Uh-1%K3_ a34JG5^q$P&pzg>eJuM6RRgUqcA^Zdup=vGw