From 48ba20e26612731107290fcffb859162ed7f3478 Mon Sep 17 00:00:00 2001 From: b3yond Date: Sat, 17 Jun 2017 19:55:52 +0200 Subject: [PATCH] reworked the retweetbot as a class --- retweetbot.py | 131 ++++++++++++++++++++++++++++++++++++++++++++++++ twitter/main.py | 85 ------------------------------- 2 files changed, 131 insertions(+), 85 deletions(-) create mode 100644 retweetbot.py delete mode 100644 twitter/main.py diff --git a/retweetbot.py b/retweetbot.py new file mode 100644 index 0000000..82ad724 --- /dev/null +++ b/retweetbot.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python + +import twitter +import requests +from time import sleep + + +class Retweetbot(object): + """ + :todo description + + api: The api object, generated with your oAuth keys, responsible for 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 + """ + + def __init__(self, keypath="appkeys/ticketfrei@twitter.com", + historypath="last_mention", + triggerpath="triggerwords"): + """ + 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.api = twitter.Api(consumer_key=keys[0].strip(), + consumer_secret=keys[1].strip(), + access_token_key=keys[2].strip(), + access_token_secret=keys[3].strip()) + self.historypath = historypath + self.triggerpath = triggerpath + self.last_mention = bot.get_history(self.historypath) + self.triggers = bot.get_trigger(self.triggerpath) + + def get_api_keys(self, path): + """ + 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 + """ + with open(path, "r") as f: + keys = f.readlines() + return keys + + def get_history(self, path): + """ This counter is needed to keep track of your mentions, so you don't double RT them """ + with open(path, "r+") as f: + last_mention = f.read() + return last_mention + + def get_trigger(self, path): + """ Words which have to be included into the tweets for the tweet to get retweeted """ + with open(path, "r") as f: + triggers = [s.strip() for s in f.readlines()] + return triggers + + def bridge_mastodon(self, status): + """ + Bridge your Retweets to mastodon. + :todo vmann: add all the mastodon API magic. + + :param status: Object of a tweet. + :return: toot: text tooted on mastodon, e.g. "_b3yond: There are uniformed controllers in the U2 at Opernhaus." + """ + toot = status.user.name + ": " + status.text + return toot + + def flow(self): + """ + + """ + try: + while 1: + sleep(1) + + # Store all mentions in a list of Status Objects + done = False + while not done: + try: + mentions = self.api.GetMentions(since_id=self.last_mention) + done = True + except requests.exceptions.ConnectionError: + print("[ERROR] Bad Connection.") + sleep(10) + + print mentions # debug + for status in mentions: + + # Is the Text of the Tweet in the triggerlist? + should_retweet = False + for triggerword in self.triggers: + if status.text.lower().find(triggerword): + should_retweet = True + break + + # Retweet status + if should_retweet: + done = False + while not done: + try: + self.api.PostRetweet(status.id) + self.bridge_mastodon(status) + done = True + + # Hopefully we got rid of this error. If not, try to uncomment these lines. + # except twitter.error.TwitterError: + # print("[ERROR] probably you already retweeted this tweet.") + # done = True + except requests.exceptions.ConnectionError: + print("[ERROR] Bad Connection.") + sleep(10) + + # save the id so it doesn't get crawled again + self.last_mention = status.id + print self.last_mention + except: + print "[ERROR] Shit went wrong, closing down." + with open(self.historypath, "w") as f: + f.write(str(self.last_mention)) + self.api.PostDirectMessage("Help! I broke down. restart me pls :$", "801098086005243904", "links_tech") + + +if __name__ == "main": + # create an Api object + bot = Retweetbot() diff --git a/twitter/main.py b/twitter/main.py deleted file mode 100644 index 7b87b70..0000000 --- a/twitter/main.py +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python - -__author__ = "b3yond" - -import twitter -import requests -from time import sleep - -""" -How to get these keys is described in doc/twitter_api.md - -After you received keys, store them in ../../api_keys, one at a line. -""" -with open("../appkeys/ticketfrei@twitter.com", "r") as file: - keys = file.readlines() - for status in keys: - print status, - - -# create an Api object -api = twitter.Api(consumer_key = keys[0].strip(), - consumer_secret = keys[1].strip(), - access_token_key = keys[2].strip(), - access_token_secret = keys[3].strip()) - - -# This counter is needed to keep track which was the last tweet you retweeted -# ACTUALLY it keeps track of the last mention, whether you retweeted it or not. -with open("../last_rt", "r+") as file: - last_rt = file.read() - - -# Words which have to be included into the tweets for the tweet to get retweeted -with open("../triggerwords", "r") as file: - triggers = [s.strip() for s in file.readlines()] - -try: - while 1: - sleep(1) - - # Store all mentions in a list of Status Objects - done = False - while not done: - try: - mentions = api.GetMentions(since_id=last_rt) - done = True - except requests.exceptions.ConnectionError: - print("[ERROR] Bad Connection.") - sleep(10) - - print mentions # debug - for status in mentions: - - # Is the Text of the Tweet in the triggerlist? - should_retweet = False - for triggerword in triggers: - if status.text.lower().find(triggerword): - should_retweet = True - break - - # Retweet status - if should_retweet: - done = False - while not done: - try: - api.PostRetweet(status.id) - done = True - - # This is an Error we need to get rid of. Why are tweets RTed twice? -# except twitter.error.TwitterError: -# print("[ERROR] probably you already retweeted this tweet.") -# done = True - except requests.exceptions.ConnectionError: - print("[ERROR] Bad Connection.") - sleep(10) - - # save the id so it doesn't get crawled again - last_rt = status.id - print last_rt - -except: - print "[ERROR] Shit went wrong, closing down." - with open("../last_rt", "w") as file: - file.write(str(last_rt)) - api.PostDirectMessage("Help! I broke down. restart me pls :$", "801098086005243904", "links_tech") \ No newline at end of file