Making Twitter Rate Limiting intelligent #35
This commit is contained in:
parent
439dbeb1fa
commit
4b953f54e5
|
@ -4,6 +4,7 @@ import logging
|
||||||
import tweepy
|
import tweepy
|
||||||
import re
|
import re
|
||||||
import requests
|
import requests
|
||||||
|
from time import time
|
||||||
import report
|
import report
|
||||||
from bot import Bot
|
from bot import Bot
|
||||||
|
|
||||||
|
@ -28,6 +29,11 @@ class TwitterBot(Bot):
|
||||||
:return: reports: (list of report.Report objects)
|
:return: reports: (list of report.Report objects)
|
||||||
"""
|
"""
|
||||||
reports = []
|
reports = []
|
||||||
|
try:
|
||||||
|
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 Exception:
|
except Exception:
|
||||||
|
@ -39,6 +45,8 @@ 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)
|
||||||
|
user.set_last_twitter_request(time())
|
||||||
|
print(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-_]+)",
|
||||||
|
|
|
@ -37,7 +37,6 @@ if __name__ == '__main__':
|
||||||
continue
|
continue
|
||||||
for bot2 in bots:
|
for bot2 in bots:
|
||||||
bot2.post(user, status)
|
bot2.post(user, status)
|
||||||
time.sleep(60) # twitter rate limit >.<
|
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.error("Shutdown.", exc_info=True)
|
logger.error("Shutdown.", exc_info=True)
|
||||||
shutdown()
|
shutdown()
|
||||||
|
|
6
db.py
6
db.py
|
@ -90,6 +90,12 @@ class DB(object):
|
||||||
active INTEGER,
|
active INTEGER,
|
||||||
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 telegram_accounts (
|
CREATE TABLE IF NOT EXISTS telegram_accounts (
|
||||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
||||||
user_id INTEGER,
|
user_id INTEGER,
|
||||||
|
|
10
user.py
10
user.py
|
@ -144,6 +144,16 @@ schlitz
|
||||||
keys.append(row[1])
|
keys.append(row[1])
|
||||||
return keys
|
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):
|
def get_seen_toot(self):
|
||||||
db.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;",
|
db.execute("SELECT toot_id FROM seen_toots WHERE user_id = ?;",
|
||||||
(self.uid,))
|
(self.uid,))
|
||||||
|
|
Loading…
Reference in a new issue