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(
|
2019-08-20 11:35:30 +02:00
|
|
|
'Re-built telegram cache. ' +
|
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()
|
|
|
|
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(
|
2019-08-20 11:35:30 +02:00
|
|
|
'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
|