Merge pull request #49 from ticketfrei/rate-limit-39

Rate limit 39
This commit is contained in:
b3yond 2018-10-07 22:19:57 +02:00 committed by GitHub
commit 9ca521493a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 27 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')

16
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,
@ -251,12 +257,10 @@ u\d\d?"""
(uid, json['email']))
self.execute("""INSERT INTO telegram_accounts (user_id, apikey,
active) VALUES(?, ?, ?);""", (uid, "", 1))
self.execute(
"INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?, ?);", (uid, 0))
self.execute(
"INSERT INTO seen_mail (user_id, mail_date) VALUES (?, ?);", (uid, 0))
self.execute("INSERT INTO seen_tweets (user_id, tweet_id) VALUES (?, ?)",
(uid, 0))
self.execute("INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?, ?);", (uid, 0))
self.execute("INSERT INTO seen_mail (user_id, mail_date) VALUES (?, ?);", (uid, 0))
self.execute("INSERT INTO seen_tweets (user_id, tweet_id) VALUES (?, ?)", (uid, 0))
self.execute("INSERT INTO twitter_last_request (user_id, date) VALUES (?, ?)", (uid, 0))
self.commit()
user = User(uid)
user.set_city(city)

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