1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-11-25 11:28:45 +01:00
community-numbers/networks/telegram.js

51 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-05-14 21:57:08 +02:00
const fetch = require('node-fetch')
const cheerio = require('cheerio')
2019-06-24 17:25:41 +02:00
const { log, logError } = require('../utils')
2019-05-14 21:57:08 +02:00
const fetchTelegram = async () => {
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()
const dataCommunity = await cheerio.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-built telegram cache. ` +
`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()
const dataNews = await cheerio.load(bodyNews, { normalizeWhitespace: true })
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-built telegram cache. ` +
2019-06-24 17:25:41 +02:00
`Total: ${membersCommunity} oceanprotocol_community 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
}
module.exports = fetchTelegram