1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-11-22 01:37:02 +01:00

return telegram numbers

This commit is contained in:
Matthias Kretschmann 2019-06-24 17:25:41 +02:00
parent 69de880a4a
commit 76b9c5aece
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 38 additions and 13 deletions

View File

@ -35,6 +35,10 @@ Endpoint: [`https://oceanprotocol-community.now.sh`](https://oceanprotocol-commu
},
"twitter": {
"followers": 1000
},
"telegram": {
"community": 1000,
"news": 1000
}
}
```

View File

@ -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)
}

View File

@ -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