wrote trigger class to filter the tweets/toots before retweeting/boosting
This commit is contained in:
parent
e631289aff
commit
10b784617e
10
badlist
Normal file
10
badlist
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
bastard
|
||||||
|
whore
|
||||||
|
slut
|
||||||
|
hure
|
||||||
|
jude
|
||||||
|
schwuchtel
|
||||||
|
fag
|
||||||
|
faggot
|
||||||
|
arbeitsplätze
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import twitter
|
import twitter
|
||||||
import requests
|
import requests
|
||||||
|
import trigger
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,9 +17,10 @@ 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, keypath="appkeys/ticketfrei@twitter.com",
|
def __init__(self, trigger=trigger.Trigger(),
|
||||||
|
keypath="appkeys/ticketfrei@twitter.com",
|
||||||
historypath="last_mention",
|
historypath="last_mention",
|
||||||
triggerpath="triggerwords",
|
triggerpath="goodlist",
|
||||||
user_id="801098086005243904",
|
user_id="801098086005243904",
|
||||||
screen_name="links_tech"):
|
screen_name="links_tech"):
|
||||||
"""
|
"""
|
||||||
|
@ -39,6 +41,7 @@ class Retweetbot(object):
|
||||||
self.screen_name = screen_name
|
self.screen_name = screen_name
|
||||||
self.last_mention = bot.get_history(self.historypath)
|
self.last_mention = bot.get_history(self.historypath)
|
||||||
self.triggers = bot.get_trigger(self.triggerpath)
|
self.triggers = bot.get_trigger(self.triggerpath)
|
||||||
|
self.trigger = trigger
|
||||||
|
|
||||||
def get_api_keys(self, path):
|
def get_api_keys(self, path):
|
||||||
"""
|
"""
|
||||||
|
@ -52,8 +55,13 @@ class Retweetbot(object):
|
||||||
|
|
||||||
:return: keys: list of these 4 strings.
|
:return: keys: list of these 4 strings.
|
||||||
"""
|
"""
|
||||||
|
keys = []
|
||||||
|
try:
|
||||||
with open(path, "r") as f:
|
with open(path, "r") as f:
|
||||||
keys = [s.strip() for s in f.readlines()]
|
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)
|
||||||
return keys
|
return keys
|
||||||
|
|
||||||
def get_history(self, path):
|
def get_history(self, path):
|
||||||
|
@ -72,7 +80,7 @@ class Retweetbot(object):
|
||||||
triggers = [s.strip() for s in f.readlines()]
|
triggers = [s.strip() for s in f.readlines()]
|
||||||
return triggers
|
return triggers
|
||||||
|
|
||||||
def bridge_mastodon(self, status):
|
def format_mastodon(self, status):
|
||||||
"""
|
"""
|
||||||
Bridge your Retweets to mastodon.
|
Bridge your Retweets to mastodon.
|
||||||
:todo vmann: add all the mastodon API magic.
|
:todo vmann: add all the mastodon API magic.
|
||||||
|
@ -89,42 +97,25 @@ class Retweetbot(object):
|
||||||
|
|
||||||
:return: list of Status objects
|
:return: list of Status objects
|
||||||
"""
|
"""
|
||||||
done = False
|
while 1:
|
||||||
mentions = []
|
|
||||||
while not done:
|
|
||||||
try:
|
try:
|
||||||
mentions = self.api.GetMentions(since_id=self.last_mention)
|
mentions = self.api.GetMentions(since_id=self.last_mention)
|
||||||
done = True
|
return mentions
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
print("[ERROR] Bad Connection.")
|
print("[ERROR] Bad Connection.")
|
||||||
sleep(10)
|
sleep(10)
|
||||||
return mentions
|
|
||||||
|
|
||||||
def trigger_rt(self, status):
|
|
||||||
"""
|
|
||||||
Checks if the text of a tweet matches the relevant trigger words.
|
|
||||||
|
|
||||||
:param status: A given tweet
|
|
||||||
:return: if it should be retweeted
|
|
||||||
"""
|
|
||||||
for triggerword in self.triggers:
|
|
||||||
if status.text.lower().find(triggerword):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def retweet(self, status):
|
def retweet(self, status):
|
||||||
"""
|
"""
|
||||||
Retweets a given tweet.
|
Retweets a given tweet.
|
||||||
|
|
||||||
:param status: A tweet object.
|
:param status: A tweet object.
|
||||||
|
:return: toot: string of the tweet, to toot on mastodon.
|
||||||
"""
|
"""
|
||||||
done = False
|
while 1:
|
||||||
while not done:
|
|
||||||
try:
|
try:
|
||||||
self.api.PostRetweet(status.id)
|
self.api.PostRetweet(status.id)
|
||||||
self.bridge_mastodon(status)
|
return self.format_mastodon(status)
|
||||||
done = True
|
|
||||||
|
|
||||||
# Hopefully we got rid of this error. If not, try to uncomment these lines.
|
# Hopefully we got rid of this error. If not, try to uncomment these lines.
|
||||||
# except twitter.error.TwitterError:
|
# except twitter.error.TwitterError:
|
||||||
# print("[ERROR] probably you already retweeted this tweet.")
|
# print("[ERROR] probably you already retweeted this tweet.")
|
||||||
|
@ -133,23 +124,47 @@ class Retweetbot(object):
|
||||||
print("[ERROR] Bad Connection.")
|
print("[ERROR] Bad Connection.")
|
||||||
sleep(10)
|
sleep(10)
|
||||||
|
|
||||||
def flow(self):
|
def tweet(self, post):
|
||||||
""" The flow of crawling mentions and retweeting them."""
|
"""
|
||||||
|
Tweet a post.
|
||||||
|
|
||||||
|
:param post: String with the text to tweet.
|
||||||
|
"""
|
||||||
|
while 1:
|
||||||
|
try:
|
||||||
|
self.api.PostUpdate(status=post)
|
||||||
|
return
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
print("[ERROR] Bad Connection.")
|
||||||
|
sleep(10)
|
||||||
|
|
||||||
|
def flow(self, to_tweet=()):
|
||||||
|
""" The flow of crawling mentions and retweeting them.
|
||||||
|
|
||||||
|
:param to_tweet: list of strings to tweet
|
||||||
|
:return list of retweeted tweets, to toot on mastodon
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Tweet the toots the Retootbot gives to us
|
||||||
|
for post in to_tweet:
|
||||||
|
self.tweet(post)
|
||||||
|
|
||||||
# Store all mentions in a list of Status Objects
|
# Store all mentions in a list of Status Objects
|
||||||
mentions = self.crawl_mentions()
|
mentions = self.crawl_mentions()
|
||||||
|
mastodon = []
|
||||||
|
|
||||||
for status in mentions:
|
for status in mentions:
|
||||||
# Is the Text of the Tweet in the triggerlist?
|
# Is the Text of the Tweet in the triggerlist?
|
||||||
should_retweet = self.trigger_rt(status)
|
should_retweet = self.trigger.check_string(status.text)
|
||||||
|
|
||||||
# Retweet status
|
# Retweet status
|
||||||
if should_retweet:
|
if should_retweet:
|
||||||
self.retweet(status)
|
mastodon.append(self.retweet(status))
|
||||||
|
|
||||||
# save the id so it doesn't get crawled again
|
# save the id so it doesn't get crawled again
|
||||||
self.last_mention = status.id
|
self.last_mention = status.id
|
||||||
print self.last_mention
|
print self.last_mention
|
||||||
|
return mastodon
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
""" If something breaks, it shuts down the bot and messages the owner. """
|
""" If something breaks, it shuts down the bot and messages the owner. """
|
||||||
|
|
48
trigger.py
Normal file
48
trigger.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
class Trigger(object):
|
||||||
|
"""
|
||||||
|
This class provides a filter to test a string against.
|
||||||
|
"""
|
||||||
|
def __init__(self, goodlistpath="goodlist", badlistpath="badlist"):
|
||||||
|
self.goodlistpath = goodlistpath
|
||||||
|
with open(goodlistpath, "r+") as f:
|
||||||
|
self.goodlist = [s.strip() for s in f.readlines()]
|
||||||
|
|
||||||
|
self.badlistpath = badlistpath
|
||||||
|
with open(badlistpath, "r+") as f:
|
||||||
|
self.badlist = [s.strip() for s in f.readlines()]
|
||||||
|
|
||||||
|
def check_string(self, string):
|
||||||
|
"""
|
||||||
|
checks if a string contains no bad words and at least 1 good word.
|
||||||
|
|
||||||
|
:param string: A given string. Tweet or Toot, cleaned from html.
|
||||||
|
:return: If the string passes the test
|
||||||
|
"""
|
||||||
|
for triggerword in self.goodlist:
|
||||||
|
if string.lower().find(triggerword):
|
||||||
|
for triggerword in self.badlist:
|
||||||
|
if string.lower().find(triggerword):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def update_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.
|
||||||
|
"""
|
||||||
|
if whichlist:
|
||||||
|
path = self.goodlistpath
|
||||||
|
else:
|
||||||
|
path = self.badlistpath
|
||||||
|
with open(path, "w") as f:
|
||||||
|
old = f.readlines()
|
||||||
|
old.append(word)
|
||||||
|
f.writelines(old)
|
||||||
|
|
||||||
|
if __name__ == "main":
|
||||||
|
pass
|
|
@ -1,24 +0,0 @@
|
||||||
kontrolle
|
|
||||||
ticketfrei
|
|
||||||
konti
|
|
||||||
db
|
|
||||||
zivil
|
|
||||||
sicherheit
|
|
||||||
uniform
|
|
||||||
station
|
|
||||||
bus
|
|
||||||
bahn
|
|
||||||
tram
|
|
||||||
linie
|
|
||||||
nuernberg
|
|
||||||
nbg
|
|
||||||
nürnberg
|
|
||||||
s1
|
|
||||||
s2
|
|
||||||
s3
|
|
||||||
u1
|
|
||||||
u2
|
|
||||||
u3
|
|
||||||
s4
|
|
||||||
u21
|
|
||||||
u11
|
|
Loading…
Reference in a new issue