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
__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)

View file

@ -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

View file

@ -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)

View file

@ -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)

Binary file not shown.