db-vendo-client/readme.md

263 lines
13 KiB
Markdown
Raw Normal View History

2016-06-22 01:39:04 +02:00
# hafas-client
**A client for the "mobile APIs" of [HAFAS](https://de.wikipedia.org/wiki/HAFAS) public transport management systems**.
2016-06-22 01:39:04 +02:00
[![npm version](https://img.shields.io/npm/v/hafas-client.svg)](https://www.npmjs.com/package/hafas-client)
[![build status](https://img.shields.io/github/workflow/status/public-transport/hafas-client/test/5)](https://github.com/public-transport/hafas-client/actions?query=branch%3A5)
2018-03-19 22:34:08 +01:00
![ISC-licensed](https://img.shields.io/github/license/public-transport/hafas-client.svg)
2020-04-18 17:15:46 +02:00
[![support Jannis via GitHub Sponsors](https://img.shields.io/badge/support%20Jannis-donate-fa7664.svg)](https://github.com/sponsors/derhuerst)
[![chat with Jannis on Twitter](https://img.shields.io/badge/chat%20with%20Jannis-on%20Twitter-1da1f2.svg)](https://twitter.com/derhuerst)
2016-06-22 01:39:04 +02:00
2017-11-13 00:38:09 +01:00
## Background
[A company called HaCon](https://hacon.de) sells [a public transport management system called HAFAS](https://de.wikipedia.org/wiki/HAFAS) to public transport authorities and providers, mostly in Europe. It provides routing and departure information to their customers.
2017-11-13 00:38:09 +01:00
Most customers get their own, **separate HAFAS deployments**; They all use the same terminology and API calls, but have slightly different versions, configurations and sets of enabled features. Using [built-in endpoint-specific customisations](p/readme.md), **`hafas-client` abstracts most of these differences away, and supports additional features in some cases**. Check the [list of supported networks/endpoints](p/readme.md#built-in-profiles) for more info.
*Note:* Currently, **`hafas-client` only supports "mobile API" endpoints**, which are designed for and used by the respective official mobile app(s); These endpoints almost always have `mgate.exe` in the URL. This library *does not* support "open API" endpoints (often they have `rest-proxy` or `openapi` in the URL) yet, but [#134](https://github.com/public-transport/hafas-client/pull/134) contains work in progress.
Strictly speaking, permission is necessary to use this library with a HAFAS "mobile" endpoint. It merely tries to remove the *technical* barrier of accessing the data, in order to kick-start an ecosystem or apps and services that will eventually rely on [*openly available* data](https://opendatahandbook.org/solutions/en/Public-Transport-Data/).
## Supported networks/endpoints
`hafas-client` has [built-in support for many public transportation networks](p/readme.md).
There are also libraries that use `hafas-client` and pass their own profile in:
HAFAS endpoint | library
---------------|--------
[Betriebsstellen & disturbances in the German rail network](http://strecken.info/) | [`db-netz-hafas`](https://github.com/derhuerst/db-netz-hafas)
2017-11-13 00:38:09 +01:00
2016-06-22 01:39:04 +02:00
## Installing
```shell
npm install hafas-client
```
### with [react-native](https://facebook.github.io/react-native/)
`hafas-client` as well its dependencies use [Node-builtin modules](https://nodejs.org/dist/latest/docs/api/) and [Node globals](https://nodejs.org/api/globals.html). To be able to use it within react-native, follow [the instructions at `node-libs-react-native`](https://github.com/parshap/node-libs-react-native/blob/master/README.md#usage).
2016-06-22 01:39:04 +02:00
## Usage
Pick the [profile](p/readme.md) for the HAFAS endpoint covering the area you want to get data for. Pass the profile and a descriptive name for your program into the `createClient` function:
2017-11-18 10:14:17 +01:00
```js
2017-11-13 00:38:09 +01:00
const createClient = require('hafas-client')
const dbProfile = require('hafas-client/p/db')
2019-12-10 19:39:17 +01:00
// create a client with the Deutsche Bahn profile
const client = createClient(dbProfile, 'my-awesome-program')
```
2017-11-13 00:38:09 +01:00
You can now use `client` to query the HAFAS endpoint configured in the [`db` profile](p/db):
```js
2017-11-13 00:38:09 +01:00
// Berlin Jungfernheide to München Hbf
const res = await client.journeys('8011167', '8000261', {results: 1})
console.log(res)
2017-11-13 00:38:09 +01:00
```
`journeys()` returns a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/promise) that will resolve with an object with an array `journeys` that contains one [*Friendly Public Transport Format* (*FPTF*) v2 draft `journey`](https://github.com/public-transport/friendly-public-transport-format/blob/3bd36faa721e85d9f5ca58fb0f38cdbedb87bbca/spec/readme.md#journey).
2017-11-13 00:38:09 +01:00
```js
2019-01-16 21:40:45 +08:00
{
journeys: [ {
2019-12-10 19:39:17 +01:00
origin: {
type: 'station',
id: '8089100',
name: 'Berlin Jungfernheide (S)',
location: { /* … */ },
products: { /* … */ }
},
2019-12-10 19:40:13 +01:00
departure: '2017-12-19T17:05:30+01:00',
plannedDeparture: '2017-12-19T17:05:00+01:00',
2019-12-10 19:39:17 +01:00
departureDelay: 30,
departurePlatform: '5',
2019-12-10 19:40:13 +01:00
plannedDeparturePlatform: '5',
2019-12-10 19:39:17 +01:00
destination: {
type: 'station',
id: '8000261',
name: 'München Hbf',
location: { /* … */ },
products: { /* … */ }
},
2019-12-10 19:40:13 +01:00
arrival: '2017-12-19T22:44:00+01:00',
plannedArrival: '2017-12-19T22:45:00+01:00',
2019-12-10 19:39:17 +01:00
arrivalDelay: -60,
arrivalPlatform: '11A',
2019-12-10 19:40:13 +01:00
plannedArrivalPlatform: '13',
2019-12-10 19:39:17 +01:00
2019-01-16 21:40:45 +08:00
legs: [ {
id: '1|100067|48|81|17122017',
line: {
type: 'line',
id: '41172',
name: 'S 41',
public: true,
mode: 'train',
product: 'suburban',
operator: {
type: 'operator',
id: 's-bahn-berlin-gmbh',
name: 'S-Bahn Berlin GmbH'
}
},
2019-12-10 19:39:17 +01:00
direction: 'Ringbahn ->',
2019-01-16 21:40:45 +08:00
origin: {
type: 'station',
2019-12-10 19:39:17 +01:00
id: '8089100',
name: 'Berlin Jungfernheide (S)',
2019-01-16 21:40:45 +08:00
location: {
type: 'location',
2019-12-10 19:39:17 +01:00
latitude: 52.530291,
longitude: 13.299451
2019-01-16 21:40:45 +08:00
},
products: { /* … */ }
},
2019-12-10 19:40:13 +01:00
departure: '2017-12-19T17:05:30+01:00',
plannedDeparture: '2017-12-19T17:05:00+01:00',
2019-12-10 19:39:17 +01:00
departureDelay: 30,
departurePlatform: '5',
2019-12-10 19:40:13 +01:00
plannedDeparturePlatform: '5',
2019-12-10 19:39:17 +01:00
2019-01-16 21:40:45 +08:00
destination: {
type: 'station',
2019-12-10 19:39:17 +01:00
id: '8089118',
name: 'Berlin Beusselstraße'
// …
2019-01-16 21:40:45 +08:00
},
2019-12-10 19:40:13 +01:00
arrival: '2017-12-19T17:08:00+01:00',
plannedArrival: '2017-12-19T17:08:00+01:00',
2019-12-10 19:39:17 +01:00
arrivalDelay: null,
2019-12-10 19:40:13 +01:00
arrivalPlatform: '2a-b',
plannedArrivalPlatform: '1'
2019-12-10 19:39:17 +01:00
},
/* more legs… */
{
walking: true,
public: true,
2019-12-10 19:39:17 +01:00
origin: {
type: 'station',
id: '730749',
name: 'Berlin Hauptbahnhof (S+U), Berlin'
// …
2019-12-10 19:39:17 +01:00
},
2019-12-10 19:40:13 +01:00
plannedDeparture: '2017-12-19T17:25:00+01:00',
prognosedDeparture: null,
2019-12-10 19:39:17 +01:00
departureDelay: null,
destination: {
type: 'station',
id: '8098160',
name: 'Berlin Hbf (tief)'
// …
2019-12-10 19:39:17 +01:00
},
2019-12-10 19:40:13 +01:00
arrival: '2017-12-19T17:33:00+01:00',
plannedArrival: '2017-12-19T17:33:00+01:00',
2019-12-10 19:39:17 +01:00
arrivalDelay: null
2019-01-16 21:40:45 +08:00
}, {
id: '1|70906|0|81|17122017',
2019-12-10 19:39:17 +01:00
line: { /* … */ },
direction: 'München Hbf',
2019-01-16 21:40:45 +08:00
origin: {
type: 'station',
id: '8098160',
2019-12-10 19:39:17 +01:00
name: 'Berlin Hbf (tief)'
// …
2019-01-16 21:40:45 +08:00
},
2019-12-10 19:40:13 +01:00
departure: '2017-12-19T17:35:00+01:00',
plannedDeparture: '2017-12-19T17:37:00+01:00',
2019-12-10 19:39:17 +01:00
departureDelay: -120,
2019-01-16 21:40:45 +08:00
departurePlatform: '1',
2019-12-10 19:40:13 +01:00
plannedDeparturePlatform: '1',
2019-12-10 19:39:17 +01:00
2019-01-16 21:40:45 +08:00
destination: {
type: 'station',
id: '8000261',
name: 'München Hbf',
// …
2019-01-16 21:40:45 +08:00
},
2019-12-10 19:40:13 +01:00
arrival: '2017-12-19T22:44:00+01:00',
plannedArrival: '2017-12-19T22:45:00+01:00',
2019-12-10 19:39:17 +01:00
arrivalDelay: -60,
2019-12-10 19:40:13 +01:00
arrivalPlatform: '11A',
plannedArrivalPlatform: '13'
2019-01-16 21:40:45 +08:00
} ],
price: {
amount: null,
hint: 'No pricing information available.'
}
// …
2019-12-10 19:39:17 +01:00
} ]
// …
2019-01-16 21:40:45 +08:00
}
2017-11-11 21:06:54 +01:00
```
2016-06-22 01:39:04 +02:00
Each [profile](p/readme.md) has more detailed example code.
### in the browser
While `hafas-client` itself should work in the browser via a bundler like [Webpack](https://webpack.js.org), most HAFAS API endpoints don't enable [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing), so you won't be able query them directly.
2019-12-10 19:39:17 +01:00
## TypeScript
There are [community-maintained TypeScript typings](https://www.npmjs.com/package/@types/hafas-client).
2019-12-10 19:39:17 +01:00
## API
2019-12-10 19:39:17 +01:00
[API documentation](docs/readme.md)
2019-12-10 19:39:17 +01:00
## Related Projects
- [`*.transport.rest`](https://transport.rest/)  Public APIs wrapping some HAFAS endpoints.
- [`BahnhofsAbfahrten`](https://github.com/marudor/BahnhofsAbfahrten) a.k.a. [`marudor.de`](https://marudor.de/)  A very detailed public transport website for Germany. Uses HAFAS underneath, [has an API](https://docs.marudor.de).
- [`public-transport-enabler`](https://github.com/schildbach/public-transport-enabler) Java equivalent to `hafas-client`, with support for more types of public transport APIs; Used by [Öffi](https://play.google.com/store/apps/details?id=de.schildbach.oeffi) & [Transportr](https://transportr.app).
- [`TripKit`](https://github.com/alexander-albers/tripkit) Swift equivalent to `hafas-client`, with support for more types of public transport APIs; Used by [ÖPNV Navigator](https://apps.apple.com/de/app/öpnv-navigator/id1239908782).
- [`kpublictransport`](https://github.com/KDE/kpublictransport) C++ equivalent to `hafas-client`, with support for more types of public transport APIs; Used by [KDE Itinerary](https://apps.kde.org/itinerary/).
- [`pyhafas`](https://github.com/n0emis/pyhafas) Python equivalent to `hafas-client`, with support for more types of public transport APIs.
- [`hafas-rs`](https://cyberchaos.dev/yuka/hafas-rs/) Rust equivalent to `hafas-client`.
- [`fshafas`](https://github.com/bergmannjg/fshafas) F# port of `hafas-client`.
2022-10-24 14:11:47 +02:00
- [`hafas-client-php`](https://github.com/MrKrisKrisu/hafas-client-php) PHP port of `hafas-client`.
- [*Transit*](https://jlnstrk.github.io/transit/) Kotlin client for mutliple kinds of public transport APIs, with support for HAFAS APIs.
- [*Friendly Public Transport Format*](https://github.com/public-transport/friendly-public-transport-format#friendly-public-transport-format-fptf) A format for APIs, libraries and datasets containing and working with public transport data.
2020-04-18 17:15:46 +02:00
- [`observe-hafas-client`](https://github.com/public-transport/observe-hafas-client) Observe all departures/arrivals/etc. returned by `hafas-client`.
- [`cached-hafas-client`](https://github.com/public-transport/cached-hafas-client) Pass in a `hafas-client` instance, cache data from it.
- [`hafas-client-rpc`](https://github.com/derhuerst/hafas-client-rpc) Make JSON-RPC calls to `hafas-client` via WebSockets & stdio.
2020-04-18 17:15:46 +02:00
- [`hafas-client-health-check`](https://github.com/public-transport/hafas-client-health-check) Check if a `hafas-client` instance and its endpoint work.
- [`hafas-rest-api`](https://github.com/public-transport/hafas-rest-api#hafas-rest-api) Expose a HAFAS client via an HTTP REST API.
- [List of european long-distance transport operators, available API endpoints, GTFS feeds and client modules.](https://github.com/public-transport/european-transport-operators)
- [Collection of european transport JavaScript modules.](https://github.com/public-transport/european-transport-modules)
2020-03-18 21:32:05 +01:00
- [`hafas-find-stations`](https://github.com/derhuerst/hafas-find-stations#hafas-find-stations) Given a HAFAS client, find all stations in a bounding box.
2020-04-18 17:15:46 +02:00
- [`hafas-collect-departures-at`](https://github.com/public-transport/hafas-collect-departures-at#hafas-collect-departures-at)  Utility to collect departures, using any HAFAS client.
- [`find-hafas-data-in-another-hafas`](https://github.com/derhuerst/find-hafas-data-in-another-hafas#find-hafas-data-in-another-hafas)  Find data from one HAFAS endpoint in the data of another HAFAS endpoint.
2022-01-13 13:57:48 +01:00
- [`hafas-gtfs-rt-feed`](https://github.com/derhuerst/hafas-gtfs-rt-feed) Generate a [GTFS Realtime (GTFS-RT)](https://developers.google.com/transit/gtfs-realtime/) feed by polling a HAFAS endpoint.
- [`hafas-monitor-trips`](https://github.com/derhuerst/hafas-monitor-trips#hafas-monitor-trips)  Using a HAFAS client, watch all trips in a bounding box.
- [`hafas-monitor-departures`](https://github.com/derhuerst/hafas-monitor-departures#hafas-monitor-departures)  Pass in a HAFAS client, fetch all departures at any set of stations.
- [`hafas-record-delays`](https://github.com/derhuerst/hafas-record-delays#hafas-record-delays)  Record delays from `hafas-monitor-departures` into a LevelDB.
- [`hafas-monitor-journeys`](https://github.com/derhuerst/hafas-monitor-journeys)  Use `hafas-client` to monitor journeys from A to B.
- [`hafas-discover-stations`](https://github.com/derhuerst/hafas-discover-stations#hafas-discover-stations) Pass in a HAFAS client, discover stations by querying departures.
- [`hafas-estimate-station-weight`](https://github.com/derhuerst/hafas-estimate-station-weight#hafas-estimate-station-weight) Pass in a HAFAS client, estimate the importance of a station.
- [`db-ticket`](https://github.com/envake/db-tickets) A library to retrieve ticket information from Deutsche Bahn.
More related libraries can be found [via the npm package index](https://www.npmjs.com/search?q=hafas).
2016-06-22 01:39:04 +02:00
## Contributing
If you **have a question**, **found a bug** or want to **propose a feature**, please [open an Issue](https://github.com/public-transport/hafas-client/issues).
2020-03-18 21:32:05 +01:00
This project needs help! Check the [list of "help wanted" Issues](https://github.com/public-transport/hafas-client/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22).
If you're contributing code, please read the [contribution guidelines](contributing.md).