journey/server/routers/api.mjs
Linus Miller ed79c5b7a6 convert server files to esm
- update deps
- remove midwest-service-errors
2020-09-06 19:49:49 +02:00

84 lines
2.1 KiB
JavaScript

import format from 'easy-tz/cjs/format.js'
import request from 'superagent'
import express from 'express'
// import get from 'get-value';
import _ from 'lodash'
import xml2json from '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 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)
}
function formatLink(link) {}
function formatResult(result) {
return _.omit(result, 'Prices')
}
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)
})
})
export default router