trying to introduce toml config to trigger & retweetbot.

This commit is contained in:
b3yond 2017-06-18 01:33:47 +02:00
parent 3f442e0f91
commit da9f0def5a
6 changed files with 39 additions and 16 deletions

View file

View file

@ -1,11 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
__encoding__ = "utf-8"
import twitter import twitter
import requests import requests
import pytoml as toml
import trigger import trigger
from time import sleep from time import sleep
__encoding__ = "utf-8"
class RetweetBot(object): class RetweetBot(object):
""" """
@ -18,7 +20,7 @@ class RetweetBot(object):
last_mention: the ID of the last tweet which mentioned you 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", keypath="appkeys/ticketfrei@twitter.com",
historypath="last_mention", historypath="last_mention",
triggerpath="goodlist", triggerpath="goodlist",
@ -182,9 +184,15 @@ class RetweetBot(object):
if __name__ == "__main__": if __name__ == "__main__":
# create an Api object # 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: while True:
bot.flow() try:
sleep(10) bot.flow()
#except: except:
# bot.shutdown() bot.shutdown()
sleep(1)

View file

@ -22,4 +22,13 @@ consumer_secret = "09muvs098u08m9vsum098mu"
access_token_key = "u098umgfres09ug-f7n60cwhxm12" access_token_key = "u098umgfres09ug-f7n60cwhxm12"
access_token_secret = "8708mj9ßc298m343333333tex" access_token_secret = "8708mj9ßc298m343333333tex"
[goodlist]
asdf
oigna
bahn
zu spät
[blacklist]
insult
slur

View file

@ -11,7 +11,7 @@ if __name__ == '__main__':
with open('ticketfrei.cfg') as configfile: with open('ticketfrei.cfg') as configfile:
config = toml.load(configfile) config = toml.load(configfile)
trigger = Trigger() trigger = Trigger(config)
mbot = RetootBot(config) mbot = RetootBot(config)
tbot = RetweetBot(config, trigger=trigger) tbot = RetweetBot(config, trigger=trigger)

View file

@ -6,21 +6,27 @@ class Trigger(object):
""" """
This class provides a filter to test a string against. 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 self.goodlistpath = goodlistpath
with open(goodlistpath, "r+") as f: with open(goodlistpath, "r+") as f:
self.goodlist = [s.strip() for s in f.readlines()] 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.goodlist = self.strings_ok(self.goodlist)
self.badlistpath = badlistpath self.blacklistpath = blacklistpath
with open(badlistpath, "r+") as f: with open(blacklistpath, "r+") as f:
self.badlist = [s.strip() for s in f.readlines()] self.blacklist = [s.strip() for s in f.readlines()]
self.badlist = self.strings_ok(self.badlist) for word in config["blacklist"]:
self.blacklist.append(word)
self.blacklist = self.strings_ok(self.blacklist)
def strings_ok(self, filterlist): def strings_ok(self, filterlist):
""" """
Checks if an empty line is in a list and removes it. 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. :return: filterlist: a corrected list.
""" """
for word in filterlist: for word in filterlist:
@ -38,7 +44,7 @@ class Trigger(object):
string = unicode.decode(string) string = unicode.decode(string)
for triggerword in self.goodlist: for triggerword in self.goodlist:
if string.lower().find(triggerword) != -1: if string.lower().find(triggerword) != -1:
for triggerword in self.badlist: for triggerword in self.blacklist:
if string.lower().find(triggerword) != -1: if string.lower().find(triggerword) != -1:
return False return False
return True return True
@ -53,7 +59,7 @@ class Trigger(object):
if whichlist: if whichlist:
path = self.goodlistpath path = self.goodlistpath
else: else:
path = self.badlistpath path = self.blacklistpath
with open(path, "w") as f: with open(path, "w") as f:
old = f.readlines() old = f.readlines()
old.append(word) old.append(word)

Binary file not shown.