Skip to content

Commit

Permalink
add trip()
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Apr 15, 2020
1 parent 8405a17 commit 75f0eed
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
3 changes: 3 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
45 changes: 45 additions & 0 deletions lib/trip.js
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -88,4 +112,5 @@ module.exports = {
parseStation,
parseOperator,
parseWhen,
parseStopover,
}
71 changes: 71 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 75f0eed

Please sign in to comment.