1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-11-21 17:27:01 +01:00

fixes & formatting

This commit is contained in:
Matthias Kretschmann 2020-06-16 12:32:47 +02:00
parent a9bf257d71
commit bf53963fe0
Signed by: m
GPG Key ID: 606EEEF3C479A91F
17 changed files with 236 additions and 221 deletions

View File

@ -1,13 +0,0 @@
# EditorConfig is awesome: http://EditorConfig.org
[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
[*.json]
indent_size = 2

View File

@ -1,3 +1,3 @@
{ {
"extends": "oceanprotocol" "extends": ["oceanprotocol", "prettier"]
} }

View File

@ -1,8 +1,8 @@
version: 2 version: 2
updates: updates:
- package-ecosystem: npm - package-ecosystem: npm
directory: "/" directory: '/'
schedule: schedule:
interval: daily interval: daily
time: '03:00' time: '03:00'
timezone: Europe/Berlin timezone: Europe/Berlin

6
.prettierrc Normal file
View File

@ -0,0 +1,6 @@
{
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"tabWidth": 2
}

View File

@ -2,4 +2,4 @@ language: node_js
node_js: node node_js: node
notifications: notifications:
email: false email: false

View File

@ -21,27 +21,27 @@ Endpoint: [`https://oceanprotocol-community.now.sh`](https://oceanprotocol-commu
```json ```json
{ {
"github": { "github": {
"stars": 1000, "stars": 1000,
"repos": 1000 "repos": 1000
}, },
"medium": { "medium": {
"followers": 1000 "followers": 1000
}, },
"bounties": { "bounties": {
"open": 1000, "open": 1000,
"total": 1000 "total": 1000
}, },
"twitter": { "twitter": {
"followers": 1000 "followers": 1000
}, },
"telegram": { "telegram": {
"community": 1000, "community": 1000,
"news": 1000 "news": 1000
}, },
"discord": { "discord": {
"members": 1000 "members": 1000
} }
} }
``` ```

View File

@ -11,54 +11,54 @@ import fetchDiscord from './networks/discord'
// Create the response // Create the response
// //
export default async (req, res) => { export default async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*') res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET') res.setHeader('Access-Control-Allow-Methods', 'GET')
let response = {} let response = {}
try { try {
const github = await fetchGitHubRepos() const github = await fetchGitHubRepos()
response = { ...response, github } response = { ...response, github }
} catch (error) { } catch (error) {
logError(error.message) logError(error.message)
} }
try { try {
const bounties = await fetchBounties() const bounties = await fetchBounties()
response = { ...response, bounties } response = { ...response, bounties }
} catch (error) { } catch (error) {
logError(error.message) logError(error.message)
} }
try { try {
const medium = await fetchMedium() const medium = await fetchMedium()
response = { ...response, medium } response = { ...response, medium }
} catch (error) { } catch (error) {
logError(error.message) logError(error.message)
} }
try { try {
const twitter = await fetchTwitter() const twitter = await fetchTwitter()
response = { ...response, twitter } response = { ...response, twitter }
} catch (error) { } catch (error) {
logError(error.message) logError(error.message)
} }
try { try {
const telegram = await fetchTelegram() const telegram = await fetchTelegram()
response = { ...response, telegram } response = { ...response, telegram }
} catch (error) { } catch (error) {
logError(error.message) logError(error.message)
} }
try { try {
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 // fake fallback response cause puppeteer fails a lot
response = { ...response, discord: { members: '240' } } response = { ...response, discord: { members: '240' } }
logError(error.message) logError(error.message)
} }
res.json(response) res.json(response)
} }

View File

@ -2,28 +2,30 @@ import fetch from 'node-fetch'
import { log, logError } from '../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'
)
if (response.status !== 200) { if (response.status !== 200) {
logError(`Non-200 response code from Gitcoin: ${response.status}`) logError(`Non-200 response code from Gitcoin: ${response.status}`)
return null return null
} }
const total = await response.json() const total = await response.json()
const open = total.filter((item) => item.is_open === true) const open = total.filter((item) => item.is_open === true)
return { total: total.length, open: open.length } return { total: total.length, open: open.length }
} }
export default async function fetchBounties() { export default async function fetchBounties() {
const start = Date.now() const start = Date.now()
const { total, open } = await getGitcoin() const { total, open } = await getGitcoin()
log( log(
'Re-fetched bounties. ' + 'Re-fetched bounties. ' +
`Total: ${total} bounties. Open: ${open} bounties. ` + `Total: ${total} bounties. Open: ${open} bounties. ` +
`Elapsed: ${new Date() - start}ms` `Elapsed: ${new Date() - start}ms`
) )
return { open, total } return { open, total }
} }

View File

