journey/server/routers/api.js

88 lines
2.2 KiB
JavaScript

'use strict'
const format = require('easy-tz/cjs/format')
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]}`
}
function formatTime(date) {
date = date || new Date()
return format(null, 'YYYY-MM-DD HH:mm', date)
}
console.log(formatTime())
function formatLink(link) {}
function formatResult(result) {
return _.omit(result, 'Prices')
}
console.log(new Date('2017-02-24T17:16:00'.split('T').join(' ')).toLocaleString())
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=${encodeURIComponent(
formatTime(req.body.datetime),
)}`,
).then((response) => {
const result = _.get(xml2json.toJson(response.text, { object: true }), journeysPath)
result.forEach(formatResult)
res.json(result)
})
})
module.exports = router