1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-09-28 03:58:40 +02:00
community-numbers/api/networks/discord.js

28 lines
806 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')
2021-02-04 12:11:07 +01:00
const regex = /\d+[,]\d+/ // one or more digits
2020-06-16 16:14:31 +02:00
const number = metaDescription.match(regex)
2021-02-04 12:11:07 +01:00
const membersNumber = number[0].replace(/,/,'')
const members = parseInt(membersNumber)
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
}