python_mvg_api – Munich Public Transport made simple

Intro

Not too long ago, MVG (aka Münchner Verkehrsgesellschaft) relaunched their Website, which now actually utilizes a JSON api! (I know, crazy, right?) This python module tries to provide easy to use access to most aspects of the mvg api[1].

It offers:

  • The next departures for any station in the MVV
    • This includes S-Bahn and Schienenersatzverkehr
  • Listing nearby stations based on geolocation
  • Search for Stations and POI
  • Routing
  • Current warnings about service interruptions

Take a look at example.py, it shows some basic concepts, or at the rest of the docs, where everything should be adequately documented.

[1]mvg.de uses stuff like an api key (although they only give it to themselves) and has some other quirks.

Module documentation

class mvg_api.Station(station)

Gives you a proxy to get the next departures for a particular station.

Either give it an exact station name (like “Hauptbahnhof”) or a station_id.

Deprecated-ish: This is not really all that useful. Just using get_id_for_station() and get_departures() really is the nicer way in most cases.

mvg_api.get_departures(station_id)

Get the next departures for station_id.

To get the station_id associated with a station name, use get_id_for_station().

Returns a list like:

[
    {
        'departureTimeMinutes': 0,
        'destination': 'Laimer Platz',
        'sev': False,
        'departureId': 1188266868,
        'live': True,
        'departureTime': 1478644495000,
        'lineBackgroundColor': '#b78730',
        'label': '5',
        'product': 'u'
    },
]

departureTimeMinutes, the time left to the departure in minutes, is added to the response from the api for your convenience.

mvg_api.get_id_for_station(station_name)

Returns the station_id for the given station name.

If more than one station match, the first result is given. None is returned if no match was found.

mvg_api.get_locations(query)

Returns all matches from the search for the given query string.

query can either be a name of a station or of a street, square, etc.

Returns a list wich looks somewhat like this:

[
    {
        'lines':
            {
            'nachtbus': [],
            'ubahn': ['2', '5', '7'],
            'tram': [],
            'sbahn': [],
            'otherlines': [],
            'nachttram': [],
            'bus': []
            },
        'hasLiveData': True,
        'place': 'München',
        'products': ['u'],
        'id': 1060,
        'type': 'nearbystation',
        'name': 'Innsbrucker Ring',
        'hasZoomData': True,
        'distance': 59,
        'longitude': 11.619138,
        'latitude': 48.120408
    },
]
mvg_api.get_nearby_stations(lat, lon)

Stations nearby the given location.

Parameters:
  • lat (float) – Latitude
  • lon (float) – and longitude of the desired location.

Returns a list which is formated in this fassion:

[
    {
        'lines':
            {
            'nachtbus': [],
            'ubahn': ['2', '5', '7'],
            'tram': [],
            'sbahn': [],
            'otherlines': [],
            'nachttram': [],
            'bus': []
            },
        'hasLiveData': True,
        'place': 'München',
        'products': ['BUS', 'TRAM', 'UBAHN', 'SBAHN'],
        'id': 1060,
        'type': 'nearbystation',
        'name': 'Innsbrucker Ring',
        'hasZoomData': True,
        'distance': 59,
        'longitude': 11.619138,
        'latitude': 48.120408
     },
 ]
mvg_api.get_route(start, dest, time=None, arrival_time=False, max_walk_time_to_start=None, max_walk_time_to_dest=None, change_limit=None)

Plans a route from start to dest

Parameters:
  • start (int/tuple) – The station_id of the starting station or a tuple of coordinates
  • dest (int/tuple) – station_id of the destination station or a tuple of coordinates
  • time (datetime, optional) –
  • arrival_time (bool, optional) – Specifies if time is the starting time (which is default) or the desired time of arrival.
  • max_walk_time_to_dest (max_walk_time_to_start,) – Maximum time of walking in minutes required to reach the start/dest.
mvg_api.get_stations(station)

Like get_locations(), but filters out all results which are not stations.

Indices and tables