1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-11-25 11:28:45 +01:00
community-numbers/api/networks/discord.js

27 lines
746 B
JavaScript
Raw Normal View History

2020-06-16 16:14:31 +02:00
import { load } from 'cheerio'
import { log } from '../utils'
2020-06-16 16:14:31 +02:00
import fetch from 'node-fetch'
2020-06-16 15:16:31 +02:00
export default async function fetchDiscord() {
2020-06-16 12:32:47 +02:00
const url = 'https://discord.com/invite/TnXjkR5'
const start = Date.now()
2020-06-11 15:32:17 +02:00
2020-06-16 16:14:31 +02:00
const response = await fetch(url)
const body = await response.text()
const data = await load(body, { normalizeWhitespace: true })
2020-06-16 16:14:31 +02:00
// extract members count from meta description
const metaDescription = data('meta[name="description"]').attr('content')
const regex = /\d+/ // one or more digits
const number = metaDescription.match(regex)
const members = parseInt(number[0])
2020-06-16 12:32:47 +02:00
log(
2020-06-16 15:16:31 +02:00
'✓ Discord. ' +
2020-06-16 12:32:47 +02:00
`Total: ${members} members. ` +
`Elapsed: ${new Date() - start}ms`
)
2020-06-11 15:32:17 +02:00
2020-06-16 15:35:28 +02:00
return { members }
2020-06-11 15:32:17 +02:00
}