1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-11-22 09:47:05 +01:00
community-numbers/api/networks/discord.js
2021-02-04 13:15:41 +02:00

28 lines
856 B
JavaScript

import { load } from 'cheerio'
import { log } from '../utils'
import fetch from 'node-fetch'
export default async function fetchDiscord() {
const url = 'https://discord.com/invite/TnXjkR5'
const start = Date.now()
const response = await fetch(url)
const body = await response.text()
const data = await load(body, { normalizeWhitespace: true })
// extract members count from meta description
const metaDescription = data('meta[name="description"]').attr('content')
const regex = /\d+[,]\d+/ // one or more digits separated by comma
const number = metaDescription.match(regex)
const membersNumber = number[0].replace(/,/,'') // eliminate comma from number
const members = parseInt(membersNumber)
log(
'✓ Discord. ' +
`Total: ${members} members. ` +
`Elapsed: ${new Date() - start}ms`
)
return { members }
}