Added size support to default image

This commit is contained in:
Marcus Noble 2018-01-14 14:16:52 +00:00
parent ff599f7893
commit 484c71c895

View File

@ -7,16 +7,15 @@ const twitter = new Twitter({
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
}); });
const defaultProfile = {
original: 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png'
};
const rateLimit = { const rateLimit = {
remaining: Infinity, remaining: Infinity,
reset: new Date() 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 sleep = timeout => new Promise(resolve => setTimeout(resolve, timeout * 1000));
const handleRateLimit = async () => { const handleRateLimit = async () => {
if (rateLimit.remaining <= 1) { if (rateLimit.remaining <= 1) {
console.log('Rate limit hit, waiting until reset'); console.log('Rate limit hit, waiting until reset');
@ -30,6 +29,19 @@ const handleRateLimit = async () => {
} }
}; };
const generateSizes = url => {
return {
normal: url,
bigger: url.replace('_normal.', '_bigger.'),
mini: url.replace('_normal.', '_mini.'),
original: url.replace('_normal.', '.'),
'200x200': url.replace('_normal.', '_200x200.'),
'400x400': url.replace('_normal.', '_400x400.')
};
};
const defaultProfile = generateSizes('https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png');
const fetchFromTwitter = async handle => { const fetchFromTwitter = async handle => {
await handleRateLimit(); await handleRateLimit();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -44,14 +56,7 @@ const fetchFromTwitter = async handle => {
rateLimit.remaining = response.headers['x-rate-limit-remaining']; rateLimit.remaining = response.headers['x-rate-limit-remaining'];
rateLimit.reset = new Date(response.headers['x-rate-limit-reset'] * 1000); rateLimit.reset = new Date(response.headers['x-rate-limit-reset'] * 1000);
const profileURLs = { const profileURLs = generateSizes(user.profile_image_url_https);
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.')
}
// Try and mitigate hitting rate limit // Try and mitigate hitting rate limit
cache.save(handle, profileURLs); cache.save(handle, profileURLs);
return resolve(profileURLs); return resolve(profileURLs);