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

52 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-11-09 09:36:02 +01:00
import axios from 'axios'
2020-06-16 11:12:27 +02:00
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()
2021-11-09 09:36:02 +01:00
const responseCommunity = await axios.get(urlCommunity)
2020-06-16 12:32:47 +02:00
if (responseCommunity.status !== 200) {
logError(`Non-200 response code from Telegram: ${responseCommunity.status}`)
return null
}
2021-11-09 09:36:02 +01:00
const bodyCommunity = await responseCommunity.data
2020-06-16 12:32:47 +02:00
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'
2021-11-09 09:36:02 +01:00
const responseNews = await axios.get(urlNews)
2020-06-16 12:32:47 +02:00
if (responseNews.status !== 200) {
logError(`Non-200 response code from Telegram: ${responseNews.status}`)
return null
}
2021-11-09 09:36:02 +01:00
const bodyNews = await responseNews.data
2020-06-16 12:32:47 +02:00
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
}