Add API route to get Protospace class list

This commit is contained in:
Tanner Collin 2017-06-25 01:23:49 -06:00
parent 18bae1f3d4
commit 7ab2a42348
2 changed files with 26 additions and 2 deletions

View File

@ -18,8 +18,10 @@
"webpack-dev-server": "^2.4.5" "webpack-dev-server": "^2.4.5"
}, },
"dependencies": { "dependencies": {
"cheerio": "^1.0.0-rc.1",
"express": "^4.15.3", "express": "^4.15.3",
"react": "^15.6.1", "react": "^15.6.1",
"react-dom": "^15.6.1" "react-dom": "^15.6.1",
"request": "^2.81.0"
} }
} }

View File

@ -1,12 +1,34 @@
const request = require('request');
const cheerio = require('cheerio');
const express = require('express') const express = require('express')
const app = express() const app = express()
app.use('/', express.static('dist')) app.use('/', express.static('dist'))
app.get('/hello', function (req, res) { app.get('/hello', (req, res) => {
res.send('Hello World!') res.send('Hello World!')
}) })
app.get('/api/data/classes', (req, res) => {
request('https://my.protospace.ca/school', (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
let classes = [];
$('table.grid tbody tr').each((i, elem) => {
classes[i] = {
subject: $(elem).children().eq(0).text(),
date: $(elem).children().eq(2).text(),
available: $(elem).children().eq(6).text()
}
});
res.setHeader('Content-Type', 'application/json');
res.send(classes);
}
});
})
app.listen(3000, function () { app.listen(3000, function () {
console.log('Example app listening on port 3000!') console.log('Example app listening on port 3000!')
}) })