From f59be986e2d785c8d395e25b933780f2be87f866 Mon Sep 17 00:00:00 2001 From: b3yond Date: Sun, 7 Oct 2018 22:10:48 +0200 Subject: [PATCH] reverting #39 - make rate limits per account, not app --- active_bots/twitterDMs.py | 10 ++++++---- active_bots/twitterbot.py | 10 ++++++---- backend.py | 3 --- db.py | 6 ++++++ tfglobals.py | 10 ---------- user.py | 10 ++++++++++ 6 files changed, 28 insertions(+), 21 deletions(-) delete mode 100644 tfglobals.py diff --git a/active_bots/twitterDMs.py b/active_bots/twitterDMs.py index a9bd5e4..36db7a4 100644 --- a/active_bots/twitterDMs.py +++ b/active_bots/twitterDMs.py @@ -5,7 +5,6 @@ import tweepy import re import requests import report -import tfglobals from time import time from bot import Bot @@ -29,8 +28,11 @@ class TwitterBot(Bot): :return: reports: (list of report.Report objects) """ reports = [] - if tfglobals.last_twitter_request + 60 > time(): - return 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 IndexError: @@ -41,7 +43,7 @@ class TwitterBot(Bot): mentions = api.direct_messages() else: mentions = api.mentions_timeline(since_id=last_dm[0]) - tfglobals.last_twitter_request = time() + user.set_last_twitter_request(time()) for status in mentions: text = re.sub( "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", diff --git a/active_bots/twitterbot.py b/active_bots/twitterbot.py index 24ea012..caf3ec5 100755 --- a/active_bots/twitterbot.py +++ b/active_bots/twitterbot.py @@ -7,7 +7,6 @@ import requests from time import time import report from bot import Bot -import tfglobals logger = logging.getLogger(__name__) @@ -31,8 +30,11 @@ class TwitterBot(Bot): """ reports = [] #global last_twitter_request - if tfglobals.last_twitter_request + 60 > time(): - return 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 TypeError: @@ -46,7 +48,7 @@ class TwitterBot(Bot): mentions = api.mentions_timeline() else: mentions = api.mentions_timeline(since_id=last_mention) - tfglobals.last_twitter_request = time() + user.set_last_twitter_request(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 13dcfe7..fd18aee 100755 --- a/backend.py +++ b/backend.py @@ -5,7 +5,6 @@ from config import config from db import db import logging from sendmail import sendmail -from time import time def shutdown(): @@ -16,8 +15,6 @@ def shutdown(): exit(1) -last_twitter_request = time() - if __name__ == '__main__': logger = logging.getLogger() fh = logging.FileHandler('/var/log/ticketfrei/backend.log') diff --git a/db.py b/db.py index cd6b99b..d8c3275 100644 --- a/db.py +++ b/db.py @@ -141,6 +141,12 @@ class DB(object): mail_date REAL, 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 cities ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER, diff --git a/tfglobals.py b/tfglobals.py deleted file mode 100644 index 32282b5..0000000 --- a/tfglobals.py +++ /dev/null @@ -1,10 +0,0 @@ -from time import time - -""" -This file is for shared global variables. They only stay during runtime. - -For reference: -https://stackoverflow.com/questions/15959534/visibility-of-global-variables-in-imported-modules -""" - -last_twitter_request = time() diff --git a/user.py b/user.py index 9074b5a..dad9b1c 100644 --- a/user.py +++ b/user.py @@ -93,6 +93,16 @@ schlitz return False return True + 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_telegram_credentials(self): db.execute("""SELECT apikey FROM telegram_accounts