1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-12-22 17:23:31 +01:00

more refactor

This commit is contained in:
Matthias Kretschmann 2020-06-16 11:12:27 +02:00
parent 0ded2b045e
commit a9bf257d71
Signed by: m
GPG Key ID: 606EEEF3C479A91F
8 changed files with 31 additions and 39 deletions

View File

@ -1,11 +1,11 @@
import { logError } from '../utils' import { logError } from './utils'
import fetchGitHubRepos from '../networks/github' import fetchGitHubRepos from './networks/github'
import fetchBounties from '../networks/bounties' import fetchBounties from './networks/bounties'
import fetchMedium from '../networks/medium' import fetchMedium from './networks/medium'
import fetchTwitter from '../networks/twitter' import fetchTwitter from './networks/twitter'
import fetchTelegram from '../networks/telegram' import fetchTelegram from './networks/telegram'
import fetchDiscord from '../networks/discord' import fetchDiscord from './networks/discord'
// //
// Create the response // Create the response
@ -55,6 +55,8 @@ export default async (req, res) => {
const discord = await fetchDiscord() const discord = await fetchDiscord()
response = { ...response, discord } response = { ...response, discord }
} catch (error) { } catch (error) {
// fake fallback response cause puppeteer fails a lot
response = { ...response, discord: { members: '240' } }
logError(error.message) logError(error.message)
} }

View File

@ -1,5 +1,5 @@
const fetch = require('node-fetch') import fetch from 'node-fetch'
const { log, logError } = require('../utils') import { log, logError } from '../utils'
const getGitcoin = async () => { const getGitcoin = async () => {
const response = await fetch('https://gitcoin.co/api/v0.1/bounties/?&org=oceanprotocol') 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 } return { total: total.length, open: open.length }
} }
const fetchBounties = async () => { export default async function fetchBounties() {
const start = Date.now() const start = Date.now()
const { total, open } = await getGitcoin() const { total, open } = await getGitcoin()
@ -27,5 +27,3 @@ const fetchBounties = async () => {
return { open, total } return { open, total }
} }
module.exports = fetchBounties

View File

@ -12,9 +12,9 @@ export default async function fetchDiscord() {
const members = await page.evaluate(() => { const members = await page.evaluate(() => {
// get the activity count element // get the activity count element
const membersElement = document.querySelector('[class*="activityCount"] > div:last-child span') const membersElement = document.querySelector('[class*="activityCount"] > div:last-child span')
console.log(membersElement) const membersElementText = membersElement.innerText
const number = membersElement.innerText.replace(' Members', '') const number = membersElementText.replace(' Members', '')
return Number(number) return number
}) })
log( log(
@ -23,5 +23,5 @@ export default async function fetchDiscord() {
`Elapsed: ${new Date() - start}ms` `Elapsed: ${new Date() - start}ms`
) )
return { members } return { members: Number(members) }
} }

View File

@ -1,5 +1,5 @@
const fetch = require('node-fetch') import fetch from 'node-fetch'
const { log, logError, arrSum } = require('../utils') import { log, logError, arrSum } from '../utils'
// Request options for all fetch calls // Request options for all fetch calls
const options = { const options = {
@ -13,7 +13,7 @@ const options = {
// //
// Fetch all public GitHub repos // 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 url = 'https://api.github.com/orgs/oceanprotocol/repos?type=public&per_page=200'
const start = Date.now() const start = Date.now()
const response = await fetch(url, options) const response = await fetch(url, options)
@ -45,5 +45,3 @@ const fetchGitHubRepos = async () => {
return { stars, repositories } return { stars, repositories }
} }
module.exports = fetchGitHubRepos

View File

@ -1,7 +1,7 @@
const fetch = require('node-fetch') import fetch from 'node-fetch'
const { log, logError } = require('../utils') import { log, logError } from '../utils'
const fetchMedium = async () => { export default async function fetchMedium() {
const url = 'https://medium.com/oceanprotocol?format=json' const url = 'https://medium.com/oceanprotocol?format=json'
const start = Date.now() const start = Date.now()
const response = await fetch(url) const response = await fetch(url)
@ -25,5 +25,3 @@ const fetchMedium = async () => {
return { followers } return { followers }
} }
module.exports = fetchMedium

View File

@ -1,8 +1,8 @@
const fetch = require('node-fetch') import fetch from 'node-fetch'
const cheerio = require('cheerio') import { load } from 'cheerio'
const { log, logError } = require('../utils') import { log, logError } from '../utils'
const fetchTelegram = async () => { export default async function fetchTelegram() {
const urlCommunity = 'https://t.me/oceanprotocol_community/?pagehidden=false' const urlCommunity = 'https://t.me/oceanprotocol_community/?pagehidden=false'
const start = Date.now() const start = Date.now()
const responseCommunity = await fetch(urlCommunity) const responseCommunity = await fetch(urlCommunity)
@ -12,7 +12,7 @@ const fetchTelegram = async () => {
return null return null
} }
const bodyCommunity = await responseCommunity.text() 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() let infoCommunity = dataCommunity('.tgme_page_extra').text()
infoCommunity = infoCommunity.replace(' members', '').replace(' ', '').replace(' ', '') infoCommunity = infoCommunity.replace(' members', '').replace(' ', '').replace(' ', '')
@ -32,7 +32,7 @@ const fetchTelegram = async () => {
return null return null
} }
const bodyNews = await responseNews.text() 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() let infoNews = dataNews('.tgme_page_extra').text()
infoNews = infoNews.replace(' members', '').replace(' ', '').replace(' ', '') infoNews = infoNews.replace(' members', '').replace(' ', '').replace(' ', '')
@ -46,5 +46,3 @@ const fetchTelegram = async () => {
return { community: membersCommunity, news: membersNews } return { community: membersCommunity, news: membersNews }
} }
module.exports = fetchTelegram

View File

@ -1,7 +1,7 @@
const fetch = require('node-fetch') import fetch from 'node-fetch'
const { log, logError } = require('../utils') 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 url = 'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=oceanprotocol'
const start = Date.now() const start = Date.now()
const response = await fetch(url) const response = await fetch(url)
@ -22,5 +22,3 @@ const fetchTwitter = async () => {
return { followers } return { followers }
} }
module.exports = fetchTwitter