mirror of
https://github.com/oceanprotocol/community-numbers.git
synced 2025-02-14 21:10:37 +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": {
|
"twitter": {
|
||||||
"followers": 1000
|
"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 fetchBounties = require('./networks/bounties')
|
||||||
const fetchMedium = require('./networks/medium')
|
const fetchMedium = require('./networks/medium')
|
||||||
const fetchTwitter = require('./networks/twitter')
|
const fetchTwitter = require('./networks/twitter')
|
||||||
|
const fetchTelegram = require('./networks/telegram')
|
||||||
|
|
||||||
let cacheGithub = null
|
let cacheGithub = null
|
||||||
let cacheBounties = null
|
let cacheBounties = null
|
||||||
let cacheMedium = null
|
let cacheMedium = null
|
||||||
let cacheTwitter = null
|
let cacheTwitter = null
|
||||||
|
let cacheTelegram = null
|
||||||
|
|
||||||
//
|
//
|
||||||
// Create the response
|
// Create the response
|
||||||
@ -19,6 +21,7 @@ module.exports = async (req, res) => {
|
|||||||
res.setHeader('Access-Control-Allow-Methods', 'GET')
|
res.setHeader('Access-Control-Allow-Methods', 'GET')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
/* eslint-disable require-atomic-updates */
|
||||||
if (!cacheGithub || Date.now() - cacheGithub.lastUpdate > ms('5m')) {
|
if (!cacheGithub || Date.now() - cacheGithub.lastUpdate > ms('5m')) {
|
||||||
cacheGithub = await fetchGitHubRepos()
|
cacheGithub = await fetchGitHubRepos()
|
||||||
}
|
}
|
||||||
@ -34,6 +37,11 @@ module.exports = async (req, res) => {
|
|||||||
if (!cacheTwitter || Date.now() - cacheTwitter.lastUpdate > ms('5m')) {
|
if (!cacheTwitter || Date.now() - cacheTwitter.lastUpdate > ms('5m')) {
|
||||||
cacheTwitter = await fetchTwitter()
|
cacheTwitter = await fetchTwitter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!cacheTelegram || Date.now() - cacheTelegram.lastUpdate > ms('5m')) {
|
||||||
|
cacheTelegram = await fetchTelegram()
|
||||||
|
}
|
||||||
|
/* eslint-enable require-atomic-updates */
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(error.message)
|
logError(error.message)
|
||||||
}
|
}
|
||||||
@ -42,6 +50,7 @@ module.exports = async (req, res) => {
|
|||||||
github: cacheGithub,
|
github: cacheGithub,
|
||||||
bounties: cacheBounties,
|
bounties: cacheBounties,
|
||||||
medium: cacheMedium,
|
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": {
|
"dependencies": {
|
||||||
"chalk": "2.4.2",
|
"chalk": "2.4.2",
|
||||||
|
"cheerio": "^1.0.0-rc.3",
|
||||||
"ms": "^2.1.1",
|
"ms": "^2.1.1",
|
||||||
"node-fetch": "2.6.0"
|
"node-fetch": "2.6.0"
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user