Possible to download incidents XML output to a file?

107 views
Skip to first unread message

TheDoc

unread,
Jul 24, 2017, 3:39:01 PM7/24/17
to A gathering place for the Open Rail Data community
Hi Everyone :)

I'm new to this so apologies if this is a newvbie question!.  I've been looking at the Stations XML thread on here and I've managed to get the Incidents XML to work for myself in PostMan where is shows be the XML output which is great.  Quite impressed I've got to this stage already :)

I'm now looking to have a way to download the XML to a file and ideally using a tool or script to do this automatically.  I know the token expires every hour so I'd need the script to get the token key and then use that each time but is this possible at all? Is there a way to script and automate the output to a file? I'm already happy with XML and the schema so once I've got the XML in a file I'll be fine again but getting the actual file created has got me a bit stuck at the moment.

I appreciate any help or pointers :)

Regards
Al

Rail Ale Fan

unread,
Jul 25, 2017, 9:22:53 AM7/25/17
to A gathering place for the Open Rail Data community
Hello Al,

Have a go with the following JavaScript which should run under Windows Scripting Host;


user = "YOUR_USERNAME";
pass = "YOUR_PASSWORD";
feed = "https://datafeeds.nationalrail.co.uk/api/staticfeeds/5.0/incidents";
http = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
http.Open("POST","https://datafeeds.nationalrail.co.uk/authenticate",false);
http.SetRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.Send("username="+user+"&password="+pass);
json = http.ResponseText;
eval('auth='+json);
http = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
http.Open("GET",feed,false);
http.SetRequestHeader("X-Auth-Token",auth.token);
http.Send();
WSH.Echo(http.ResponseText);


Edit YOUR_USERNAME and YOUR_PASSWORD and save as incidents.js and then from a command prompt:

cscript /NoLogo incidents.js

...which will output to the console - do this first to test and then to redirect output to an XML file:

cscript /NoLogo incidents.js > incidents.xml

Cheers

TheDoc

unread,
Jul 25, 2017, 5:25:19 PM7/25/17
to A gathering place for the Open Rail Data community
Hiya,

Thanks so much for the script you supplied that's perfect for what I wanted and can't thank you enough for your help with this. 

I can now work with the XML file perfectly :) 

Hopefully as I learn more I will be able to give back to this group over time as well

Thanks again for your help !!

Regards
Al
Reply all
Reply to author
Forward
0 new messages