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

52 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-06-16 11:12:27 +02:00
import fetch from 'node-fetch'
import { load } from 'cheerio'
import { log, logError } from '../utils'
2019-05-14 21:57:08 +02:00
2020-06-16 11:12:27 +02:00
export default async function fetchTelegram() {
2020-06-16 12:32:47 +02:00
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(' ', '')
2020-06-16 14:43:59 +02:00
const community = parseInt(infoCommunity)
2020-06-16 12:32:47 +02:00
log(
2020-06-16 15:16:31 +02:00
'✓ Telegram. ' +
2020-06-16 14:43:59 +02:00
`Total: ${community} oceanprotocol_community members. ` +
2020-06-16 12:32:47 +02:00
`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(' ', '')
2020-06-16 14:43:59 +02:00
const news = parseInt(infoNews)
2020-06-16 12:32:47 +02:00
log(
2020-06-16 15:16:31 +02:00
'✓ Telegram. ' +
2020-06-16 14:43:59 +02:00
`Total: ${news} oceanprotocol members. ` +
2020-06-16 12:32:47 +02:00
`Elapsed: ${new Date() - start}ms`
)
2020-06-16 14:43:59 +02:00
return { community, news }
2019-05-14 21:57:08 +02:00
}