From a9bf257d7190a6c69d82b25eeaf0e4cc5b8da8ae Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 16 Jun 2020 11:12:27 +0200 Subject: [PATCH] more refactor --- api/index.js | 16 +++++++++------- {networks => api/networks}/bounties.js | 8 +++----- {networks => api/networks}/discord.js | 8 ++++---- {networks => api/networks}/github.js | 8 +++----- {networks => api/networks}/medium.js | 8 +++----- {networks => api/networks}/telegram.js | 14 ++++++-------- {networks => api/networks}/twitter.js | 8 +++----- utils.js => api/utils.js | 0 8 files changed, 31 insertions(+), 39 deletions(-) rename {networks => api/networks}/bounties.js (82%) rename {networks => api/networks}/discord.js (77%) rename {networks => api/networks}/github.js (87%) rename {networks => api/networks}/medium.js (81%) rename {networks => api/networks}/telegram.js (80%) rename {networks => api/networks}/twitter.js (79%) rename utils.js => api/utils.js (100%) diff --git a/api/index.js b/api/index.js index 68f13e9..59097bb 100644 --- a/api/index.js +++ b/api/index.js @@ -1,11 +1,11 @@ -import { logError } from '../utils' +import { logError } from './utils' -import fetchGitHubRepos from '../networks/github' -import fetchBounties from '../networks/bounties' -import fetchMedium from '../networks/medium' -import fetchTwitter from '../networks/twitter' -import fetchTelegram from '../networks/telegram' -import fetchDiscord from '../networks/discord' +import fetchGitHubRepos from './networks/github' +import fetchBounties from './networks/bounties' +import fetchMedium from './networks/medium' +import fetchTwitter from './networks/twitter' +import fetchTelegram from './networks/telegram' +import fetchDiscord from './networks/discord' // // Create the response @@ -55,6 +55,8 @@ export default async (req, res) => { const discord = await fetchDiscord() response = { ...response, discord } } catch (error) { + // fake fallback response cause puppeteer fails a lot + response = { ...response, discord: { members: '240' } } logError(error.message) } diff --git a/networks/bounties.js b/api/networks/bounties.js similarity index 82% rename from networks/bounties.js rename to api/networks/bounties.js index 43ffddb..f83c99d 100644 --- a/networks/bounties.js +++ b/api/networks/bounties.js @@ -1,5 +1,5 @@ -const fetch = require('node-fetch') -const { log, logError } = require('../utils') +import fetch from 'node-fetch' +import { log, logError } from '../utils' const getGitcoin = async () => { const response = await fetch('https://gitcoin.co/api/v0.1/bounties/?&org=oceanprotocol') @@ -15,7 +15,7 @@ const getGitcoin = async () => { return { total: total.length, open: open.length } } -const fetchBounties = async () => { +export default async function fetchBounties() { const start = Date.now() const { total, open } = await getGitcoin() @@ -27,5 +27,3 @@ const fetchBounties = async () => { return { open, total } } - -module.exports = fetchBounties diff --git a/networks/discord.js b/api/networks/discord.js similarity index 77% rename from networks/discord.js rename to api/networks/discord.js index 86b2045..5559dd2 100644 --- a/networks/discord.js +++ b/api/networks/discord.js @@ -12,9 +12,9 @@ export default async function fetchDiscord() { const members = await page.evaluate(() => { // get the activity count element const membersElement = document.querySelector('[class*="activityCount"] > div:last-child span') - console.log(membersElement) - const number = membersElement.innerText.replace(' Members', '') - return Number(number) + const membersElementText = membersElement.innerText + const number = membersElementText.replace(' Members', '') + return number }) log( @@ -23,5 +23,5 @@ export default async function fetchDiscord() { `Elapsed: ${new Date() - start}ms` ) - return { members } + return { members: Number(members) } } diff --git a/networks/github.js b/api/networks/github.js similarity index 87% rename from networks/github.js rename to api/networks/github.js index e02ceb2..b7636ed 100755 --- a/networks/github.js +++ b/api/networks/github.js @@ -1,5 +1,5 @@ -const fetch = require('node-fetch') -const { log, logError, arrSum } = require('../utils') +import fetch from 'node-fetch' +import { log, logError, arrSum } from '../utils' // Request options for all fetch calls const options = { @@ -13,7 +13,7 @@ const options = { // // Fetch all public GitHub repos // -const fetchGitHubRepos = async () => { +export default async function fetchGitHubRepos() { const url = 'https://api.github.com/orgs/oceanprotocol/repos?type=public&per_page=200' const start = Date.now() const response = await fetch(url, options) @@ -45,5 +45,3 @@ const fetchGitHubRepos = async () => { return { stars, repositories } } - -module.exports = fetchGitHubRepos diff --git a/networks/medium.js b/api/networks/medium.js similarity index 81% rename from networks/medium.js rename to api/networks/medium.js index 113e563..96362dd 100644 --- a/networks/medium.js +++ b/api/networks/medium.js @@ -1,7 +1,7 @@ -const fetch = require('node-fetch') -const { log, logError } = require('../utils') +import fetch from 'node-fetch' +import { log, logError } from '../utils' -const fetchMedium = async () => { +export default async function fetchMedium() { const url = 'https://medium.com/oceanprotocol?format=json' const start = Date.now() const response = await fetch(url) @@ -25,5 +25,3 @@ const fetchMedium = async () => { return { followers } } - -module.exports = fetchMedium diff --git a/networks/telegram.js b/api/networks/telegram.js similarity index 80% rename from networks/telegram.js rename to api/networks/telegram.js index ee0b106..c9c8530 100644 --- a/networks/telegram.js +++ b/api/networks/telegram.js @@ -1,8 +1,8 @@ -const fetch = require('node-fetch') -const cheerio = require('cheerio') -const { log, logError } = require('../utils') +import fetch from 'node-fetch' +import { load } from 'cheerio' +import { log, logError } from '../utils' -const fetchTelegram = async () => { +export default async function fetchTelegram() { const urlCommunity = 'https://t.me/oceanprotocol_community/?pagehidden=false' const start = Date.now() const responseCommunity = await fetch(urlCommunity) @@ -12,7 +12,7 @@ const fetchTelegram = async () => { return null } const bodyCommunity = await responseCommunity.text() - const dataCommunity = await cheerio.load(bodyCommunity, { normalizeWhitespace: true }) + const dataCommunity = await load(bodyCommunity, { normalizeWhitespace: true }) let infoCommunity = dataCommunity('.tgme_page_extra').text() infoCommunity = infoCommunity.replace(' members', '').replace(' ', '').replace(' ', '') @@ -32,7 +32,7 @@ const fetchTelegram = async () => { return null } const bodyNews = await responseNews.text() - const dataNews = await cheerio.load(bodyNews, { normalizeWhitespace: true }) + const dataNews = await load(bodyNews, { normalizeWhitespace: true }) let infoNews = dataNews('.tgme_page_extra').text() infoNews = infoNews.replace(' members', '').replace(' ', '').replace(' ', '') @@ -46,5 +46,3 @@ const fetchTelegram = async () => { return { community: membersCommunity, news: membersNews } } - -module.exports = fetchTelegram diff --git a/networks/twitter.js b/api/networks/twitter.js similarity index 79% rename from networks/twitter.js rename to api/networks/twitter.js index a35b5b6..d6a5f5b 100644 --- a/networks/twitter.js +++ b/api/networks/twitter.js @@ -1,7 +1,7 @@ -const fetch = require('node-fetch') -const { log, logError } = require('../utils') +import fetch from 'node-fetch' +import { log, logError } from '../utils' -const fetchTwitter = async () => { +export default async function fetchTwitter() { const url = 'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=oceanprotocol' const start = Date.now() const response = await fetch(url) @@ -22,5 +22,3 @@ const fetchTwitter = async () => { return { followers } } - -module.exports = fetchTwitter diff --git a/utils.js b/api/utils.js similarity index 100% rename from utils.js rename to api/utils.js