changed twitter to work with db

This commit is contained in:
b3yond 2018-03-23 17:35:04 +01:00
parent c548a81272
commit 5f55eb88ff
3 changed files with 30 additions and 31 deletions

View file

@ -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

View file

@ -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)
"""

14
user.py
View file

@ -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: