Added rudimentary rate limit handling
This commit is contained in:
parent
ada8436997
commit
ffd12aa758
21
twitter.js
21
twitter.js
@ -10,10 +10,28 @@ const twitter = new Twitter({
|
|||||||
const defaultProfile = {
|
const defaultProfile = {
|
||||||
original: 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png'
|
original: 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png'
|
||||||
};
|
};
|
||||||
|
const rateLimit = {
|
||||||
|
remaining: Infinity,
|
||||||
|
reset: new Date()
|
||||||
|
};
|
||||||
|
|
||||||
const cleanHandle = handle => (handle[0] === '@') ? handle.slice(1) : handle;
|
const cleanHandle = handle => (handle[0] === '@') ? handle.slice(1) : handle;
|
||||||
|
const sleep = timeout => new Promise(resolve => setTimeout(resolve, timeout * 1000));
|
||||||
|
const handleRateLimit = async () => {
|
||||||
|
if (rateLimit.remaining <= 1) {
|
||||||
|
console.log('Rate limit hit, waiting until reset');
|
||||||
|
await sleep(rateLimit.reset - new Date());
|
||||||
|
} else if (rateLimit.remaining < 10) {
|
||||||
|
console.log('Waiting for 10s due to rate limiting');
|
||||||
|
await sleep(10);
|
||||||
|
} else if (rateLimit.remaining < 500) {
|
||||||
|
console.log('Waiting for 1s due to rate limiting');
|
||||||
|
await sleep(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const fetchFromTwitter = async handle => {
|
const fetchFromTwitter = async handle => {
|
||||||
|
await handleRateLimit();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
twitter.get('users/show', {
|
twitter.get('users/show', {
|
||||||
user_id: handle,
|
user_id: handle,
|
||||||
@ -23,6 +41,9 @@ const fetchFromTwitter = async handle => {
|
|||||||
return resolve(defaultProfile);
|
return resolve(defaultProfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rateLimit.remaining = response.headers['x-rate-limit-remaining'];
|
||||||
|
rateLimit.reset = new Date(response.headers['x-rate-limit-reset'] * 1000);
|
||||||
|
|
||||||
const profileURLs = {
|
const profileURLs = {
|
||||||
normal: user.profile_image_url_https,
|
normal: user.profile_image_url_https,
|
||||||
bigger: user.profile_image_url_https.replace('_normal.', '_bigger.'),
|
bigger: user.profile_image_url_https.replace('_normal.', '_bigger.'),
|
||||||
|
Loading…
Reference in New Issue
Block a user