#!/usr/bin/env node var jsonfile = require('jsonfile'), https = require('https'), UltraSES = require('ultrases'),cron = require('node-cron'); var Sugar = require('sugar-date'); var file = 'data/data.json'; var m = [], prev = [], mo = [], star = [], trip = [], results = [], line = []; var re = []; var resultsObj = {}; var mailer = new UltraSES({ aws: { accessKeyId: 'AKIAJWJS75F7WNCGK64A', secretAccessKey: '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', "region": "eu-west-1" }, defaults: { from: 'Martin Donnelly ' } }); function $(elm) { return document.getElementById(elm); } function resultsQuery(callback) { /* var options = { host: 'nunofcguerreiro.com', path: '/api-euromillions-json', port: 443, headers: { accept: '*!/!*' /!* 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data)*!/ }, method: 'GET' };*/ var options = { host: 'euromillions.p.mashape.com', path: '/ResultsService/FindLast', //port: 443, headers: { accept: "text/plain", "X-Mashape-Key": "5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4" /* 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data)*/ }, method: 'GET' }; /* unirest.get("https://euromillions.p.mashape.com/ResultsService/FindLast") .header("X-Mashape-Key", "5A0H980jK6mshSFL24ZmfiRrNHV2p1d1fhQjsngtx8QWuO9oe4") .header("Accept", "text/plain") .end(function (result) { console.log(result.status, result.headers, result.body); }); */ https.request(options).on('response', function (response) { var data = ''; response.on("data", function (chunk) { data += chunk; }); response.on('end', function () { //console.log(data); callback(JSON.parse(data)); }); }).end(); } function sortNumber(a, b) { return a - b; } function dynamicSort(property) { var sortOrder = 1; if (property[0] === "-") { sortOrder = -1; property = property.substr(1); } return function (a, b) { var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; return result * sortOrder; } } function buildArray() { for (var x = 0; x < 51; x++) { var l = []; for (var y = 0; y < 51; y++) { l.push(0); } m[x] = l; } } function buildTable() { var area = $('area'); // clean area. /* while (area.lastChild) { area.removeChild(area.lastChild); } */ var table = document.createElement('table'); var row = document.createElement('tr'); var cell = document.createElement('td'); var p = document.createTextNode('---') cell.appendChild(p); row.appendChild(cell); for (var x = 1; x < 50; x++) { var cell = document.createElement('td'); var p = document.createTextNode(x) cell.appendChild(p); row.appendChild(cell); } table.appendChild(row); /** * */ for (var y = 0; y < 51; y++) { var row = document.createElement('tr'); var cell = document.createElement('td'); var p = document.createTextNode('[' + parseInt(y + 1) + ']') cell.appendChild(p); row.appendChild(cell); for (var x = 1; x < 51; x++) { var cell = document.createElement('td'); var p = document.createTextNode(m[y][x - 1]) cell.appendChild(p); row.appendChild(cell); } table.appendChild(row); } area.appendChild(table); } function calc() { var i = 0; for (i = 0; i < re.length - 1; i++) { console.log(re[i]); for (var s = 1; s < 6; s++) { var cv = re[i][s]; console.log("row: " + cv); for (var n = 1; n < 6; n++) { if (n != s) { wv = re[i][n]; console.log(wv); m[cv - 1][wv - 1]++; } } } } buildTable(); } function othercalc() { var i = 0; // var p = 1 / ((-1)*re.length); var p = 1.0 / (re.length); console.log('P', p); for (i = 0; i < re.length - 1; i++) { console.log((p * i)); // console.log(re[i]); for (var s = 1; s < 6; s++) { var cv = re[i][s]; // console.log("row: " + cv); for (var n = 1; n < 6; n++) { if (n != s) { wv = re[i][n]; // console.log(wv); // m[cv-1][wv-1]++; var flag = false; for (var t = 0; t < mo.length; t++) { if (mo[t].a == cv && mo[t].b == wv) { if (typeof mo[t].c == 'undefined' || mo[t].c == null) { mo[t].c = 0; } mo[t].c++; flag = true; // mo[t].v++; /* if (i < (re.length/2)) mo[t].v = mo[t].v - (p * i) else */ // original weighting //mo[t].v = mo[t].v + (p * i); //reverse weighting.. mo[t].v = mo[t].v + (1-(p * i)); console.log(mo[t], 1-(p*i)); //; //mo[t].v = mo[t].v + 1; // ; } } if (!flag) { mo.push({ a: cv, b: wv, v: 1 }); } } } } } console.log("******************************************"); mo.sort(dynamicSort("v")); //for (var t = (mo.length - 20); t < mo.length; t++) { var t = mo.length - 1; while (line.length < 5) { if (line.indexOf(mo[t].a) == -1 && line.length < 5) { line.push(mo[t].a); } if (line.indexOf(mo[t].b) == -1 && line.length < 5) { line.push(mo[t].b); } t = t - 1; } // console.log(line); console.log(line.sort()); resultsObj.mainline = line.sort(); resultsObj.otherLine = []; resultsObj.otherLine.push(mo[mo.length - 1].a); resultsObj.otherLine.push(mo[mo.length - 1].b); for (var t = mo.length - 1; t > (mo.length - 3); t--) { // if (mo[t].v >= 3) { if (mo[t].v > 1) { console.log(mo[t].a + ', ' + mo[t].b + ", " + mo[t].v) } } // buildTable(); } function tripCalc() { var blist = [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]; var p = 1.0 / (re.length); var a, b, c, cur; //var trip = []; //console.log for (i = 0; i < re.length - 1; i++) { for (var t = 0; t < blist.length; t++) { cur = blist[t]; a = re[i][cur[0] + 1]; b = re[i][cur[1] + 1]; c = re[i][cur[2] + 1]; // console.log(a + ", " + b + "," + c); var found = false; for (y = 0; y < trip.length; y++) { var nums = trip[y].nums; if (!found) { if ((nums.indexOf(a) != -1) && (nums.indexOf(b) != -1) && (nums.indexOf(c) != -1)) { /* console.log("nums:" + nums); console.log("want: " + a + ", " + b + "," + c); console.log("A:" + nums.indexOf(a)); console.log("B:" + nums.indexOf(b)); console.log("C:" + nums.indexOf(c)); */ trip[y].cnt = trip[y].cnt + (p * i); // trip[y].cnt = trip[y].cnt + 1; // console.log(">>>" + trip[y].cnt); found = true; } } } if (!found) { //trip.push({nums:[a,b,c],cnt:(p * i)}); trip.push({ nums: [a, b, c], cnt: 1 }); } } } var max = 0; for (y = 0; y < trip.length; y++) { if (trip[y].cnt > max) max = trip[y].cnt; } console.log("Max: " + max); for (y = 0; y < trip.length; y++) { if (trip[y].cnt == max) { console.log(trip[y].cnt + ", " + trip[y].nums); resultsObj.otherLine = resultsObj.otherLine.concat(trip[y].nums).sort(); } } // console.log(JSON.stringify(trip)); } function starcalc() { var i = 0; var p = 1 / re.length; for (i = 0; i < re.length - 1; i++) { // console.log(re[i]); for (var s = 6; s < 8; s++) { var cv = re[i][s]; // console.log("row: " + cv); for (var n = 6; n < 8; n++) { if (n != s) { wv = re[i][n]; // console.log(wv); // m[cv-1][wv-1]++; var flag = false; for (var t = 0; t < mo.length; t++) { if (mo[t].a == cv && mo[t].b == wv) { flag = true; mo[t].v++; mo[t].v = mo[t].v + (p * i); } } if (!flag) { mo.push({ a: cv, b: wv, v: 1 }); } } } } } console.log("******************************************"); mo.sort(dynamicSort("v")); var t = mo.length - 1; var starOutput = []; starOutput.push(mo[t].a); starOutput.push(mo[t].b); resultsObj.mainstars = []; resultsObj.mainstars.push(starOutput.sort()[0]); resultsObj.mainstars.push(starOutput.sort()[1]); resultsObj.otherStars = []; resultsObj.otherStars.push(mo[mo.length - 3].a); resultsObj.otherStars.push(mo[mo.length - 3].b); //console.log('Stars: ' + mo[t].a + ', ' + mo[t].b); console.log('Stars: ' + starOutput.sort()[0] + ', ' + starOutput.sort()[1]); for (var t = mo.length - 6; t < mo.length; t++) { if (mo[t].v >= 3) { console.log(mo[t].a + ', ' + mo[t].b + ", " + mo[t].v) } } // buildTable(); } function performCalcs() { buildArray(); //buildTable(); othercalc(); console.log("---------------------------------------"); starcalc(); tripCalc(); } function prepareResults() { resultsObj.mainline = resultsObj.mainline.sort(sortNumber); resultsObj.otherLine = resultsObj.otherLine.sort(sortNumber); resultsObj.mainstars = resultsObj.mainstars.sort(sortNumber); resultsObj.otherStars = resultsObj.otherStars.sort(sortNumber); } function saveData() { jsonfile.writeFileSync(file, re); } function sendEmail() { var email = { to: 'martind2000@gmail.com', subject: 'Todays numbers' }; var template = { file: './jade/output.jade', locals: resultsObj }; mailer.sendTemplate(email, template, function(err){ if(err) throw err; console.log('compiled template email sent'); }); } function processData(data) { newArray.push(data.drawns[0].date); numbers = data.drawns[0].numbers.split(' '); for (var i = 0; i < numbers.length; ++i) { numbers[i] = parseInt(numbers[i]); } stars = data.drawns[0].stars.split(' '); for (var i = 0; i < stars.length; ++i) { stars[i] = parseInt(stars[i]); } newArray = newArray.concat(numbers); newArray = newArray.concat(stars); re.unshift(newArray); } function processDataV2(data) { var newArray = []; var numbers, stars, date ; console.log(data); date = /(\/Date\()([0-9]+)([\s\S]+)/.exec(data.Date); console.log(date[2]); date = date[2]; //date = parseInt(date) / 1000; console.log(new Date(date)); // date = Date.create(date.toString()); console.log(date); } function GO() { re = []; resultsObj = {}; jsonfile.readFile(file, function (err, obj) { console.log(err); re = obj; /* performCalcs(); prepareResults(); console.log(resultsObj); */ resultsQuery(function (a) { processData(a); performCalcs(); prepareResults(); console.log(resultsObj); sendEmail(); saveData(); }); }); } //saveData(); //GO(); //sendEmail(); cron.schedule('45 17 * * 2,5', function(){ GO(); // console.log('tick'); return -1; }); processDataV2({ Date: '/Date(1477008000000+0000)/', Jackpot: 39000000, NextJackpot: 49000000, Num1: 9, Num2: 20, Num3: 23, Num4: 44, Num5: 48, PrizeCombinations: [ { Numbers: 5, Prize: 0, Stars: 2, Winners: 0 }, { Numbers: 5, Prize: 408989.59, Stars: 1, Winners: 3 }, { Numbers: 5, Prize: 28577.5, Stars: 0, Winners: 10 }, { Numbers: 4, Prize: 3882.81, Stars: 2, Winners: 36 }, { Numbers: 4, Prize: 176.24, Stars: 1, Winners: 846 }, { Numbers: 3, Prize: 130.81, Stars: 2, Winners: 1591 }, { Numbers: 4, Prize: 65.36, Stars: 0, Winners: 1806 }, { Numbers: 2, Prize: 21.79, Stars: 2, Winners: 24943 }, { Numbers: 3, Prize: 15.99, Stars: 1, Winners: 35930 }, { Numbers: 3, Prize: 12.73, Stars: 0, Winners: 85437 }, { Numbers: 1, Prize: 11.99, Stars: 2, Winners: 128187 }, { Numbers: 2, Prize: 8.48, Stars: 1, Winners: 544209 }, { Numbers: 2, Prize: 4.4, Stars: 0, Winners: 1289660 } ], RaffleNumber: 0, Star1: 8, Star2: 10 } );