2022-12-06 01:20:43 +00:00
|
|
|
// Get CORPUS data from Network Rail and format the data for OwlBoard
|
|
|
|
|
2022-12-08 12:37:23 +00:00
|
|
|
// Network Rail Datafeed user and pass must be stored in `/srv/keys/owlboard/keys.config.js`
|
2022-12-06 01:20:43 +00:00
|
|
|
|
2022-12-08 16:39:13 +00:00
|
|
|
// FUNCTIONS/
|
2022-12-08 10:46:29 +00:00
|
|
|
// init() : Exported: Uses the internal functions to return a clean CORPUS file.
|
|
|
|
// get() : Get the CORPUS data from Network Rail as a gzip file.
|
|
|
|
// extract(): Extract the CORPUS JSON file from the GZIP file.
|
2022-12-08 16:39:13 +00:00
|
|
|
// clean() : Cleans the CORPUS data, removing unneccesary keys and objects from the data.
|
2022-12-08 10:46:29 +00:00
|
|
|
|
2022-12-08 16:39:13 +00:00
|
|
|
const keys = require('/srv/keys/owlboard/keys.configs')
|
|
|
|
const axios = require('axios')
|
|
|
|
const gz = require('node-gzip')
|
2022-12-06 13:40:03 +00:00
|
|
|
|
2022-12-08 15:21:59 +00:00
|
|
|
async function init() {
|
2022-12-08 16:39:13 +00:00
|
|
|
var gzipData = await get()
|
|
|
|
var corpus = await extract(gzipData)
|
|
|
|
var cleanCorpus = await clean(corpus)
|
|
|
|
return cleanCorpus
|
2022-12-07 13:22:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 15:21:59 +00:00
|
|
|
async function get() {
|
2022-12-08 16:39:13 +00:00
|
|
|
authHead = Buffer.from(`${keys.nr_user}:${keys.nr_pass}`).toString('base64')
|
2022-12-08 12:37:23 +00:00
|
|
|
const url = 'https://datafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS'
|
2022-12-08 10:46:29 +00:00
|
|
|
const options = {
|
2022-12-06 13:40:03 +00:00
|
|
|
method: 'get',
|
2022-12-08 15:21:59 +00:00
|
|
|
timeout: 20000,
|
2022-12-07 13:22:00 +00:00
|
|
|
headers: {'Authorization': `Basic ${authHead}`},
|
|
|
|
responseType: 'arraybuffer'
|
2022-12-08 16:39:13 +00:00
|
|
|
}
|
|
|
|
var { data } = await axios.get(url, options)
|
|
|
|
console.log(data)
|
|
|
|
return data
|
2022-12-07 14:23:25 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 15:21:59 +00:00
|
|
|
async function extract(input) {
|
2022-12-08 16:39:13 +00:00
|
|
|
var raw = await gz.ungzip(input)
|
|
|
|
var obj = await JSON.parse(raw)
|
|
|
|
var output = obj.TIPLOCDATA
|
|
|
|
return output
|
2022-12-08 11:03:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 16:39:13 +00:00
|
|
|
async function clean(input) {
|
|
|
|
let clean = [];
|
|
|
|
for (const element of input) {
|
|
|
|
if (element.STANOX != ' ' && element['3ALPHA'] != ' '){
|
|
|
|
delete(element.UIC);
|
|
|
|
delete(element.NLCDESC16);
|
|
|
|
delete(element.NLC);
|
|
|
|
clean.push(element);
|
|
|
|
}
|
2022-12-08 16:41:40 +00:00
|
|
|
}
|
2022-12-08 16:39:13 +00:00
|
|
|
return clean;
|
2022-12-06 01:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2022-12-08 12:37:23 +00:00
|
|
|
init
|
2022-12-06 01:20:43 +00:00
|
|
|
}
|