So far I have this code:

- Code: Select all
`import json`

import urllib2

import time

url = urllib2.urlopen('http://reis.trafikanten.no/reisrest/realtime/getalldepartures/3010532')

lakkegata = json.load(url)

epoch = int(time.time())

count = 0

while (count <= 9):

destname = lakkegata[count]['DestinationName']

linename = lakkegata[count]['PublishedLineName']

realtime = lakkegata[count]['ExpectedDepartureTime']

time1 = realtime[6:]

time1 = time1[:-7]

time1 = int(time1) / 1000

arrive = (epoch - time1)

if (arrive >= -60):

print linename, destname, arrive, 'Now!'

if (-61 >= arrive >= -120):

print linename, destname, arrive, '1 min'

if (-121 >= arrive >= -180):

print linename, destname, arrive, '2 min'

if (-181 >= arrive >= -240):

print linename, destname, arrive, '3 min'

if (-241 >= arrive >= -300):

print linename, destname, arrive, '4 min'

if (-301 >= arrive >= -360):

print linename, destname, arrive, '5 min'

if (-361 >= arrive >= -420):

print linename, destname, arrive, '6 min'

if (-421 >= arrive >= -480):

print linename, destname, arrive, '7 min'

if (-481 >= arrive >= -540):

print linename, destname, arrive, '8 min'

if (-541 >= arrive >= -600):

print linename, destname, arrive, '9 min'

if (-601 >= arrive):

arrive2 = arrive

arrive = arrive / -60

print linename, destname, arrive2, arrive, 'min'

count = count + 1

First it polls the URL provided by the public transport company, then it uses json to load it into the value "Lakkegata". Then I made a loop that pulls the 10 first departures from this data, and format it.

The resulting output is this:

31 Grorud T -137 2 min

31 Tonsenhagen -250 4 min

17 Rikshospitalet -284 4 min

302 Hellerudhaugen ekspress -311 5 min

17 Sinsen-Grefsen stasjon -395 6 min

31 Snarøya -452 7 min

31 Grorud T -530 8 min

301 Kongskog -611 10 min

17 Sinsen-Grefsen stasjon -627 10 min

31 Fornebu -671 11 min

First number is the line number, then the name of the final station, then the number of seconds untill (realtime) arrival, then lastly formated into minutes (2 min = 2-3 minutes, 3 min = 3-4 min).

Next on my list is to turn the code into something that is looped regularly. I want the datafetch from the public transport company to happen every 60 seconds, since the company dont want anyone to hammer their servers. The next part of the code, where I pull data from the reply and format it. I want to run this fairly regularly and update every 10 seconds or something like this, so the time "untill arrival" stays correct.

The ultimate goal of this project is to make a LED display that shows the values of the output above, and is continually updated with information about my closest buss/tram stop.