From 4b953f54e596b3abd58a627603dc6cb7f6b19f2c Mon Sep 17 00:00:00 2001 From: b3yond Date: Sun, 9 Sep 2018 17:29:06 +0200 Subject: [PATCH] Making Twitter Rate Limiting intelligent #35 --- active_bots/twitterbot.py | 8 ++++++++ backend.py | 1 - db.py | 6 ++++++ user.py | 10 ++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/active_bots/twitterbot.py b/active_bots/twitterbot.py index 37b7029..eb4e4bd 100755 --- a/active_bots/twitterbot.py +++ b/active_bots/twitterbot.py @@ -4,6 +4,7 @@ import logging import tweepy import re import requests +from time import time import report from bot import Bot @@ -28,6 +29,11 @@ class TwitterBot(Bot): :return: reports: (list of report.Report objects) """ reports = [] + try: + if user.get_last_twitter_request() + 60 > time(): + return reports + except TypeError: + user.set_last_twitter_request(time()) try: api = self.get_api(user) except Exception: @@ -39,6 +45,8 @@ class TwitterBot(Bot): mentions = api.mentions_timeline() else: mentions = api.mentions_timeline(since_id=last_mention) + user.set_last_twitter_request(time()) + print(time()) for status in mentions: text = re.sub( "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", diff --git a/backend.py b/backend.py index 77189f3..47997a2 100755 --- a/backend.py +++ b/backend.py @@ -37,7 +37,6 @@ if __name__ == '__main__': continue for bot2 in bots: bot2.post(user, status) - time.sleep(60) # twitter rate limit >.< except Exception: logger.error("Shutdown.", exc_info=True) shutdown() diff --git a/db.py b/db.py index 473b24a..b46e51a 100644 --- a/db.py +++ b/db.py @@ -90,6 +90,12 @@ class DB(object): active INTEGER, FOREIGN KEY(user_id) REFERENCES user(id) ); + CREATE TABLE IF NOT EXISTS twitter_last_request ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + user_id INTEGER, + date INTEGER, + FOREIGN KEY(user_id) REFERENCES user(id) + ); CREATE TABLE IF NOT EXISTS telegram_accounts ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER, diff --git a/user.py b/user.py index 9356518..2e5615b 100644 --- a/user.py +++ b/user.py @@ -144,6 +144,16 @@ schlitz keys.append(row[1]) return keys + def get_last_twitter_request(self): + db.execute("SELECT date FROM twitter_last_request WHERE user_id = ?;", + (self.uid,)) + return db.cur.fetchone()[0] + + def set_last_twitter_request(self, date): + db.execute("UPDATE twitter_last_request SET date = ? WHERE user_id = ?;", + (date, self.uid)) + db.commit() + def get_seen_toot(self): db.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;", (self.uid,))