Help with a project to display real time bus/train time tables

I'm bored and am playing with having a dynamic display of bus arrival times from Point 1 to Point 2.

I live in Sydney and there is a page which I can use to get the times but it has me curious.

This link to get times (example)

(Follow the red line. give or take.)

This is the flow:
But it doesn't work as expected.

[{"id":"1f40efb6859bd765","type":"http request","z":"c69fc09ca37a90cf","name":"Call Transport NSW API","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":530,"y":570,"wires":[["a23c4fd08cde3d5c"]]},{"id":"fb748427cbbd5591","type":"function","z":"c69fc09ca37a90cf","name":"Build URL & Headers","func":"// Your API key goes here\nconst apiKey = \"my_key_here\";\n\nconst originStop = \"2131101\";\nconst destinationStop = \"2046143\";\n\n// Get current date/time\nconst now = new Date();\n\n// Format date as YYYYMMDD\nconst year = now.getFullYear();\nconst month = String(now.getMonth() + 1).padStart(2, '0');\nconst day = String(now.getDate()).padStart(2, '0');\nconst dateStr = `${year}${month}${day}`;\n\n// Format time as HHMM\nconst hours = String(now.getHours()).padStart(2, '0');\nconst minutes = String(now.getMinutes()).padStart(2, '0');\nconst timeStr = `${hours}${minutes}`;\n\n// Build URL\nconst url = `https://api.transport.nsw.gov.au/v1/tp/trip?outputFormat=rapidJSON&coordOutputFormat=WGS84%5BDD.DDDDD%5D&depArrMacro=dep&type_origin=any&name_origin=${originStop}&type_destination=any&name_destination=${destinationStop}&date=${dateStr}&time=${timeStr}&calcNumberOfTrips=3`;\n\n// Set message properties\nmsg.url = url;\nmsg.headers = {\"Authorization\": `apikey ${apiKey}`};\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":570,"wires":[["1f40efb6859bd765"]]},{"id":"a23c4fd08cde3d5c","type":"json","z":"c69fc09ca37a90cf","name":"Parse JSON","property":"payload","action":"obj","pretty":false,"x":730,"y":570,"wires":[["998cfb197a08f554"]]},{"id":"1805d454664526f0","type":"inject","z":"c69fc09ca37a90cf","name":"Start","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":570,"wires":[["fb748427cbbd5591"]]},{"id":"998cfb197a08f554","type":"function","z":"c69fc09ca37a90cf","name":"Process Journeys","func":"// Extract journeys\nlet journeys = msg.payload.journeys || [];\n\nif (journeys.length === 0) {\n    node.warn(\"No journeys found.\");\n} else {\n    journeys.forEach((journey, i) => {\n        journey.legs.forEach((leg, j) => {\n            let stops = leg.stopSequence ? leg.stopSequence.map(stop => stop.name) : [];\n            node.warn(`Journey ${i+1} Leg ${j+1} stops: ${stops.join(\", \")}`);\n        });\n    });\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":910,"y":570,"wires":[["ce5db5ecd35b3166"]]},{"id":"ce5db5ecd35b3166","type":"debug","z":"c69fc09ca37a90cf","name":"Debug Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":570,"wires":[]}]

This is what I get as output:
Journey 1 Leg 1 stops: Frederick St at Banks St, Ashfield, Frederick St at Albert Pde, Ashfield

Journey 1 Leg 2 stops: Frederick St at Albert Pde, Ashfield, Frederick St before Elizabeth St, Ashfield, Elizabeth St before Alt St, Ashfield, Elizabeth St at Bland St, Ashfield, Ashfield Station, Brown St, Stand C, Ashfield

Journey 1 Leg 3 stops: Ashfield Station, Platform 5, Ashfield, Croydon Station, Platform 5, Croydon, Burwood Station, Platform 6, Burwood

Journey 1 Leg 4 stops: Burwood Station, Burwood Rd, Stand E, Burwood, Westfield Burwood, Victoria St, Burwood, Victoria St at Shaftesbury Rd, Burwood, Victoria St at Lucas Rd, Burwood, Burwood Girls High School, Queen St, Croydon, Queen St opp Webb St, Croydon, Blair Park, Queen St, Croydon, Lang St after Queen St, Croydon, Church St opp Centenary Park, Croydon, Church St at Croydon Rd, Croydon, Croydon Rd at Dalmar St, Croydon, Great North Rd opp Harrabrook Ave, Five Dock, Great North Rd at Longview St, Five Dock, Great North Rd opp Club Five Dock RSL, Five Dock

And then it goes on to Journey 2, so I won't include it for now.

But I don't get what's happening.

It seems to me that it thinks I walk from one side of the road to the other and catch the bus going in the opposite direction and all this other stuff which complicates what I'm wanting to get/do.

The API_KEY is removed. Sorry. But I'm sure you understand.

Anyone near by mind having a look and help me understand how to get it working?

I have a few bus routes / train time tables I want to see dynamically see their times on a GUI.

Interesting that I've been thinking about timetables myself recently. But I started with something simpler

This uses a simplified web site that already processes the main UK timetables and makes it easier to deal with. I've not tried to do any realtime info as yet.

I really do need to get on top of bus timetables as well but that is certainly a bit harder.

Sorry I can't help you with Sidney though. Wrong side of the world. :smiley:

1 Like

No problems.

I've got talking to Mr ChatGPT and as handy as he is......

I was given scripts (not exactly) to do and get the information.
And they worked so then we went to the function node and codes.

But they don't.

So there seems to be something wrong.

I mentioned the failings and they were promptly addressed, but it seems there is something else going on not helping.

Well, interesting! Turns out that there is a UK-wide realtime bus API! Who knew :smiley:

Did you check for something like that in Oz?

1 Like

As far as I know, yes.

I can use the site provided or I can get a KEY and make my own front end to display stuff.

It is just knowing how the data is structured to get what I want.

(Strange Mr Chatgpt couldn't work that out)

Maybe he gets confused by being upside-down (being in Oz and all!) :rofl:

1 Like