2020-06-16 14:43:59 +02:00
|
|
|
import chrome from 'chrome-aws-lambda'
|
2020-06-16 10:48:36 +02:00
|
|
|
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')
|
|
|
|
|
2020-06-16 10:48:36 +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 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 10:48:36 +02:00
|
|
|
|
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
|
|
|
}
|