Making Twitter Rate Limiting intelligent #35
This commit is contained in:
parent
439dbeb1fa
commit
4b953f54e5
|
@ -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-_]+)",
|
||||
|
|
|
@ -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()
|
||||
|
|
6
db.py
6
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,
|
||||
|
|
10
user.py
10
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,))
|
||||
|
|
Loading…
Reference in a new issue