good- and blacklists are now stored in folders, retweetbot uses configfile

This commit is contained in:
b3yond 2017-06-25 18:06:33 +02:00
parent 647543ddc4
commit 563eaa3971
6 changed files with 66 additions and 44 deletions

View file

@ -44,7 +44,7 @@ Note that atm the good- & blacklist are still outside of ticketfrei.cfg, in sepa
* write the muted people to the db, to easily undo the mutes if necessary. * write the muted people to the db, to easily undo the mutes if necessary.
## to do ## to do
Desktop/pycharm-community-2017.1.4/bin/pycharm.sh
- [x] Twitter: Crawl mentions - [x] Twitter: Crawl mentions
- [x] Mastodon: Crawl mentions - [x] Mastodon: Crawl mentions
- [ ] Write toots/tweets to database/log - [ ] Write toots/tweets to database/log

View file

@ -20,8 +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, def __init__(self, trigger, config,
keypath="appkeys/ticketfrei@twitter.com",
historypath="last_mention", historypath="last_mention",
triggerpath="goodlist", triggerpath="goodlist",
user_id="801098086005243904", user_id="801098086005243904",
@ -29,11 +28,11 @@ class RetweetBot(object):
""" """
Initializes the bot and loads all the necessary data. Initializes the bot and loads all the necessary data.
:param keypath: Path to the file with API keys
:param historypath: Path to the file with ID of the last retweeted Tweet :param historypath: Path to the file with ID of the last retweeted Tweet
:param triggerpath: Path to the file of the triggerwords :param triggerpath: Path to the file of the triggerwords
""" """
keys = self.get_api_keys(keypath) self.config = config
keys = self.get_api_keys()
self.api = twitter.Api(consumer_key=keys[0], self.api = twitter.Api(consumer_key=keys[0],
consumer_secret=keys[1], consumer_secret=keys[1],
access_token_key=keys[2], access_token_key=keys[2],
@ -46,25 +45,26 @@ class RetweetBot(object):
self.triggers = self.get_trigger(self.triggerpath) self.triggers = self.get_trigger(self.triggerpath)
self.trigger = trigger self.trigger = trigger
def get_api_keys(self, path): def get_api_keys(self):
""" """
How to get these keys is described in doc/twitter_api.md How to get these keys is described in doc/twitter_api.md
After you received keys, store them in ../appkeys/appname@service.tld, one at a line: After you received keys, store them in your ticketfrei.cfg like this:
consumer_key [tapp]
consumer_secret consumer_key = "..."
access_token_key consumer_secret = "..."
access_token_secret
[tuser]
access_token_key = "..."
access_token_secret = "..."
:return: keys: list of these 4 strings. :return: keys: list of these 4 strings.
""" """
keys = [] keys = []
try: keys.append(self.config['tapp']['consumer_key'])
with open(path, "r") as f: keys.append(self.config['tapp']['consumer_secret'])
keys = [s.strip() for s in f.readlines()] keys.append(self.config['tuser']['access_token_key'])
except IOError: keys.append(self.config['tuser']['access_token_secret'])
print "[ERROR] You didn't specify Twitter API oAuth keys. Look into the documentation."
exit(-1)
return keys return keys
def get_history(self, path): def get_history(self, path):

View file

@ -7,19 +7,15 @@ email = 'youremail@server.tld'
password = 'yourpassword' password = 'yourpassword'
server = 'yourmastodoninstance' server = 'yourmastodoninstance'
[tapp]
consumer_key = "709823example98n4cnc098rnnsc98nec"
consumer_secret = "09muvs098u08m9examplevsum098mu"
[tuser] [tuser]
consumer_key = "70982398n4cnc098rnnsc98nec" access_token_key = "u098umgfres09ugexa-mplef7n60cwhxm12"
consumer_secret = "09muvs098u08m9vsum098mu" access_token_secret = "8708mj9ßc298m34333example3333tex"
access_token_key = "u098umgfres09ug-f7n60cwhxm12"
access_token_secret = "8708mj9ßc298m343333333tex"
[goodlist] [trigger]
asdf goodlist_path = "goodlist/"
oigna blacklist_path = "blacklist/"
bahn
zu spät
[blacklist]
insult
slur

View file

@ -1,28 +1,38 @@
#!/usr/bin/env python #!/usr/bin/env python
__encoding__ = "utf-8" __encoding__ = "utf-8"
import os
import pytoml as toml
class Trigger(object): 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, config, goodlistpath="goodlist", blacklistpath="blacklist"): def __init__(self, config):
self.config = config self.config = config
self.goodlistpath = goodlistpath
with open(goodlistpath, "r+") as f: self.goodlistpath = config['trigger']['goodlist_path']
self.goodlist = [s.strip() for s in f.readlines()] self.goodlist = self.get_lists(self.goodlistpath)
for word in config["goodlist"]:
self.goodlist.append(word)
self.goodlist = self.strings_ok(self.goodlist) self.goodlist = self.strings_ok(self.goodlist)
self.blacklistpath = blacklistpath self.blacklistpath = config['trigger']['blacklist_path']
with open(blacklistpath, "r+") as f: self.blacklist = self.get_lists(self.blacklistpath)
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) self.blacklist = self.strings_ok(self.blacklist)
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(path + filename, "r+") as f:
[trigger_words.add(s.strip()) for s in f.readlines()]
return trigger_words
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.
@ -50,11 +60,11 @@ class Trigger(object):
return True return True
return False return False
def update_list(self, word, whichlist): def add_to_list(self, word, whichlist):
""" """
:param word: a string of a word which should be appended to one of the lists :param word: a string of a word which should be appended to one of the lists
:param boolean whichlist: 0 : goodlist, 1 : badlist. :param boolean whichlist: 0 : goodlist, 1 : blacklist.
""" """
if whichlist: if whichlist:
path = self.goodlistpath path = self.goodlistpath
@ -66,4 +76,20 @@ class Trigger(object):
f.writelines(old) f.writelines(old)
if __name__ == "__main__": if __name__ == "__main__":
pass with open("ticketfrei.cfg", "r") as configfile:
config = toml.load(configfile)
print "testing the trigger"
trigger = Trigger(config)
print "Printing words which trigger the bot:"
for i in trigger.goodlist:
print i
print
print "Printing words which block a bot:"
for i in trigger.blacklist:
print i
print