From 484c71c895a98cd308099003949600828c8c6b87 Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Sun, 14 Jan 2018 14:16:52 +0000 Subject: [PATCH] Added size support to default image --- twitter.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/twitter.js b/twitter.js index 608f285..8488a75 100644 --- a/twitter.js +++ b/twitter.js @@ -7,16 +7,15 @@ const twitter = new Twitter({ 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 = { 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'); @@ -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 => { await handleRateLimit(); return new Promise((resolve, reject) => { @@ -44,14 +56,7 @@ const fetchFromTwitter = async handle => { 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.'), - 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.') - } + const profileURLs = generateSizes(user.profile_image_url_https); // Try and mitigate hitting rate limit cache.save(handle, profileURLs); return resolve(profileURLs);