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

76 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-12-06 01:20:43 +00:00
// Get CORPUS data from Network Rail and format the data for OwlBoard
// Username and password must be stored in `/srv/keys/owlboard/keys.config.js`
2022-12-08 10:46:29 +00:00
// 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 from the data.
2022-12-06 13:40:03 +00:00
const keys = require('/srv/keys/owlboard/keys.configs');
const axios = require('axios');
2022-12-07 01:28:00 +00:00
const zlib = require('zlib');
2022-12-06 13:40:03 +00:00
2022-12-08 11:03:33 +00:00
async function initCorpus() {
2022-12-08 10:46:29 +00:00
let gzipData = await getCorpusAgain();
console.log(gzipData);
}
2022-12-08 11:03:33 +00:00
async function getCorpus() {
2022-12-06 13:40:03 +00:00
authHead = Buffer.from(`${keys.nr_user}:${keys.nr_pass}`).toString('base64');
2022-12-08 10:46:29 +00:00
const options = {
2022-12-06 13:40:03 +00:00
method: 'get',
baseURL: 'https://datafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS',
timeout: 3000,
headers: {'Authorization': `Basic ${authHead}`},
responseType: 'arraybuffer'
2022-12-08 10:46:29 +00:00
};
let download = axios.get(options);
console.log(download);
2022-12-06 13:40:03 +00:00
}
2022-12-06 01:20:43 +00:00
2022-12-08 11:03:33 +00:00
async function getCorpusAgain() {
2022-12-07 14:23:25 +00:00
authHead = Buffer.from(`${keys.nr_user}:${keys.nr_pass}`).toString('base64');
const decomp = zlib.createGunzip();
const getReq = axios.create({
method: 'get',
baseURL: 'https://datafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS',
timeout: 3000,
headers: {'Authorization': `Basic ${authHead}`},
responseType: 'arraybuffer'
});
let download = await getReq().then(function (response){
return response.data;
})
zlib.gunzip(download, function (_err, output){
let string = output.toString()
let obj = JSON.parse(string)
//console.log(obj);
return obj; // Find out how to get data out of callback -> See pinned tab on laptop2.ws
})
}
2022-12-08 11:03:33 +00:00
async function init() {
}
async function get() {
}
async function extract() {
}
async function clean() {
2022-12-06 01:20:43 +00:00
}
module.exports = {
2022-12-07 14:23:25 +00:00
initCorpus,
2022-12-08 10:46:29 +00:00
getCorpusAgain,
getCorpus
2022-12-06 01:20:43 +00:00
}