This repository has been archived on 2023-08-24. You can view files and clone it, but cannot push or open issues or pull requests.
OwlBoard/src/utils/corpus.utils.js

58 lines
1.7 KiB
JavaScript

// Get CORPUS data from Network Rail and format the data for OwlBoard
// Network Rail Datafeed user and pass must be stored in `/srv/keys/owlboard/keys.config.js`
// FUNCTIONS/
// 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.
// clean() : Cleans the CORPUS data, removing unneccesary keys and objects from the data.
const keys = require('/srv/keys/owlboard/keys.configs')
const axios = require('axios')
const gz = require('node-gzip')
async function init() {
var gzipData = await get()
var corpus = await extract(gzipData)
var cleanCorpus = await clean(corpus)
return cleanCorpus
}
async function get() {
authHead = Buffer.from(`${keys.nr_user}:${keys.nr_pass}`).toString('base64')
const url = 'https://datafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS'
const options = {
method: 'get',
timeout: 20000,
headers: {'Authorization': `Basic ${authHead}`},
responseType: 'arraybuffer'
}
var { data } = await axios.get(url, options)
console.log(data)
return data
}
async function extract(input) {
var raw = await gz.ungzip(input)
var obj = await JSON.parse(raw)
var output = obj.TIPLOCDATA
return output
}
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);
}
}
return clean;
}
module.exports = {
init
}