Created
January 3, 2017 02:20
-
-
Save muthuspark/ffecc4d212e336c385b524068c001305 to your computer and use it in GitHub Desktop.
To Find the best time to leave my office. The coordinates are the routes from my office to home. The code runs and collects time to reach at regular intervals of 7 mins and stores. I will analyze the data later
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var schedule = require('node-schedule'); | |
var firebase = require('firebase'); | |
var app = firebase.initializeApp({ | |
apiKey: "AIzaSyB1mr1VzndsFlKzNLHLlVtMKmOTXaUAM94", | |
authDomain: "squirreltask.firebaseapp.com", | |
databaseURL: "https://squirreltask.firebaseio.com", | |
storageBucket: "squirreltask.appspot.com", | |
messagingSenderId: "302372771732" | |
}); | |
var database = firebase.database(); | |
firebase.auth().signInAnonymously().catch(function(error) { | |
// Handle Errors here. | |
var errorCode = error.code; | |
var errorMessage = error.message; | |
// ... | |
}); | |
var locations = [ | |
'12.913704,77.644740|12.927480,77.620914|12.936606,77.611073|12.954702,77.605513|12.960039,77.601708|12.965381,77.601113|12.968031,77.591337|12.970194,77.591711|12.974727,77.590886|12.977551,77.590950', | |
'12.927480,77.620914|12.936606,77.611073|12.954702,77.605513|12.960039,77.601708|12.965381,77.601113|12.968031,77.591337|12.970194,77.591711|12.974727,77.590886|12.977551,77.590950|13.0811293,77.5370976' | |
]; | |
var j = schedule.scheduleJob('*/7 * * * *', function(){ | |
console.log('finding the awesomeness in you master Muthu!'); | |
var url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins='+locations[0]+'&destinations='+locations[1]+'&mode=driving&key=AIzaSyC18Oc8zP-Wv_XRrmw4Qllsy12Fk0ukowI'; | |
request(url, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var res = JSON.parse(body); | |
var totald = 0; | |
var totalt = 0; | |
var j = 0; | |
res.rows.forEach(function(k){ | |
totald += k.elements[j].distance.value; | |
totalt += k.elements[j].duration.value; | |
j++; | |
}); | |
// console.log(totald+','+totalt); | |
var d = new Date(); | |
var dstr = d.toISOString(); | |
var mat = { | |
datestr: dstr, | |
distance: totald, | |
time: totalt | |
} | |
firebase.database().ref('distancematrix/' + d.getTime()).set(mat); | |
// console.log(res.rows[0].elements) | |
} | |
}) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment