2017-05-29 14:20:58 +02:00
|
|
|
const fetch = require('node-fetch')
|
|
|
|
const ms = require('ms')
|
|
|
|
const chalk = require('chalk')
|
|
|
|
|
|
|
|
let data = []
|
|
|
|
const url = 'https://api.meetup.com/BigchainDB-IPDB-Meetup/events'
|
|
|
|
|
|
|
|
const log = text => console.log(text)
|
|
|
|
const logError = text => console.log(chalk.bold.red(text))
|
|
|
|
|
|
|
|
// Response handling for all fetch calls
|
|
|
|
const handleResponse = res => {
|
|
|
|
if (res.status !== 200) {
|
|
|
|
return logError('Non-200 response code from Meetup: ' + res.status)
|
|
|
|
}
|
|
|
|
return res.json()
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Fetch all upcoming meetups
|
|
|
|
//
|
|
|
|
const fetchMeetups = () => {
|
|
|
|
const start = Date.now()
|
|
|
|
|
|
|
|
fetch(url)
|
|
|
|
.then(res => {
|
|
|
|
return handleResponse(res)
|
|
|
|
})
|
|
|
|
.then(data_ => {
|
|
|
|
if (!data_) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data = data_
|
|
|
|
|
2018-09-04 13:34:49 +02:00
|
|
|
log('Re-built meetups cache. ' +
|
2017-05-29 14:20:58 +02:00
|
|
|
`Total: ${data_.length} public meetups. ` +
|
|
|
|
`Elapsed: ${(new Date() - start)}ms`)
|
|
|
|
})
|
2018-09-04 13:34:49 +02:00
|
|
|
.catch(error => {
|
|
|
|
logError('Error parsing response from Meetup: ' + error.stack)
|
2017-05-29 14:20:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Let's roll, and roll again every X ms
|
|
|
|
//
|
|
|
|
fetchMeetups()
|
|
|
|
setInterval(fetchMeetups, ms('15m'))
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create the response
|
|
|
|
//
|
|
|
|
module.exports = async (req, res) => {
|
2018-09-04 13:34:49 +02:00
|
|
|
await res.setHeader('Access-Control-Allow-Origin', '*')
|
|
|
|
await res.setHeader('Access-Control-Allow-Methods', 'GET')
|
2017-05-29 14:20:58 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|