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

Merge pull request #2 from oceanprotocol/feature/telegram

telegram member count
This commit is contained in:
Matthias Kretschmann 2019-06-24 18:02:33 +02:00 committed by GitHub
commit d923f64c4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 1 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

@ -5,11 +5,13 @@ const fetchGitHubRepos = require('./networks/github')
const fetchBounties = require('./networks/bounties')
const fetchMedium = require('./networks/medium')
const fetchTwitter = require('./networks/twitter')
const fetchTelegram = require('./networks/telegram')
let cacheGithub = null
let cacheBounties = null
let cacheMedium = null
let cacheTwitter = null
let cacheTelegram = null
//
// Create the response
@ -19,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()
}
@ -34,6 +37,11 @@ module.exports = async (req, res) => {
if (!cacheTwitter || Date.now() - cacheTwitter.lastUpdate > ms('5m')) {
cacheTwitter = await fetchTwitter()
}
if (!cacheTelegram || Date.now() - cacheTelegram.lastUpdate > ms('5m')) {
cacheTelegram = await fetchTelegram()
}
/* eslint-enable require-atomic-updates */
} catch (error) {
logError(error.message)
}
@ -42,6 +50,7 @@ module.exports = async (req, res) => {
github: cacheGithub,
bounties: cacheBounties,
medium: cacheMedium,
twitter: cacheTwitter
twitter: cacheTwitter,
telegram: cacheTelegram
}))
}

50
networks/telegram.js Normal file
View File

@ -0,0 +1,50 @@
const fetch = require('node-fetch')
const cheerio = require('cheerio')
const { log, logError } = require('../utils')
const fetchTelegram = async () => {
const urlCommunity = 'https://t.me/oceanprotocol_community/?pagehidden=false'
const start = Date.now()
const responseCommunity = await fetch(urlCommunity)
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 })
let infoCommunity = dataCommunity('.tgme_page_extra').text()
infoCommunity = infoCommunity.replace(' members', '').replace(' ', '').replace(' ', '')
const membersCommunity = parseInt(infoCommunity)
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)
log(
`Re-built telegram cache. ` +
`Total: ${membersCommunity} oceanprotocol_community members. ` +
`Elapsed: ${new Date() - start}ms`
)
return { community: membersCommunity, news: membersNews }
}
module.exports = fetchTelegram

View File

@ -9,6 +9,7 @@
},
"dependencies": {
"chalk": "2.4.2",
"cheerio": "^1.0.0-rc.3",
"ms": "^2.1.1",
"node-fetch": "2.6.0"
},