diff --git a/example.js b/example.js index a4ced99..2080bcb 100644 --- a/example.js +++ b/example.js @@ -29,6 +29,9 @@ const find = (name, stream) => { const [depAtAlexanderplatz] = await flix.departures(berlinAlexanderplatz) console.error(depAtAlexanderplatz) + + const trip = await flix.trip(depAtAlexanderplatz.tripId) + console.error(trip) })() .catch((err) => { console.error(err) diff --git a/lib/index.js b/lib/index.js index 8b00266..b93ec78 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,10 +4,12 @@ const stations = require('./stations') const regions = require('./regions') const journeys = require('./journeys') const { departures, arrivals } = require('./timetable') +const trip = require('./trip') module.exports = { stations, regions, journeys, departures, arrivals, + trip, } diff --git a/lib/trip.js b/lib/trip.js new file mode 100644 index 0000000..ba7f3ae --- /dev/null +++ b/lib/trip.js @@ -0,0 +1,45 @@ +'use strict' + +const { + parseDateTime, + parseOperator, + parseStopover, + normalizeEmpty, +} = require('./util') +const fetch = require('./fetch') + +const trip = async (tripId, opt = {}) => { + opt = { + ...opt, + } + + if ('string' !== typeof tripId || !tripId) { + throw new Error('tripId is must be a non-empty string') + } + + const t = await fetch(`trips/${encodeURIComponent(tripId)}/info.json`, opt) + const [trip] = t.trips + + return { + id: t.uid, + tripType: t.type, // don't conflict with FPTF's `type` + direction: trip.line_direction, + lineName: trip.line_code, + operator: trip.operated_by ? parseOperator(trip.operated_by) : null, + cancelled: trip.is_cancelled === true, + realtimeDataUpdatedAt: ( + t.real_time_info && t.real_time_info.status !== 'too_soon' + ? parseDateTime(t.real_time_info.updated_at) + : null + ), + stopovers: trip.stops.map(st => parseStopover(st)), + hasTracker: normalizeEmpty(trip.has_tracker), + // @todo trip.messages + } +} + +trip.features = { // require by fpti + ...fetch.features, +} + +module.exports = trip diff --git a/lib/util.js b/lib/util.js index dc3990d..3620799 100644 --- a/lib/util.js +++ b/lib/util.js @@ -80,6 +80,30 @@ const parseWhen = (keys, data, cancelled = false) => { } } +const parseStopover = (st) => { + const arrCancelled = st.arrival_had && st.arrival_had.status === 'trip_cancelled' + const arr = parseWhen({ + planned: 'arrival', + prognosed: 'arrival_had', + }, st, arrCancelled) + const depCancelled = st.departure_had && st.departure_had.status === 'trip_cancelled' + const dep = parseWhen({ + planned: 'departure', + prognosed: 'departure_had', + }, st, depCancelled) + + return { + station: parseStation(st.station), + cancelled: !!depCancelled && !!arrCancelled, + arrival: arr.when, + plannedArrival: arr.plannedWhen, + arrivalDelay: arr.delay, + departure: dep.when, + plannedDeparture: dep.plannedWhen, + departureDelay: dep.delay, + } +} + module.exports = { formatDateTime, parseDateTime, @@ -88,4 +112,5 @@ module.exports = { parseStation, parseOperator, parseWhen, + parseStopover, } diff --git a/readme.md b/readme.md index 52ebaac..d5dce85 100644 --- a/readme.md +++ b/readme.md @@ -328,6 +328,77 @@ await flix.departures(berlinZOB) } ``` +--- + +### `trip(tripId, [opt])` + +Fetch a trip, a specific vehicle travelling to stations at specific points in time. + +#### Supported Options + +Attribute | Description | Value type | Default +----------|-------------|------------|-------- +`apiKey` | Custom Flix API key | `String` | *default api key* + +#### Example + +```js +const [dep] = await flix.departures({ type: 'station', id: '1' }) +await flix.trip(dep.tripId) +``` + +```js +{ + id: 'direct:97649761:1224:36', + tripType: 'direct', + direction: 'Route 070 direction Hamburg ZOB', + lineName: '070', + operator: { + type: 'operator', + id: 'busart-tours-gmbh', + name: 'BusArt Tours GmbH', + url: null, + address: 'Albrechtstraße. 138-140 12099 Berlin' + }, + + cancelled: true, + realtimeDataUpdatedAt: null, + stopovers: [ + { + station: { + type: 'station', + id: '1224', + name: 'Berlin Alexanderplatz', + // … + }, + cancelled: false, + arrival: null, + plannedArrival: null, + arrivalDelay: null, + departure: null, + plannedDeparture: '2020-04-15T15:00:00+02:00', + departureDelay: null, + }, + { + station: { + type: 'station', + id: '36', + name: 'Hamburg ZOB', + // … + }, + cancelled: false, + arrival: '2020-04-15T18:35:00+02:00', + plannedArrival: '2020-04-15T18:30:00+02:00', + arrivalDelay: 300, + departure: null, + plannedDeparture: null, + departureDelay: null, + } + ], + hasTracker: false, +} +``` + ## Similar Projects - [search-flix-locations](https://github.com/derhuerst/search-flix-locations/) - Search for FlixBus (Meinfernbus) / FlixTrain cities & stations.