diff --git a/.gitignore b/.gitignore index e71c682..42994b1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # random files .DS_Store +instance/cache.json +instance/secret.json # Logs logs diff --git a/instance/config.json b/instance/config.json new file mode 100644 index 0000000..8c64750 --- /dev/null +++ b/instance/config.json @@ -0,0 +1,4 @@ +{ + "port": 3000, + "cachefile": "instance/cache.json" +} diff --git a/scrape.js b/scrape.js index 7ec92da..8aca568 100644 --- a/scrape.js +++ b/scrape.js @@ -2,7 +2,7 @@ const Nightmare = require('nightmare') const nightmare = Nightmare({ show: true }) const fs = require('fs') -const config = JSON.parse(fs.readFileSync('./instance/secret.json')) +const secrets = JSON.parse(fs.readFileSync('./instance/secret.json')) module.exports = () => { const nightmare = Nightmare({ show: false }) @@ -12,8 +12,8 @@ module.exports = () => { .goto('https://www.eusa.ed.ac.uk/organisation/memberlist/8868/?sort=groups') .click('.student-login-block') .wait('#login') - .type('#login', config.email) - .type('#password', config.password) + .type('#login', secrets.email) + .type('#password', secrets.password) .click('[value=" Login now "]') .wait('.member_list_group') .evaluate(() => { diff --git a/server.js b/server.js index e69de29..c6c4de0 100644 --- a/server.js +++ b/server.js @@ -0,0 +1,40 @@ +const express = require('express') +const scrape_members = require('./scrape.js') +const fs = require('fs') + +const config = JSON.parse(fs.readFileSync('./instance/config.json')) + +const app = express() +const cachefile = config.cachefile +const port = config.port + +const writeScrape = async () => { + const members = await scrape_members() + + const out = { + members: JSON.parse(members).members, + date: new Date().toISOString() + } + + fs.writeFileSync( + cachefile, + JSON.stringify(out) + ) + + return out +} + +const readScrape = () => JSON.parse(fs.readFileSync(cachefile)) + +app.get('/api/members', (req, res) => { + res.json(readScrape()) +}) + +app.get('/api/refresh', (req, res) => { + writeScrape() + .then(r => res.json(r)) +}) + +console.log('getting initial scrape...') +writeScrape() + .then(() => app.listen(port, () => console.log(`EUSA members api listening on port ${port}!`)))