@ -2,26 +2,28 @@ import puppeteer from 'puppeteer'
import { log } from '../utils' import { log } from '../utils'
export default async function fetchDiscord() { export default async function fetchDiscord() {
const url = 'https://discord.com/invite/TnXjkR5' const url = 'https://discord.com/invite/TnXjkR5'
const start = Date.now() const start = Date.now()
const browser = await puppeteer.launch({ headless: true }) const browser = await puppeteer.launch({ headless: true })
const page = await browser.newPage() const page = await browser.newPage()
await page.goto(url) await page.goto(url)
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(
const membersElementText = membersElement.innerText '[class*="activityCount"] > div:last-child span'
const number = membersElementText.replace(' Members', '')
return number
})
log(
'Re-fetched Discord. ' +
`Total: ${members} members. ` +
`Elapsed: ${new Date() - start}ms`
) )
const membersElementText = membersElement.innerText
const number = membersElementText.replace(' Members', '')
return number
})
return { members: Number(members) } log(
'Re-fetched Discord. ' +
`Total: ${members} members. ` +
`Elapsed: ${new Date() - start}ms`
)
return { members: Number(members) }
} }

View File

@ -3,45 +3,46 @@ import { log, logError, arrSum } from '../utils'
// Request options for all fetch calls // Request options for all fetch calls
const options = { const options = {
headers: { headers: {
// For getting topics, see note on https://developer.github.com/v3/search/ // For getting topics, see note on https://developer.github.com/v3/search/
// Accept: 'application/vnd.github.mercy-preview+json' // Accept: 'application/vnd.github.mercy-preview+json'
Accept: 'application/vnd.github.preview' Accept: 'application/vnd.github.preview'
} }
} }
// //
// Fetch all public GitHub repos // Fetch all public GitHub repos
// //
export default async function fetchGitHubRepos() { export default async function fetchGitHubRepos() {
const url = 'https://api.github.com/orgs/oceanprotocol/repos?type=public&per_page=200' const url =
const start = Date.now() 'https://api.github.com/orgs/oceanprotocol/repos?type=public&per_page=200'
const response = await fetch(url, options) const start = Date.now()
const response = await fetch(url, options)
if (response.status !== 200) { if (response.status !== 200) {
logError(`Non-200 response code from GitHub: ${response.status}`) logError(`Non-200 response code from GitHub: ${response.status}`)
return null return null
}
const json = await response.json()
const numbers = []
json.map((item) => {
if (item.stargazers_count) {
return numbers.push(item.stargazers_count)
} }
return null
})
const json = await response.json() const stars = arrSum(numbers)
const repositories = json.length
const numbers = [] log(
'Re-fetched GitHub. ' +
`Total: ${repositories} public projects with a total of ${stars} stargazers. ` +
`Elapsed: ${new Date() - start}ms`
)
json.map(item => { return { stars, repositories }
if (item.stargazers_count) {
return numbers.push(item.stargazers_count)
}
return null
})
const stars = arrSum(numbers)
const repositories = json.length
log(
'Re-fetched GitHub. ' +
`Total: ${repositories} public projects with a total of ${stars} stargazers. ` +
`Elapsed: ${new Date() - start}ms`
)
return { stars, repositories }
} }

View File

@ -2,26 +2,26 @@ import fetch from 'node-fetch'
import { log, logError } from '../utils' import { log, logError } from '../utils'
export default async function fetchMedium() { 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)
if (response.status !== 200) { if (response.status !== 200) {
logError(`Non-200 response code from Medium: ${response.status}`) logError(`Non-200 response code from Medium: ${response.status}`)
return null return null
} }
const responseText = await response.text() const responseText = await response.text()
const json = await JSON.parse(responseText.replace('])}while(1);</x>', '')) const json = await JSON.parse(responseText.replace('])}while(1);</x>', ''))
const { collection } = json.payload const { collection } = json.payload
const followers = collection.metadata.followerCount const followers = collection.metadata.followerCount
log( log(
'Re-fetched Medium. ' + 'Re-fetched Medium. ' +
`Total: ${followers} followers. ` + `Total: ${followers} followers. ` +
`Elapsed: ${new Date() - start}ms` `Elapsed: ${new Date() - start}ms`
) )
return { followers } return { followers }
} }

View File

