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.
## to do
Desktop/pycharm-community-2017.1.4/bin/pycharm.sh
- [x] Twitter: Crawl mentions
- [x] Mastodon: Crawl mentions
- [ ] 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
"""
def __init__(self, trigger,
keypath="appkeys/ticketfrei@twitter.com",
def __init__(self, trigger, config,
historypath="last_mention",
triggerpath="goodlist",
user_id="801098086005243904",
@ -29,11 +28,11 @@ class RetweetBot(object):
"""
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 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],
consumer_secret=keys[1],
access_token_key=keys[2],
@ -46,25 +45,26 @@ class RetweetBot(object):
self.triggers = self.get_trigger(self.triggerpath)
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
After you received keys, store them in ../appkeys/appname@service.tld, one at a line:
consumer_key
consumer_secret
access_token_key
access_token_secret
After you received keys, store them in your ticketfrei.cfg like this:
[tapp]
consumer_key = "..."
consumer_secret = "..."
[tuser]
access_token_key = "..."
access_token_secret = "..."
:return: keys: list of these 4 strings.
"""
keys = []
try:
with open(path, "r") as f:
keys = [s.strip() for s in f.readlines()]
except IOError:
print "[ERROR] You didn't specify Twitter API oAuth keys. Look into the documentation."
exit(-1)
keys.append(self.config['tapp']['consumer_key'])
keys.append(self.config['tapp']['consumer_secret'])
keys.append(self.config['tuser']['access_token_key'])
keys.append(self.config['tuser']['access_token_secret'])
return keys
def get_history(self, path):

View file

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

View file

@ -1,28 +1,38 @@
#!/usr/bin/env python
__encoding__ = "utf-8"
import os
import pytoml as toml
class Trigger(object):
"""
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.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.goodlistpath = config['trigger']['goodlist_path']
self.goodlist = self.get_lists(self.goodlistpath)
self.goodlist = self.strings_ok(self.goodlist)
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.blacklistpath = config['trigger']['blacklist_path']
self.blacklist = self.get_lists(self.blacklistpath)
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):
"""
Checks if an empty line is in a list and removes it.
@ -50,11 +60,11 @@ class Trigger(object):
return True
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 boolean whichlist: 0 : goodlist, 1 : badlist.
:param boolean whichlist: 0 : goodlist, 1 : blacklist.
"""
if whichlist:
path = self.goodlistpath
@ -66,4 +76,20 @@ class Trigger(object):
f.writelines(old)
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