adapt to Friendly Public Transport Format, part 1

This commit is contained in:
Jannis R 2017-04-25 01:57:09 +02:00
parent d505ee744e
commit 5abb2150fe
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 40 additions and 25 deletions

View file

@ -13,7 +13,8 @@
"engines" : {"node": ">=6"}, "engines" : {"node": ">=6"},
"dependencies": { "dependencies": {
"got": "^6.7.1", "got": "^6.7.1",
"moment-timezone": "^0.5.13" "moment-timezone": "^0.5.13",
"slugg": "^1.2.0"
}, },
"scripts": { "scripts": {
"test": "node -e \"require('.')\"", "test": "node -e \"require('.')\"",

View file

@ -1,6 +1,7 @@
'use strict' 'use strict'
const moment = require('moment-timezone') const moment = require('moment-timezone')
const slugg = require('slugg')
@ -22,9 +23,12 @@ const types = {P: 'poi', S: 'station', A: 'address'}
const location = (l) => { const location = (l) => {
const type = types[l.type] || 'unknown' const type = types[l.type] || 'unknown'
const result = { const result = {
type, name: l.name type,
, latitude: l.crd ? l.crd.y / 1000000 : null name: l.name,
, longitude: l.crd ? l.crd.x / 1000000 : null coordinates: l.crd ? {
latitude: l.crd.y / 1000000,
longitude: l.crd.x / 1000000
} : null
} }
if (type === 'poi' || type === 'station') result.id = parseInt(l.extId) if (type === 'poi' || type === 'station') result.id = parseInt(l.extId)
if ('pCls' in l) result.products = l.pCls if ('pCls' in l) result.products = l.pCls
@ -52,17 +56,23 @@ const remark = (r) => null // todo
const agency = (a) => a.name const operator = (a) => ({
type: 'operator',
id: slugg(a.name),
name: a.name
})
// s = stations, p = products, r = remarks, c = connection // s = stations, p = products, r = remarks, c = connection
const stop = (tz, s, p, r, c) => (st) => { const stop = (tz, s, p, r, c) => (st) => {
const result = {station: s[parseInt(st.locX)]} const result = {station: s[parseInt(st.locX)]}
if (st.aTimeR || st.aTimeS) result.arrival = if (st.aTimeR || st.aTimeS) {
new Date(dateTime(tz, c.date, st.aTimeR || st.aTimeS)) result.arrival = dateTime(tz, c.date, st.aTimeR || st.aTimeS)
if (st.dTimeR || st.dTimeS) result.departure = }
new Date(dateTime(tz, c.date, st.dTimeR || st.dTimeS)) if (st.dTimeR || st.dTimeS) {
result.departure = dateTime(tz, c.date, st.dTimeR || st.dTimeS)
}
return result return result
} }
@ -77,20 +87,20 @@ const applyRemark = (s, p, r, c) => (rm) => null
// s = stations, p = products, r = remarks, c = connection // s = stations, p = products, r = remarks, c = connection
const part = (tz, s, p, r, c) => (pt) => { const part = (tz, s, p, r, c) => (pt) => {
const result = { const result = {
from: Object.assign({}, s[parseInt(pt.dep.locX)]) origin: Object.assign({}, s[parseInt(pt.dep.locX)])
, to: Object.assign({}, s[parseInt(pt.arr.locX)]) , destination: Object.assign({}, s[parseInt(pt.arr.locX)])
, start: new Date(dateTime(tz, c.date, pt.dep.dTimeR || pt.dep.dTimeS)) , departure: dateTime(tz, c.date, pt.dep.dTimeR || pt.dep.dTimeS)
, end: new Date(dateTime(tz, c.date, pt.arr.aTimeR || pt.arr.aTimeS)) , arrival: dateTime(tz, c.date, pt.arr.aTimeR || pt.arr.aTimeS)
} }
if (pt.dep.dTimeR && pt.dep.dTimeS) result.delay = if (pt.dep.dTimeR && pt.dep.dTimeS) result.delay =
dateTime(tz, c.date, pt.dep.dTimeR) - dateTime(tz, c.date, pt.dep.dTimeS) dateTime(tz, c.date, pt.dep.dTimeR) - dateTime(tz, c.date, pt.dep.dTimeS)
if (pt.type === 'WALK') result.type = 'walking' if (pt.type === 'WALK') result.mode = 'walking'
else if (pt.type === 'JNY') { else if (pt.type === 'JNY') {
result.product = p[parseInt(pt.jny.prodX)] result.product = p[parseInt(pt.jny.prodX)]
result.direction = pt.jny.dirTxt // todo: parse this result.direction = pt.jny.dirTxt // todo: parse this
if (pt.dep.dPlatfS) result.from.platform = pt.dep.dPlatfS if (pt.dep.dPlatfS) result.departurePlatform = pt.dep.dPlatfS
if (pt.arr.aPlatfS) result.to.platform = pt.arr.aPlatfS if (pt.arr.aPlatfS) result.arrivalPlatform = pt.arr.aPlatfS
if (pt.jny.stopL) result.passed = pt.jny.stopL.map(stop(tz, s, p, r, c)) if (pt.jny.stopL) result.passed = pt.jny.stopL.map(stop(tz, s, p, r, c))
if (Array.isArray(pt.jny.remL)) if (Array.isArray(pt.jny.remL))
@ -101,7 +111,7 @@ const part = (tz, s, p, r, c) => (pt) => {
.filter((a) => a.stopL[0].locX === pt.dep.locX) .filter((a) => a.stopL[0].locX === pt.dep.locX)
.map((a) => ({ .map((a) => ({
product: p[parseInt(a.prodX)], product: p[parseInt(a.prodX)],
when: new Date(dateTime(tz, c.date, a.stopL[0].dTimeS)) when: dateTime(tz, c.date, a.stopL[0].dTimeS)
})) }))
} }
return result return result
@ -131,7 +141,7 @@ const route = (tz, s, p, r) => (c) => {
const departure = (tz, s, p, r) => (d) => { const departure = (tz, s, p, r) => (d) => {
const result = { const result = {
station: s[parseInt(d.stbStop.locX)] station: s[parseInt(d.stbStop.locX)]
, when: new Date(dateTime(tz, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS)) , when: dateTime(tz, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS)
, direction: d.dirTxt , direction: d.dirTxt
, product: p[parseInt(d.prodX)] , product: p[parseInt(d.prodX)]
, remarks: d.remL ? d.remL.map((rm) => r[parseInt(rm.remX)]) : null , remarks: d.remL ? d.remL.map((rm) => r[parseInt(rm.remX)]) : null
@ -165,14 +175,18 @@ const movement = (tz, l, p, r) => (m) => {
const result = { const result = {
direction: m.dirTxt direction: m.dirTxt
, product: p[m.prodX] , product: p[m.prodX]
, latitude: m.pos ? m.pos.y / 1000000 : null , coordinates: m.pos ? {
, longitude: m.pos ? m.pos.x / 1000000 : null latitude: m.pos.y / 1000000,
longitude: m.pos.x / 1000000
} : null
, nextStops: m.stopL.map((s) => ({ , nextStops: m.stopL.map((s) => ({
station: l[s.locX] station: l[s.locX]
, departure: s.dTimeR || s.dTimeS ? , departure: s.dTimeR || s.dTimeS
new Date(dateTime(tz, m.date, s.dTimeR || s.dTimeS)) : null ? dateTime(tz, m.date, s.dTimeR || s.dTimeS)
, arrival: s.aTimeR || s.aTimeS ? : null
new Date(dateTime(tz, m.date, s.aTimeR || s.aTimeS)) : null , arrival: s.aTimeR || s.aTimeS
? dateTime(tz, m.date, s.aTimeR || s.aTimeS)
: null
})) }))
, frames: [] , frames: []
} }
@ -190,7 +204,7 @@ const movement = (tz, l, p, r) => (m) => {
module.exports = { module.exports = {
dateTime, dateTime,
location, product, remark, agency, location, product, remark, operator,
stop, applyRemark, part, route, stop, applyRemark, part, route,
departure, departure,
nearby, nearby,