@ -3,46 +3,49 @@ import { load } from 'cheerio'
import { log, logError } from '../utils' import { log, logError } from '../utils'
export default async function fetchTelegram() { 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)
if (responseCommunity.status !== 200) { if (responseCommunity.status !== 200) {
logError(`Non-200 response code from Telegram: ${responseCommunity.status}`) logError(`Non-200 response code from Telegram: ${responseCommunity.status}`)
return null return null
} }
const bodyCommunity = await responseCommunity.text() const bodyCommunity = await responseCommunity.text()
const dataCommunity = await 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
const membersCommunity = parseInt(infoCommunity) .replace(' members', '')
.replace(' ', '')
.replace(' ', '')
const membersCommunity = parseInt(infoCommunity)
log( log(
'Re-fetched Telegram. ' + 'Re-fetched Telegram. ' +
`Total: ${membersCommunity} oceanprotocol_community members. ` + `Total: ${membersCommunity} oceanprotocol_community members. ` +
`Elapsed: ${new Date() - start}ms` `Elapsed: ${new Date() - start}ms`
) )
const urlNews = 'https://t.me/oceanprotocol/?pagehidden=false' const urlNews = 'https://t.me/oceanprotocol/?pagehidden=false'
const responseNews = await fetch(urlNews) const responseNews = await fetch(urlNews)
if (responseNews.status !== 200) { if (responseNews.status !== 200) {
logError(`Non-200 response code from Telegram: ${responseNews.status}`) logError(`Non-200 response code from Telegram: ${responseNews.status}`)
return null return null
} }
const bodyNews = await responseNews.text() const bodyNews = await responseNews.text()
const dataNews = await 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(' ', '')
const membersNews = parseInt(infoNews) const membersNews = parseInt(infoNews)
log( log(
'Re-fetched Telegram. ' + 'Re-fetched Telegram. ' +
`Total: ${membersCommunity} oceanprotocol members. ` + `Total: ${membersCommunity} oceanprotocol members. ` +
`Elapsed: ${new Date() - start}ms` `Elapsed: ${new Date() - start}ms`
) )
return { community: membersCommunity, news: membersNews } return { community: membersCommunity, news: membersNews }
} }

View File

@ -2,23 +2,24 @@ import fetch from 'node-fetch'
import { log, logError } from '../utils' import { log, logError } from '../utils'
export default async function fetchTwitter() { export default async function fetchTwitter() {
const url = 'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=oceanprotocol' const url =
const start = Date.now() 'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=oceanprotocol'
const response = await fetch(url) const start = Date.now()
const response = await fetch(url)
if (response.status !== 200) { if (response.status !== 200) {
logError(`Non-200 response code from Twitter: ${response.status}`) logError(`Non-200 response code from Twitter: ${response.status}`)
return null return null
} }
const json = await response.json() const json = await response.json()
const followers = json[0].followers_count const followers = json[0].followers_count
log( log(
'Re-fetched Twitter. ' + 'Re-fetched Twitter. ' +
`Total: ${followers} followers. ` + `Total: ${followers} followers. ` +
`Elapsed: ${new Date() - start}ms` `Elapsed: ${new Date() - start}ms`
) )
return { followers } return { followers }
} }

View File

@ -1,9 +1,9 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
const chalk = require('chalk') const chalk = require('chalk')
const log = text => console.log(text) const log = (text) => console.log(text)
const logError = text => console.error(chalk.bold.red(text)) const logError = (text) => console.error(chalk.bold.red(text))
const arrSum = arr => arr.reduce((a, b) => a + b, 0) const arrSum = (arr) => arr.reduce((a, b) => a + b, 0)
module.exports = { log, logError, arrSum } module.exports = { log, logError, arrSum }

15
package-lock.json generated
View File

@ -757,6 +757,15 @@
} }
} }
}, },
"eslint-config-prettier": {
"version": "6.11.0",
"resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-6.11.0.tgz",
"integrity": "sha512-oB8cpLWSAjOVFEJhhyMZh6NOEOtBVziaqdDQ86+qhDHFbZXoRTM7pNSvFRfW/W/L/LrQ38C99J5CGuRBBzBsdA==",
"dev": true,
"requires": {
"get-stdin": "^6.0.0"
}
},
"eslint-config-standard": { "eslint-config-standard": {
"version": "14.0.0", "version": "14.0.0",
"resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-14.0.0.tgz", "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-14.0.0.tgz",
@ -1186,6 +1195,12 @@
"integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=",
"dev": true "dev": true
}, },
"get-stdin": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-6.0.0.tgz",
"integrity": "sha512-jp4tHawyV7+fkkSKyvjuLZswblUtz+SQKzSWnBbii16BuZksJlU1wuBYXY75r+duh/llF1ur6oNwi+2ZzjKZ7g==",
"dev": true
},
"get-stream": { "get-stream": {
"version": "5.1.0", "version": "5.1.0",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.1.0.tgz", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.1.0.tgz",

View File

@ -9,13 +9,13 @@
"dependencies": { "dependencies": {
"chalk": "^4.1.0", "chalk": "^4.1.0",
"cheerio": "^1.0.0-rc.3", "cheerio": "^1.0.0-rc.3",
"ms": "^2.1.1",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"puppeteer": "^4.0.0" "puppeteer": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^7.2.0", "eslint": "^7.2.0",
"eslint-config-oceanprotocol": "^1.5.0", "eslint-config-oceanprotocol": "^1.5.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-node": "^11.1.0" "eslint-plugin-node": "^11.1.0"
} }
} }

View File

@ -1,5 +1,3 @@
{ {
"routes": [ "routes": [{ "src": "/", "dest": "/api" }]
{ "src": "/", "dest": "/api" }
]
} }