88 lines
1.8 KiB
JavaScript
88 lines
1.8 KiB
JavaScript
const jsonfile = require('jsonfile');
|
|
const https = require('https');
|
|
const Sugar = require('sugar-date');
|
|
|
|
const file = 'data/data.json';
|
|
|
|
function resultsQuery(callback) {
|
|
const options = {
|
|
'host': 'euromillions.p.mashape.com',
|
|
'path': '/ResultsService/FindLast',
|
|
|
|
'headers': {
|
|
'accept': 'text/plain',
|
|
'X-Mashape-Key': '5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4'
|
|
|
|
},
|
|
'method': 'GET'
|
|
};
|
|
|
|
https.request(options).on('response', function (response) {
|
|
let data = '';
|
|
response.on('data', function (chunk) {
|
|
data += chunk;
|
|
});
|
|
response.on('end', function () {
|
|
// console.log(data);
|
|
callback(JSON.parse(data));
|
|
});
|
|
}).end();
|
|
}
|
|
|
|
function processDataV2(data) {
|
|
const newArray = [];
|
|
const date = /(\/Date\()([0-9]+)([\s\S]+)/.exec(data.Date);
|
|
|
|
// console.log(data);
|
|
|
|
// date = /(\/Date\()([0-9]+)([\s\S]+)/.exec(data.Date);
|
|
const newdate = Sugar.Date.format(new Date(parseInt(date[2])), '%Y-%m-%d');
|
|
|
|
newArray.push(newdate);
|
|
|
|
newArray.push(data.Num1);
|
|
newArray.push(data.Num2);
|
|
newArray.push(data.Num3);
|
|
newArray.push(data.Num4);
|
|
newArray.push(data.Num5);
|
|
|
|
newArray.push(data.Star1);
|
|
newArray.push(data.Star2);
|
|
|
|
return newArray;
|
|
}
|
|
|
|
function loadV2(cb) {
|
|
const lotData = jsonfile.readFileSync(file);
|
|
cb(lotData);
|
|
}
|
|
|
|
function load(cb) {
|
|
jsonfile.readFile(file, (err, obj) => {
|
|
if (err) console.error(err);
|
|
|
|
const lotData = obj;
|
|
|
|
resultsQuery( (a) => {
|
|
const latest = processDataV2(a);
|
|
|
|
const dateExists = lotData.filter((item) => {
|
|
if (item[0] === latest[0]) return item;
|
|
});
|
|
|
|
if (dateExists.length === 0)
|
|
lotData.unshift(latest);
|
|
|
|
// return the data
|
|
cb(lotData);
|
|
});
|
|
});
|
|
}
|
|
|
|
function save(data) {
|
|
jsonfile.writeFileSync(file, data);
|
|
}
|
|
|
|
module.exports.load = loadV2;
|
|
module.exports.save = save;
|