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

This commit is contained in:
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 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-_]+)",

View file

@ -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-_]+)",

View file

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

6
db.py
View file

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

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