Skip to content

Commit

Permalink
add the api
Browse files Browse the repository at this point in the history
  • Loading branch information
pkage committed Oct 18, 2019
1 parent 9be2ab3 commit 0631c86
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# random files
.DS_Store
instance/cache.json
instance/secret.json

# Logs
logs
Expand Down
4 changes: 4 additions & 0 deletions instance/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"port": 3000,
"cachefile": "instance/cache.json"
}
6 changes: 3 additions & 3 deletions scrape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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(() => {
Expand Down
40 changes: 40 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -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}!`)))

0 comments on commit 0631c86

Please sign in to comment.