Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions MVGLive/mvglive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests
import json
import sys
import re
import datetime
try:
unicode
Expand Down Expand Up @@ -110,6 +111,50 @@ def getDeparture(self, current, departure):
else:
return departure

def getdisruptiondata(self):

s = requests.Session();

# Depatures
payload = ("7|0|4|http://www.mvg-live.de/MvgLive/mvglive/|"
"7690A2A77A0295D3EC713772A06B8898|"
"de.swm.mvglive.gwt.client.newsticker.GuiNewstickerService|"
"getNewsticker|1|2|3|4|0|")
headers = {'Content-Type': 'text/x-gwt-rpc; charset=utf-8'}
r = s.post("http://www.mvg-live.de/MvgLive/mvglive/rpc/newstickerService", data = payload, headers = headers)

if (r.text[:4] == '//OK'):
data = r.text[4:]
data = data.replace("'", '"')
data = json.loads(data)

for _item in data:
if not isinstance(_item, list):
continue
okay_items = _item
break

regex = re.compile('java.util.ArrayList|de.swm.mvglive.gwt.client.newsticker.NewstickerItem')
okay_items = [x for x in okay_items if not regex.match(x)]

#elements alternate, first is the headline, second is the description
headlines = okay_items[::2] # Headlines, Start at first element, then every other.
details = okay_items[1::2] # Details, start at second element, then every other.

#No disruption is no disruption...
if len(headlines) == 1 and headlines[0] == 'Zurzeit liegen keine Meldungen vor. Wir wünschen Ihnen gute Fahrt mit U-Bahn, Bus und Tram - Ihre MVG.':
return []

disruptions = []
for i in range(len(headlines)):
disruption = {'headline': headlines[i], 'details': details[i]}
disruptions.append(disruption)
return disruptions

else:
raise(ValueError('Returned Data is not //OK - Aborting.'))


# Adapted from https://github.com/dice-cyfronet/gwt-proxy/blob/master/src/com/gdevelop/gwt/syncrpc/Utils.java
def longFromBase64(self, value):
longval = self.base64Value(value[0])
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ Configuration variables:
- **bus** (*Optional*): If 'False', do not display bus departures

- **sbahn** (*Optional*): If 'False', do not display S-Bahn (suburban train) departures

## MVGLive.getdisruptiondata()
Retrieve current disruptions/status messages from mvg-live.de. Returns headline and description of disruption.

Please note, that this call will execute another API-request and might cause problems with rate-limits imposed by the webservice.

For a saver way, please use [RSS-feed](http://www.mvg-mobil.de/Tickerrss/CreateRssClass) that is also provided by the MVG. You can find a sample implementation [here](https://github.com/muccc/anzeigr/blob/master/current_nodes/mvgdefas/ticker/getmvgticker.py)
2 changes: 2 additions & 0 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

foo = MVGLive.MVGLive()
print(foo.getlivedata("Hauptbahnhof"))

print(foo.getdisruptiondata())