pc-coholic / PyMVGLive

Python-Library to get live-data from mvg-live.de - yet another workaround the official, non-released API....
14 stars 2 forks source link

Update to use new API #12

Open pc-coholic opened 5 years ago

pc-coholic commented 5 years ago

MVG has launched a new API while keeping the old one online.

Unfortunately, it seems like the data served by the old (currently used) API is staring to degrade. For example, queries for Garching-Hochbrück only return U-Bahn-trains but not any busses.

The new API lives at https://www.mvg.de/fahrinfo/api/departure/480?footway=0, where footway takes the current timeoffset parameter and 480 is the ID of the station (in this example: Garching-Hochbrück).

Querying for station-data can be done at https://www.mvg.de/fahrinfo/api/location/queryWeb?q=garching, with the q-parameter obviously being the search-string.

Queries need to be authorized by providing the "X-MVG-Authorization-Key-header. One possible value is for example 5af1beca494712ed38d313714d4caff6 (mvg.de website-key).

The new API is a lot more usable as it does not need any of that RPC-decryption that is currently used.

If anyone feels like implementing the new API - possibly while keeping compatibility to the current Syntax: please do so :-)

mammuth commented 5 years ago

Note for people stumbling over this via Google:

The endpoint seems to check the user agent and restricts access for eg. using request's default one. The following works for me as of today:

station = 2  # Marienplatz
url = f'https://www.mvg.de/fahrinfo/api/departure/{station}?footway=0'
headers = {'X-MVG-Authorization-Key': '5af1beca494712ed38d313714d4caff6', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'}

requests.get(url, headers=headers).json()
jeff25 commented 4 years ago

Hi,

i try to use your sample but did not work... do you have any idea why, is there any change in the MVG API?

Br, RObert

pc-coholic commented 4 years ago

That is quite possible - they love to change things around...

A quick check on their website reveals, that there have apparently been some changes...

For example, the call for Marienplatz is now https://www.mvg.de/api/fahrinfo/departure/de:09162:2?footway=0, the one for Ostbahnhof https://www.mvg.de/api/fahrinfo/departure/de:09162:5?footway=0.

The Search-Endpoint (https://www.mvg.de/api/fahrinfo/location/queryWeb?q=ost) will provide you with the correct station id.

Furthermore, the API-token seems to be now x-api-token: Vv7L4Gbu2k.

Someone[TM] should really update this library to use the new endpoints and data ;-)

jeff25 commented 4 years ago

Thanks, i did nearly the same and check with chrome developer tools and now your code works with the new station ID´s :-) THanks!

laim2003 commented 4 years ago

The api token was not required in my case