Python class for TriMet train arrivals

I wanted a Bash alias that would output the time remaining until the next Eastbound train at the stop near my apartment. I created a Python class to get this data that I could implement in a script. This class takes as parameters your TriMet API key and the ID of the stop. To obtain a TriMet API key, visit

The data that the API returns is documented here:


import urllib2
import json
import datetime
import math

class NextTrain:

	def __init__(self, stopID, apiKey):
		self.__stopID = stopID
		self.__apiKey = apiKey	

	def printNextArrival(self):
		base_url = ""

		response = urllib2.urlopen(base_url + "locIDs=" + str(self.__stopID) + "&appID=" + self.__apiKey + "&json=true")
		data =
		jsonData = json.loads(data)

		direction = jsonData["resultSet"]["location"][0]["dir"]
		firstResult = jsonData["resultSet"]["arrival"][0]
		station = jsonData["resultSet"]["location"][0]["desc"]
		trainType = firstResult["shortSign"]
		nextArrivalTime = datetime.datetime.strptime(firstResult["estimated"], "%Y-%m-%dT%H:%M:%S.000-0800")
		nowTime =
		timeRemaining = nextArrivalTime - nowTime
		print "Next " + direction + " train at " + station + ":"
		print str(int(math.floor(timeRemaining.total_seconds() / 60))) + " min"
		print trainType

Using this class:

apiKey = "yourAPIKey"
stopID = integer_stop_id_here
nextTrain = NextTrain(stopID, apiKey)
Written on March 1, 2015