mirror of
https://github.com/oceanprotocol/community-numbers.git
synced 2024-12-22 17:23:31 +01:00
return telegram numbers
This commit is contained in:
parent
69de880a4a
commit
76b9c5aece
@ -35,6 +35,10 @@ Endpoint: [`https://oceanprotocol-community.now.sh`](https://oceanprotocol-commu
|
||||
},
|
||||
"twitter": {
|
||||
"followers": 1000
|
||||
},
|
||||
"telegram": {
|
||||
"community": 1000,
|
||||
"news": 1000
|
||||
}
|
||||
}
|
||||
```
|
||||
|
2
index.js
2
index.js
@ -21,6 +21,7 @@ module.exports = async (req, res) => {
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET')
|
||||
|
||||
try {
|
||||
/* eslint-disable require-atomic-updates */
|
||||
if (!cacheGithub || Date.now() - cacheGithub.lastUpdate > ms('5m')) {
|
||||
cacheGithub = await fetchGitHubRepos()
|
||||
}
|
||||
@ -40,6 +41,7 @@ module.exports = async (req, res) => {
|
||||
if (!cacheTelegram || Date.now() - cacheTelegram.lastUpdate > ms('5m')) {
|
||||
cacheTelegram = await fetchTelegram()
|
||||
}
|
||||
/* eslint-enable require-atomic-updates */
|
||||
} catch (error) {
|
||||
logError(error.message)
|
||||
}
|
||||
|
@ -1,31 +1,50 @@
|
||||
const fetch = require('node-fetch')
|
||||
const cheerio = require('cheerio')
|
||||
const { log, logError } = require('../util/logger')
|
||||
const { log, logError } = require('../utils')
|
||||
|
||||
const fetchTelegram = async () => {
|
||||
const url = 'https://t.me/OceanProtocolCommunity/?pagehidden=false'
|
||||
const urlCommunity = 'https://t.me/oceanprotocol_community/?pagehidden=false'
|
||||
const start = Date.now()
|
||||
const response = await fetch(url)
|
||||
const responseCommunity = await fetch(urlCommunity)
|
||||
|
||||
if (response.status !== 200) {
|
||||
logError(`Non-200 response code from Twitter: ${response.status}`)
|
||||
if (responseCommunity.status !== 200) {
|
||||
logError(`Non-200 response code from Telegram: ${responseCommunity.status}`)
|
||||
return null
|
||||
}
|
||||
const bodyCommunity = await responseCommunity.text()
|
||||
const dataCommunity = await cheerio.load(bodyCommunity, { normalizeWhitespace: true })
|
||||
|
||||
const body = await response.text()
|
||||
const data = await cheerio.load(body, { normalizeWhitespace: true })
|
||||
|
||||
let info = data('.tgme_page_extra').text()
|
||||
info = info.replace(' members', '').replace(' ', '').replace(' ', '')
|
||||
const members = parseInt(info)
|
||||
let infoCommunity = dataCommunity('.tgme_page_extra').text()
|
||||
infoCommunity = infoCommunity.replace(' members', '').replace(' ', '').replace(' ', '')
|
||||
const membersCommunity = parseInt(infoCommunity)
|
||||
|
||||
log(
|
||||
`Re-built telegram cache. ` +
|
||||
`Total: ${members} members. ` +
|
||||
`Total: ${membersCommunity} oceanprotocol_community members. ` +
|
||||
`Elapsed: ${new Date() - start}ms`
|
||||
)
|
||||
|
||||
return { members }
|
||||
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)
|
||||
|
||||
log(
|
||||
`Re-built telegram cache. ` +
|
||||
`Total: ${membersCommunity} oceanprotocol_community members. ` +
|
||||
`Elapsed: ${new Date() - start}ms`
|
||||
)
|
||||
|
||||
return { community: membersCommunity, news: membersNews }
|
||||
}
|
||||
|
||||
module.exports = fetchTelegram
|
||||
|
Loading…
Reference in New Issue
Block a user