Skip to content

Get public transport timetables from the Google Transit API

License

Notifications You must be signed in to change notification settings

andrashann/gptt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gptt

gptt (Get Public Transport Timetables) is a command line tool to download and format public transport timetables from the Google Directions API for a given day between an origin and a destination using the Transit travel mode.

Installation

The best way to install gptt is via pip: pip install gptt. You can alternatively install it from the source: python setup.py install.

Usage

Command line

gptt is primarily intended to be used as a command line tool, although its functions can be used in a Python program (see below). Using the default template, it generates detailed timetables like this one:

Example timetable

gptt -f "Budapest, Kelenföld vasútállomás" -t "Hejce" -d "2020-07-01" -k $GOOGLE_API_KEY -o timetable.html, for example, downloads all public transport connections between Budapest, Kelenföld vasútállomás (a train station in Budapest, Hungary) and Hejce (a village in Hungary) for the date July 1, 2020 using the Google Maps API key defined in the environment variable $GOOGLE_API_KEY, then format this data using the default HTML template and output it to timetable.html.

Note: the default HTML template is designed to be used for routes between, not within municipalities (it emphasizes the locality more than the actual stop name), however, you can create custom templates to suit your specific needs.

Full description of command line options:

flag option description
-h --help Show the help.
Basic arguments to get timetable data (must be passed here or in the config file):
-f --from Where to plan the route from (in a form that Google Maps would understand).
-t --to Where to plan the route to (in a form that Google Maps would understand).
-d --date The date to be used for planning in a YYYY-MM-DD format.
-k --api-key Google API key with the Directions, Geocoding, and Time Zone API enabled.
Further arguments to customize the timetable:
-l --lang Language code used to display results, eg. 'en-GB' or 'hu' - see Google's list of supported languages. Defaults to 'en'.
--max-transfers Maximum number of allowed transfers in the results. Default is 99.
--vehicle-type-names Used to replace vehicle type names (e.g. HEAVY_RAIL or BUS with another string in the output – accepts one or more '=' separated pairs, e.g. "HEAVY_RAIL=Ⓣ" "BUS=Ⓑ".
Arguments related to the output:
-v --verbose Print diagnostic messages to stderr.
-j --json Output the results in the raw JSON format it is processed from the API.
--json-indent If the output is JSON, this many spaces will be used to indent it. If not passed, everything will be on one line.
--template Jinja2 template file to use instead of the default template. The default template is HTML, but it could be any text format, such as Markdown or LaTeX. Irrelevant when `--json` is also passed.
-o --output Output file to be written. If not given, results will be printed to stdout.
Using a config file:
-c --config Accepts a JSON file with a single object whose keys are zero or more of the options described in this table. Values should be appropriate for the option. See an example below. If given, any value present in the config will overwrite the value given by the command line flag/option.

Using a config file

The following config.json adds values for the non-required options and the API key. (DO NOT commit your API key to a version control system!).

{
  "vehicle-type-names": ["HEAVY_RAIL=Ⓣ","BUS=Ⓑ"],
  "station-name-replacements": ["Hauptbahnhof=hbf.", "Bahnhof=bf."],
  "lang": "en-GB",
  "max-transfers": 3,
  "api-key": "ab4ab2fa-74c9-4af1-a250-9efe735c80fb"
}

Using this file, we can run gptt -f "London" -t "Manchester" -d "2020-08-19" -c config.json.

Python package

The two main functions, get_transit_plan_for_timestamp() and get_transit_plans_for_day() can be accessed by

from gptt import timetables
timetables.get_transit_plan_for_timestamp(...)
timetables.get_transit_plans_for_day(...)

Detailed documentation of these functions can be found in the code.

Contributing

Issue submissions and pull requests are welcome. Simple fixes do not require an issue to be submitted, however, do submit one if your pull request includes a lot of changes or new features.

More info

Read more about this project on my blog.

Changelog

  • 0.1.1:
    • [bugfix] Fixed a bug when certain localities were not parsed correctly from API response (#1)
    • [bugfix] Made the program aware of the local time of the origin of the query to define the day (#2)
    • [bugfix] (partial): Better handling of the Routing API not returning transit results. This is not entirely resolved (#3)
  • 0.1.0:
    • initial public release

Releases

No releases published

Packages

No packages published