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

49 lines
1.7 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() {
2019-06-24 17:25:41 +02:00
const urlCommunity = 'https://t.me/oceanprotocol_community/?pagehidden=false'
2019-05-14 21:57:08 +02:00
const start = Date.now()
2019-06-24 17:25:41 +02:00
const responseCommunity = await fetch(urlCommunity)
2019-05-14 21:57:08 +02:00
2019-06-24 17:25:41 +02:00
if (responseCommunity.status !== 200) {
logError(`Non-200 response code from Telegram: ${responseCommunity.status}`)
2019-05-14 21:57:08 +02:00
return null
}
2019-06-24 17:25:41 +02:00
const bodyCommunity = await responseCommunity.text()
2020-06-16 11:12:27 +02:00
const dataCommunity = await load(bodyCommunity, { normalizeWhitespace: true })
2019-05-14 21:57:08 +02:00
2019-06-24 17:25:41 +02:00
let infoCommunity = dataCommunity('.tgme_page_extra').text()
infoCommunity = infoCommunity.replace(' members', '').replace(' ', '').replace(' ', '')
const membersCommunity = parseInt(infoCommunity)
2019-05-14 21:57:08 +02:00
2019-06-24 17:25:41 +02:00
log(
'Re-fetched Telegram. ' +
2019-06-24 17:25:41 +02:00
`Total: ${membersCommunity} 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()
2020-06-16 11:12:27 +02:00
const dataNews = await load(bodyNews, { normalizeWhitespace: true })
2019-06-24 17:25:41 +02:00
let infoNews = dataNews('.tgme_page_extra').text()
infoNews = infoNews.replace(' members', '').replace(' ', '').replace(' ', '')
const membersNews = parseInt(infoNews)
2019-05-14 21:57:08 +02:00
log(
'Re-fetched Telegram. ' +
`Total: ${membersCommunity} oceanprotocol members. ` +
2019-05-14 21:57:08 +02:00
`Elapsed: ${new Date() - start}ms`
)
2019-06-24 17:25:41 +02:00
return { community: membersCommunity, news: membersNews }
2019-05-14 21:57:08 +02:00
}