1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-12-22 17:23:31 +01:00
community-numbers/api/networks/telegram.js

52 lines
1.6 KiB
JavaScript

import fetch from 'node-fetch'
import { load } from 'cheerio'
import { log, logError } from '../utils'
export default async function fetchTelegram() {
const urlCommunity = 'https://t.me/oceanprotocol_community/?pagehidden=false'
const start = Date.now()
const responseCommunity = await fetch(urlCommunity)
if (responseCommunity.status !== 200) {
logError(`Non-200 response code from Telegram: ${responseCommunity.status}`)
return null
}
const bodyCommunity = await responseCommunity.text()
const dataCommunity = await load(bodyCommunity, { normalizeWhitespace: true })
let infoCommunity = dataCommunity('.tgme_page_extra').text()
infoCommunity = infoCommunity
.replace(' members', '')
.replace(' ', '')
.replace(' ', '')
const community = parseInt(infoCommunity)
log(
'✓ Telegram. ' +
`Total: ${community} oceanprotocol_community members. ` +
`Elapsed: ${new Date() - start}ms`
)
const urlNews = 'https://t.me/oceanprotocol/?pagehidden=false'
const responseNews = await fetch(urlNews)
if (responseNews.status !== 200) {
logError(`Non-200 response code from Telegram: ${responseNews.status}`)
return null
}
const bodyNews = await responseNews.text()
const dataNews = await load(bodyNews, { normalizeWhitespace: true })
let infoNews = dataNews('.tgme_page_extra').text()
infoNews = infoNews.replace(' members', '').replace(' ', '').replace(' ', '')
const news = parseInt(infoNews)
log(
'✓ Telegram. ' +
`Total: ${news} oceanprotocol members. ` +
`Elapsed: ${new Date() - start}ms`
)
return { community, news }
}