1
0
mirror of https://github.com/oceanprotocol/community-numbers.git synced 2024-12-22 17:23:31 +01:00

prototype telegram member count

This commit is contained in:
Matthias Kretschmann 2019-05-14 21:57:08 +02:00
parent 4d0447b5f9
commit 69de880a4a
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 40 additions and 1 deletions

View File

@ -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
@ -34,6 +36,10 @@ 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()
}
} catch (error) { } catch (error) {
logError(error.message) logError(error.message)
} }
@ -42,6 +48,7 @@ module.exports = async (req, res) => {
github: cacheGithub, github: cacheGithub,
bounties: cacheBounties, bounties: cacheBounties,
medium: cacheMedium, medium: cacheMedium,
twitter: cacheTwitter twitter: cacheTwitter,
telegram: cacheTelegram
})) }))
} }

31
networks/telegram.js Normal file
View File

@ -0,0 +1,31 @@
const fetch = require('node-fetch')
const cheerio = require('cheerio')
const { log, logError } = require('../util/logger')
const fetchTelegram = async () => {
const url = 'https://t.me/OceanProtocolCommunity/?pagehidden=false'
const start = Date.now()
const response = await fetch(url)
if (response.status !== 200) {
logError(`Non-200 response code from Twitter: ${response.status}`)
return null
}
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)
log(
`Re-built telegram cache. ` +
`Total: ${members} members. ` +
`Elapsed: ${new Date() - start}ms`
)
return { members }
}
module.exports = fetchTelegram

View File

@ -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"
}, },