journey/server/routers/api.js
2017-02-23 19:29:58 +01:00

68 lines
1.8 KiB
JavaScript

'use strict';
const request = require('superagent');
// import get from 'get-value';
const _ = require('lodash');
const xml2json = require('xml2json');
// const to = 'Lund Mellanvångsvägen';
// const from = 'Malmö Triangeln';
// request(`http://www.labs.skanetrafiken.se/v2.2/querypage.asp?inpPointFr=${encodeURIComponent(from)}&inpPointTo=${encodeURIComponent(to)}`).then((res) => {
// console.dir(xml2json.toJson(res.text, { object: true }), { colors: true, depth: null });
// });
const stations = {
home: {
id: '81016',
name: 'Lund Idrottsplatsen',
type: 'STOP_AREA',
},
office: {
name: 'Lund John Ericssons väg',
id: 81229,
type: 'STOP_AREA',
},
brother: {
id: '81831',
name: 'Lund Mellanvångsvägen',
type: 'STOP_AREA',
},
therapist: {
id: '80140',
name: 'Malmö Triangeln',
type: 'STOP_AREA',
},
};
const types = {
'STOP_AREA': 0,
}
const router = new (require('express')).Router();
function formatStation(json) {
return `${encodeURIComponent(json.name)}|${json.id}|${types[json.type]}`
}
const journeysPath = [ 'soap:Envelope', 'soap:Body', 'GetJourneyResponse', 'GetJourneyResult', 'Journeys', 'Journey']
// http://www.labs.skanetrafiken.se/v2.2/resultspage.asp?cmdaction=next&selPointFr=malm%F6%20C|80000|0&selPointTo=landskrona|82000|0&LastStart=2017-02-23%2016:38
router.post('/trip', (req, res, next) => {
request(`http://www.labs.skanetrafiken.se/v2.2/resultspage.asp?cmdaction=next&selPointFr=${formatStation(stations[req.body.from])}&selPointTo=${formatStation(stations[req.body.to])}&LastStart=2017-02-23%2016:38`).then((response) =>{
const result = _.get(xml2json.toJson(response.text, { object: true }), journeysPath);
result.forEach((obj) => {
delete obj.Prices
});
res.json(result);
});
});
module.exports = router;