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:
commit
d923f64c4f
@ -35,6 +35,10 @@ Endpoint: [`https://oceanprotocol-community.now.sh`](https://oceanprotocol-commu
|
||||
},
|
||||
"twitter": {
|
||||
"followers": 1000
|
||||
},
|
||||
"telegram": {
|
||||
"community": 1000,
|
||||
"news": 1000
|
||||
}
|
||||
}
|
||||
```
|
||||
|
11
index.js
11
index.js
@ -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
50
networks/telegram.js
Normal 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
|
@ -9,6 +9,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"chalk": "2.4.2",
|
||||
"cheerio": "^1.0.0-rc.3",
|
||||
"ms": "^2.1.1",
|
||||
"node-fetch": "2.6.0"
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user