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

65 lines
1.9 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/
// initSubset() : Exported: Uses the internal functions to return a clean CORPUS object.
// initAll() : Exported: Uses the internal functions to return a full CORPUS object.
// 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 non-stations from the data.
const axios = require('axios')
const gz = require('node-gzip')
const corpusUser = process.env.OWL_LDB_CORPUSUSER
const corpusPass = process.env.OWL_LDB_CORPUSPASS
async function initSubset(allCorpus) {
var cleanCorpus = await clean(allCorpus)
return cleanCorpus
}
async function initAll(){
var gzipData = await get()
var allCorpus = await extract(gzipData)
return allCorpus
}
async function get() {
authHead = Buffer.from(`${corpusUser}:${corpusPass}`).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)
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 = {
initAll,
initSubset
}