twitter-profile-pic/twitter.js

71 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-01-14 13:50:34 +00:00
const cache = require('./cache');
2018-01-14 13:25:23 +00:00
const Twitter = require('twitter');
const twitter = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
const defaultProfile = {
original: 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png'
};
2018-01-14 14:10:25 +00:00
const rateLimit = {
remaining: Infinity,
reset: new Date()
};
2018-01-14 13:25:23 +00:00
const cleanHandle = handle => (handle[0] === '@') ? handle.slice(1) : handle;
2018-01-14 14:10:25 +00:00
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);
}
};
2018-01-14 13:25:23 +00:00
2018-01-14 13:50:34 +00:00
const fetchFromTwitter = async handle => {
2018-01-14 14:10:25 +00:00
await handleRateLimit();
2018-01-14 13:25:23 +00:00
return new Promise((resolve, reject) => {
twitter.get('users/show', {
user_id: handle,
screen_name: handle
}, (err, user, response) => {
if (err || !user) {
return resolve(defaultProfile);
}
2018-01-14 14:10:25 +00:00
rateLimit.remaining = response.headers['x-rate-limit-remaining'];
rateLimit.reset = new Date(response.headers['x-rate-limit-reset'] * 1000);
2018-01-14 13:25:23 +00:00
const profileURLs = {
normal: user.profile_image_url_https,
bigger: user.profile_image_url_https.replace('_normal.', '_bigger.'),
mini: user.profile_image_url_https.replace('_normal.', '_mini.'),
original: user.profile_image_url_https.replace('_normal.', '.'),
'200x200': user.profile_image_url_https.replace('_normal.', '_200x200.'),
'400x400': user.profile_image_url_https.replace('_normal.', '_400x400.')
}
2018-01-14 13:50:34 +00:00
// Try and mitigate hitting rate limit
cache.save(handle, profileURLs);
2018-01-14 13:25:23 +00:00
return resolve(profileURLs);
});
});
2018-01-14 13:50:34 +00:00
}
const getProfileURLs = async handle => {
handle = cleanHandle(handle);
return await cache.get(handle)|| await fetchFromTwitter(handle);
2018-01-14 13:25:23 +00:00
};
module.exports = {
getProfileURLs,
defaultProfile
};