My city runs free bus services inside the city called CATs (Central Area Transit).
They have a "Live" time service where you can check to see when the next bus is due at your chosen stop. - http://www.transperth.wa.gov.au/Timetables/Live-Perth-CAT-Times
But their app is unreliable and sometimes takes up to 5 mins to refresh - kind of defeating the purpose of having a "live" tracker...this is the "new and improved" version too lol
So I decided to try and make my own tracker that I can put on our company intranet, quick and reliable checks so that you know when to head out the door instead of getting to the stop and having to wait 5-15 mins
Unfortunately it's a closed API, but with a little help from Fiddler you can see the call to the API being made, so all you have to do is reproduce it!
I used Powershell to start with, but may move it to Python if I end up deciding to make a little screen/button/arduino build next to the exit door.
They have a "Live" time service where you can check to see when the next bus is due at your chosen stop. - http://www.transperth.wa.gov.au/Timetables/Live-Perth-CAT-Times
But their app is unreliable and sometimes takes up to 5 mins to refresh - kind of defeating the purpose of having a "live" tracker...this is the "new and improved" version too lol
So I decided to try and make my own tracker that I can put on our company intranet, quick and reliable checks so that you know when to head out the door instead of getting to the stop and having to wait 5-15 mins
Unfortunately it's a closed API, but with a little help from Fiddler you can see the call to the API being made, so all you have to do is reproduce it!
I used Powershell to start with, but may move it to Python if I end up deciding to make a little screen/button/arduino build next to the exit door.
Code:
#Get our initial data from the page (cookies, viewstates, etc) and store it all in a session variable I decided to call Cookies.
$r=Invoke-WebRequest -Uri 'http://www.transperth.wa.gov.au/Timetables/Live-Perth-CAT-Times' -SessionVariable Cookies
#Dump $r and check which form or field we need to access to get the required data to pass to the API. In this case, Fiddler (by Telerik) revealed we need the "RequestVerificationToken"
#So find where that data is in $r and save it so we can pass it.
$requestToken = $r.InputFields.FindByName("__RequestVerificationToken").Value
#Fiddler also showed that we needed the TabId and ModuleId set - I've just hardcoded for now, but may need to find where they're generated using the same procedure as above.
$postParams = @{RequestVerificationToken=$requestToken;Referer='http://www.transperth.wa.gov.au/Timetables/Live-Perth-CAT-Times';TabId="249";ModuleId="1478"}
#POST our data to transperth's API page, using the fields above in the header, and not forgetting to include our SessionVariable that contains our cookie data!
$response = Invoke-WebRequest -Uri 'http://www.transperth.wa.gov.au/DesktopModules/CatLiveTimesMap/API/CatLiveTimesMapApi/GetLiveCatInfo' -WebSession $Cookies -Method POST -Headers $postParams
#Bam. JSON received. Now we just parse it and grab what we want.
$catTime = $response.Content | ConvertFrom-Json
$secondsLeft = $catTime.data.Stops | where {$_.Name -eq "Lord Street"} | select -ExpandProperty ETA
$ETA = [timespan]::FromSeconds($secondsLeft)
Write-Host "The bus is due in $($ETA.Minutes) mins and $($ETA.Seconds) seconds"
Last edited: