From 8c96f01d70697ba7512c6db5fba6e92defc9c063 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 14 May 2019 20:59:15 +0200 Subject: [PATCH] add twitter follower count --- README.md | 3 +++ index.js | 9 ++++++++- networks/twitter.js | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 networks/twitter.js diff --git a/README.md b/README.md index 9969cad..46ddb03 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ Endpoint: [`https://oceanprotocol-community.now.sh`](https://oceanprotocol-commu "gitcoin": 1000, "bountiesNetwork": 1000, "total": 1000 + }, + "twitter": { + "followers": 1000 } } } diff --git a/index.js b/index.js index 0b44f6c..3aaf644 100644 --- a/index.js +++ b/index.js @@ -4,10 +4,12 @@ const { logError } = require('./util/logger') const fetchGitHubRepos = require('./networks/github') const fetchBounties = require('./networks/bounties') const fetchMedium = require('./networks/medium') +const fetchTwitter = require('./networks/twitter') let cacheGithub = null let cacheBounties = null let cacheMedium = null +let cacheTwitter = null // // Create the response @@ -28,6 +30,10 @@ module.exports = async (req, res) => { if (!cacheMedium || Date.now() - cacheMedium.lastUpdate > ms('5m')) { cacheMedium = await fetchMedium() } + + if (!cacheTwitter || Date.now() - cacheTwitter.lastUpdate > ms('5m')) { + cacheTwitter = await fetchTwitter() + } } catch (error) { logError(error.message) } @@ -35,6 +41,7 @@ module.exports = async (req, res) => { res.end(JSON.stringify({ github: cacheGithub, bounties: cacheBounties, - medium: cacheMedium + medium: cacheMedium, + twitter: cacheTwitter })) } diff --git a/networks/twitter.js b/networks/twitter.js new file mode 100644 index 0000000..629197c --- /dev/null +++ b/networks/twitter.js @@ -0,0 +1,26 @@ +const fetch = require('node-fetch') +const { log, logError } = require('../util/logger') + +const fetchTwitter = async () => { + const url = 'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=oceanprotocol' + const start = Date.now() + const response = await fetch(url) + + if (response.status !== 200) { + logError(`Non-200 response code from Twitter: ${response.status}`) + return null + } + + const json = await response.json() + const followers = json[0].followers_count + + log( + `Re-built twitter cache. ` + + `Total: ${followers} followers. ` + + `Elapsed: ${new Date() - start}ms` + ) + + return { followers } +} + +module.exports = fetchTwitter