merged config example changes

This commit is contained in:
b3yond 2017-06-25 19:52:29 +02:00
commit 2099ad8156
2 changed files with 33 additions and 20 deletions

View file

@ -6,8 +6,6 @@ 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):
""" """
@ -15,19 +13,20 @@ class RetweetBot(object):
1) mention him, 1) mention him,
2) contain at least one of the triggerwords provided. 2) contain at least one of the triggerwords provided.
api: The api object, generated with your oAuth keys, responsible for communication with twitter rest API api: The api object, generated with your oAuth keys, responsible for
triggers: a list of words, one of them has to be in a tweet for it to be retweeted communication with twitter rest API
triggers: a list of words, one of them has to be in a tweet for it to be
retweeted
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, config, def __init__(self, trigger, config,
historypath="last_mention", historypath="last_mention"):
user_id="801098086005243904",
screen_name="links_tech"):
""" """
Initializes the bot and loads all the necessary data. Initializes the bot and loads all the necessary data.
: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
""" """
self.config = config self.config = config
keys = self.get_api_keys() keys = self.get_api_keys()
@ -36,8 +35,12 @@ class RetweetBot(object):
access_token_key=keys[2], access_token_key=keys[2],
access_token_secret=keys[3]) access_token_secret=keys[3])
self.historypath = historypath self.historypath = historypath
self.user_id = user_id try:
self.screen_name = screen_name self.user_id = self.config['tapp']['shutdown_contact_userid']
self.screen_name = \
self.config['tapp']['shutdown_contact_screen_name']
except KeyError:
self.no_shutdown_contact = True
self.last_mention = self.get_history(self.historypath) self.last_mention = self.get_history(self.historypath)
self.trigger = trigger self.trigger = trigger
@ -64,9 +67,11 @@ class RetweetBot(object):
return keys return keys
def get_history(self, path): def get_history(self, path):
""" This counter is needed to keep track of your mentions, so you don't double RT them """ This counter is needed to keep track of your mentions, so you
don't double RT them
:param path: string: contains path to the file where the ID of the last_mention is stored. :param path: string: contains path to the file where the ID of the
last_mention is stored.
:return: last_mention: ID of the last tweet which mentioned the bot :return: last_mention: ID of the last tweet which mentioned the bot
""" """
try: try:
@ -83,7 +88,8 @@ class RetweetBot(object):
:todo vmann: add all the mastodon API magic. :todo vmann: add all the mastodon API magic.
:param status: Object of a tweet. :param status: Object of a tweet.
:return: toot: text tooted on mastodon, e.g. "_b3yond: There are uniformed controllers in the U2 at Opernhaus." :return: toot: text tooted on mastodon, e.g. "_b3yond: There are
uniformed controllers in the U2 at Opernhaus."
""" """
toot = status.user.name + ": " + status.text toot = status.user.name + ": " + status.text
return toot return toot
@ -99,7 +105,7 @@ class RetweetBot(object):
mentions = self.api.GetMentions(since_id=self.last_mention) mentions = self.api.GetMentions(since_id=self.last_mention)
return mentions return mentions
except twitter.TwitterError: except twitter.TwitterError:
print("[ERROR] Rate Limit exceeded, trying again once a minute") print("[ERROR] Rate Limit exceeded, trying again in a minute")
sleep(60) sleep(60)
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
print("[ERROR] Bad Connection.") print("[ERROR] Bad Connection.")
@ -116,7 +122,8 @@ class RetweetBot(object):
try: try:
self.api.PostRetweet(status.id) self.api.PostRetweet(status.id)
return self.format_mastodon(status) return self.format_mastodon(status)
# maybe one day we get rid of this error. If not, try to uncomment these lines. # maybe one day we get 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.")
return None return None
@ -165,11 +172,15 @@ class RetweetBot(object):
return mastodon 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.
"""
print("[ERROR] Shit went wrong, closing down.") print("[ERROR] Shit went wrong, closing down.")
if self.no_shutdown_contact:
return
with open(self.historypath, "w") as f: with open(self.historypath, "w") as f:
f.write(str(self.last_mention)) f.write(str(self.last_mention))
self.api.PostDirectMessage("Help! I broke down. restart me pls :$", self.user_id, self.screen_name) self.api.PostDirectMessage("Help! I broke down. restart me pls :$",
self.user_id, self.screen_name)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -14,7 +14,9 @@ consumer_secret = yourconsumersecret"
access_token_key = "youraccesstokenkey" access_token_key = "youraccesstokenkey"
access_token_secret = "youraccesstokensecret" access_token_secret = "youraccesstokensecret"
[trigger] # shutdown_contact_userid = 012345
goodlist_path = "goodlists" # shutdown_contact_screen_name = 'yourscreenname'
blacklist_path = "blacklists"
# [trigger]
# goodlist_path = 'goodlists'
# blacklist_path = 'blacklists'