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

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-06-16 14:43:59 +02:00
import chrome from 'chrome-aws-lambda'
import { log } from '../utils'
2020-06-11 15:32:17 +02:00
2020-06-16 15:16:31 +02:00
const isDev = process.env.VERCEL_URL === undefined
const puppeteer = isDev ? require('puppeteer') : require('puppeteer-core')
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 15:16:31 +02:00
const config = {
ignoreHTTPSErrors: true,
...(isDev
? { headless: true }
: {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless
})
}
const browser = await puppeteer.launch(config)
2020-06-16 12:32:47 +02:00
const page = await browser.newPage()
await page.goto(url)
2020-06-16 12:32:47 +02:00
const members = await page.evaluate(() => {
// get the activity count element
const membersElement = document.querySelector(
'[class*="activityCount"] > div:last-child span'
2020-06-11 15:32:17 +02:00
)
2020-06-16 12:32:47 +02:00
const membersElementText = membersElement.innerText
const number = membersElementText.replace(' Members', '')
2020-06-16 15:35:28 +02:00
return parseInt(number)
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
}