reverting #39 - make rate limits per account, not app

coc
b3yond 2018-10-07 22:10:48 +02:00
parent 79d5a6f112
commit f59be986e2
6 changed files with 28 additions and 21 deletions

View File

@ -5,7 +5,6 @@ import tweepy
import re import re
import requests import requests
import report import report
import tfglobals
from time import time from time import time
from bot import Bot from bot import Bot
@ -29,8 +28,11 @@ class TwitterBot(Bot):
:return: reports: (list of report.Report objects) :return: reports: (list of report.Report objects)
""" """
reports = [] reports = []
if tfglobals.last_twitter_request + 60 > time(): try:
return reports if user.get_last_twitter_request() + 60 > time():
return reports
except TypeError:
user.set_last_twitter_request(time())
try: try:
api = self.get_api(user) api = self.get_api(user)
except IndexError: except IndexError:
@ -41,7 +43,7 @@ class TwitterBot(Bot):
mentions = api.direct_messages() mentions = api.direct_messages()
else: else:
mentions = api.mentions_timeline(since_id=last_dm[0]) mentions = api.mentions_timeline(since_id=last_dm[0])
tfglobals.last_twitter_request = time() user.set_last_twitter_request(time())
for status in mentions: for status in mentions:
text = re.sub( text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",

View File

@ -7,7 +7,6 @@ import requests
from time import time from time import time
import report import report
from bot import Bot from bot import Bot
import tfglobals
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -31,8 +30,11 @@ class TwitterBot(Bot):
""" """
reports = [] reports = []
#global last_twitter_request #global last_twitter_request
if tfglobals.last_twitter_request + 60 > time(): try:
return reports if user.get_last_twitter_request() + 60 > time():
return reports
except TypeError:
user.set_last_twitter_request(time())
try: try:
api = self.get_api(user) api = self.get_api(user)
except TypeError: except TypeError:
@ -46,7 +48,7 @@ class TwitterBot(Bot):
mentions = api.mentions_timeline() mentions = api.mentions_timeline()
else: else:
mentions = api.mentions_timeline(since_id=last_mention) mentions = api.mentions_timeline(since_id=last_mention)
tfglobals.last_twitter_request = time() user.set_last_twitter_request(time())
for status in mentions: for status in mentions:
text = re.sub( text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",

View File

@ -5,7 +5,6 @@ from config import config
from db import db from db import db
import logging import logging
from sendmail import sendmail from sendmail import sendmail
from time import time
def shutdown(): def shutdown():
@ -16,8 +15,6 @@ def shutdown():
exit(1) exit(1)
last_twitter_request = time()
if __name__ == '__main__': if __name__ == '__main__':
logger = logging.getLogger() logger = logging.getLogger()
fh = logging.FileHandler('/var/log/ticketfrei/backend.log') fh = logging.FileHandler('/var/log/ticketfrei/backend.log')

6
db.py
View File

@ -141,6 +141,12 @@ class DB(object):
mail_date REAL, mail_date REAL,
FOREIGN KEY(user_id) REFERENCES user(id) 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 ( CREATE TABLE IF NOT EXISTS cities (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER, user_id INTEGER,

View File

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

10
user.py
View File

@ -93,6 +93,16 @@ schlitz
return False return False
return True 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): def get_telegram_credentials(self):
db.execute("""SELECT apikey db.execute("""SELECT apikey
FROM telegram_accounts FROM telegram_accounts