hafas-client

repository·main·Indexed 18 days ago

https://github.com/public-transport/hafas-client

A client library for interacting with HAFAS (Hannover Anruf- und Verkehrsservice) public transport APIs. Version 6.3.6 supports request throttling, automatic retries, custom logging, and User-Agent randomization. It includes various transport provider profiles such as AVV, BART, BLS, BVG, CFL, CMTA, DART (Des Moines), DB, and DB Busradar NRW.

Tokens
46.7K
Snippets
147
Records
184
Agent score
61%

What's inside hafas-client

  1. Use the `hafas-client` API to query public transport data

    main

    The hafas-client provides a comprehensive set of methods to interact with HAFAS servers. You can query journeys, departures, arrivals, locations, and more. Each method typically accepts a primary identifier (like a station ID or location query) and an optional opt object for fine-tuning the request.

    Core API Methods:

    • Journeys & Trips:

      • journeys(from, to, [opt]): Get journeys between two locations.
      • refreshJourney(refreshToken, [opt]): Fetch up-to-date details or more information for an existing journey using a refresh token.
      • journeysFromTrip(tripId, previousStopover, to, [opt]): Get journeys starting from a specific trip to a destination.
      • trip(id, lineName, [opt]): Get detailed information for a specific trip.
      • tripsByName(lineNameOrFahrtNr, [opt]): Retrieve all trips matching a specific name or FahrtNr.
    • Station & Location Queries:

      • departures(station, [opt]): Query upcoming departures at a specific station.
      • arrivals(station, [opt]): Query upcoming arrivals at a specific station.
      • locations(query, [opt]): Search for stations, Points of Interest (POIs), and addresses.
      • stop(id, [opt]): Get detailed information about a specific stop or station.
      • nearby(location, [opt]): Find stations and POIs near a given location.
      • reachableFrom(address, [opt]): Find all stations reachable from a specific address within a certain time limit.
    • System & Infrastructure:

      • radar(north, west, south, east, [opt]): Find all vehicles currently located within a specified bounding box.
      • lines(query, [opt]): Search for lines matching a name.
      • remarks([opt]): Retrieve all current service remarks/alerts.
      • serverInfo([opt]): Fetch metadata and information about the HAFAS server.
  2. Understand the `journeys()` response format

    main

    The journeys() method resolves with an object containing:

    • journeys: An array of journey objects.
    • earlierRef: A reference string for retrieving earlier journeys.
    • laterRef: A reference string for retrieving later journeys.
    • realtimeDataUpdatedAt: A UNIX timestamp of the latest realtime data update.

    Key Journey Data Fields

    • Realtime vs Scheduled: departure and arrival times include current delays. Use plannedDeparture/plannedArrival for the original schedule and departureDelay/arrivalDelay to see the difference.
    • Cancellations: If a journey leg is cancelled, cancelled: true is added, and departure/arrival fields will be null.
    • Tickets: If tickets: true is used (and supported by the profile), each journey includes a tickets array with name, price, tariff, coverage, variant, and amount.
    • Scheduled Days: If scheduledDays: true is used, each journey includes a scheduledDays object mapping dates (e.g., '2018-01-01') to a boolean indicating validity.
  3. Format dates and times for HAFAS mgate.exe

    main

    HAFAS mgate.exe endpoints use specific string formats for dates and times. These are relative to the timezone configured on the server side for that specific endpoint.

    • Date format: YYYYMMDD (e.g., 20191212).
    • Time format: HHMMSS (e.g., 010013).

    Day Offsets: If a requested time range crosses into the next day, HAFAS uses a 'day offset' in the time string. For example, a departure at 2019-12-13T00:13 requested during a query for 2019-12-12 would be encoded as date 20191212 and time 01001300 (where the extra digits represent the offset).

  4. Format coordinates for HAFAS mgate.exe

    main

    Coordinates in mgate.exe endpoints use the WGS84 standard. However, the values are scaled by $10^6$.

    To encode a coordinate, multiply the decimal value by $1,000,000$.

    Example: To encode {latitude: 1.23, longitude: -2.34}, use:

    • Y: 1230000 (Latitude)
    • X: -2340000 (Longitude)

    There is also an optional z parameter for elevation.