From d9be9f7705dd2aee11cd78ec49c17708de465372 Mon Sep 17 00:00:00 2001 From: b3yond Date: Fri, 23 Mar 2018 17:35:04 +0100 Subject: [PATCH] changed twitter to work with db --- backend.py | 4 ++-- retweetbot.py | 43 ++++++++++++++----------------------------- user.py | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/backend.py b/backend.py index 24302d9..917bb39 100755 --- a/backend.py +++ b/backend.py @@ -7,7 +7,7 @@ import sendmail from db import DB from retootbot import RetootBot -# from retweetbot import RetweetBot +from retweetbot import RetweetBot from mailbot import Mailbot from trigger import Trigger @@ -23,7 +23,7 @@ def get_users(db): def init_bots(config, logger, db, users): for uid in users: users[uid].append(RetootBot(config, logger, uid, db)) - # users[uid].append(RetweetBot(config, uid, db)) + users[uid].append(RetweetBot(config, logger, uid, db)) users[uid].append(Mailbot(config, logger, uid, db)) return users diff --git a/retweetbot.py b/retweetbot.py index 26ac4b4..d9e44e3 100755 --- a/retweetbot.py +++ b/retweetbot.py @@ -3,11 +3,10 @@ import tweepy import re import requests -import trigger from time import sleep import report -import backend -import sendmail +from user import User + class RetweetBot(object): """ @@ -20,7 +19,7 @@ class RetweetBot(object): last_mention: the ID of the last tweet which mentioned you """ - def __init__(self, config, history_path="last_mention"): + def __init__(self, config, logger, uid, db): """ Initializes the bot and loads all the necessary data. @@ -29,7 +28,9 @@ class RetweetBot(object): Tweet """ self.config = config - self.logger = backend.get_logger(config) + self.logger = logger + self.db = db + self.user = User(db, uid) # initialize API access keys = self.get_api_keys() @@ -39,8 +40,7 @@ class RetweetBot(object): keys[3]) # access_token_secret self.api = tweepy.API(auth) - self.history_path = history_path - self.last_mention = self.get_history(self.history_path) + self.last_mention = self.user.get_seen_tweet() self.waitcounter = 0 def get_api_keys(self): @@ -58,31 +58,15 @@ class RetweetBot(object): :return: keys: list of these 4 strings. """ - keys = [self.config['tapp']['consumer_key'], self.config['tapp']['consumer_secret'], - self.config['tuser']['access_token_key'], self.config['tuser']['access_token_secret']] + keys = [self.config['twitter']['consumer_key'], self.config['twitter']['consumer_secret']] + row = self.user.get_twitter_token() + keys.append(row[0]) + keys.append(row[1]) return keys - def get_history(self, path): - """ 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. - :return: last_mention: ID of the last tweet which mentioned the bot - """ - try: - with open(path, "r+") as f: - last_mention = f.read() - except IOError: - with open(path, "w+") as f: - last_mention = "0" - f.write(last_mention) - return int(last_mention) - def save_last(self): """ Saves the last retweeted tweet in last_mention. """ - with open(self.history_path, "w") as f: - f.write(str(self.last_mention)) + self.user.save_seen_tweet(self.last_mention) def waiting(self): """ @@ -198,7 +182,7 @@ class RetweetBot(object): # Return Retweets for posting on other bots return all_tweets - +""" if __name__ == "__main__": config = backend.get_config() @@ -225,3 +209,4 @@ if __name__ == "__main__": attachment=config['logging']['logpath']) except: bot.logger.error('Mail sending failed', exc_info=True) +""" \ No newline at end of file diff --git a/user.py b/user.py index 4a667bb..84fcc53 100644 --- a/user.py +++ b/user.py @@ -27,6 +27,15 @@ class User(object): self.db.cur.execute("UPDATE seen_toots SET toot_id = ? WHERE user_id = ?;", (toot_id, self.uid)) + def get_seen_tweet(self): + self.db.cur.execute("SELECT tweet_id FROM seen_tweets WHERE user_id = ?;", + (self.uid, )) + return self.db.cur.fetchone()[0] + + def save_seen_tweet(self, tweet_id): + self.db.cur.execute("UPDATE seen_tweets SET tweet_id = ? WHERE user_id = ?;", + (tweet_id, self.uid)) + def get_mail(self): self.db.cur.execute("SELECT email FROM mail WHERE user_id = ?;", (self.uid, )) @@ -59,6 +68,11 @@ class User(object): (id, access_token, access_token_secret)) self.db.conn.commit() + def get_twitter_token(self): + self.db.cur.execute("SELECT access_token, access_token_secret FROM twitter_accouts WHERE user_id = ?;", + (self.uid, )) + return self.db.cur.fetchall() + def get_mastodon_app_keys(self, instance): self.db.cur.execute("SELECT client_id, client_secret FROM mastodon_instances WHERE instance = ?;", (instance, )) try: