Added rudimentary rate limit handling

This commit is contained in:
Marcus Noble 2018-01-14 14:10:25 +00:00
parent ada8436997
commit ffd12aa758
1 changed files with 21 additions and 0 deletions

View File

@ -10,10 +10,28 @@ const twitter = new Twitter({
const defaultProfile = {
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 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 => {
await handleRateLimit();
return new Promise((resolve, reject) => {
twitter.get('users/show', {
user_id: handle,
@ -23,6 +41,9 @@ const fetchFromTwitter = async handle => {
return resolve(defaultProfile);
}
rateLimit.remaining = response.headers['x-rate-limit-remaining'];
rateLimit.reset = new Date(response.headers['x-rate-limit-reset'] * 1000);
const profileURLs = {
normal: user.profile_image_url_https,
bigger: user.profile_image_url_https.replace('_normal.', '_bigger.'),