Created
December 25, 2015 00:47
-
-
Save daniellevass/94de1337311ef36d013e to your computer and use it in GitHub Desktop.
my script to collect data from google's santa tracker
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 dateFormat = require('dateformat'); | |
var fs = require('fs'); | |
var colors = require('colors'); | |
var destinations;//our final list of destinations | |
//function to get data from google - set on a timeout at the bottom to refresh it | |
function getData(){ | |
//standard headings | |
var options = { | |
method: "GET", | |
url: 'https://santa-api.appspot.com/info?client=web&language=en&fingerprint=&routeOffset=0&streamOffset=0', | |
headers: { | |
'User-Agent': 'nodejs script 1' | |
} | |
}; | |
//completion function | |
function callback(error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var result = JSON.parse(body); | |
//if we didn't have any destinations just set the array to it | |
if (! destinations) { | |
destinations = result.destinations; | |
//display the destination to the terminal | |
for (var i in result.destinations){ | |
var d = result.destinations[i]; | |
console.log(dateFormat(d.arrival, "hh:MM") + " - " + | |
dateFormat(d.departure, "hh:MM") + " - " + | |
d.region + " - " + d.city); | |
} | |
console.log(colors.yellow(destinations.length + " results")); | |
} else { | |
//check if we had any new rows | |
if (result.destinations.length == destinations.length ) { | |
//no new rows :( | |
console.log(colors.grey(destinations.length + " results - no new rows")); | |
} else { | |
//how many do we have to add? | |
var numberRowsToAdd = result.destinations.length - destinations.length; | |
//add those | |
for(var i = (result.destinations.length - numberRowsToAdd); i < result.destinations.length; i++){ | |
var d = result.destinations[i]; | |
destinations.push(d); | |
//all display them plz | |
console.log(dateFormat(d.arrival, "hh:MM") + " - " + | |
dateFormat(d.departure, "hh:MM") + " - " + | |
d.region + " - " + d.city); | |
} | |
//suffient new data | |
writeFile(); | |
console.log(colors.red(destinations.length + " results - "+ numberRowsToAdd + " new rows")); | |
}//end if number of records is different | |
}//end if we didn't have any records | |
} else { | |
//ERROR! | |
console.log(response); | |
} | |
} | |
//send request. | |
request(options, callback); | |
//refresh every 30 secs | |
setTimeout(getData, 30000); | |
} | |
//write to a file - it'll overwrite a file with the same minute | |
//this means tht when google clear all their data, hopefully i'll have almost the right number | |
function writeFile(){ | |
var filename = dateFormat(new Date(), "HH-MM"); | |
fs.writeFile("files/christmas_"+filename+".JSON", JSON.stringify(destinations), function(err) { | |
if(err) { | |
return console.log(err); | |
} | |
console.log("The file was saved!"); | |
}); | |
} | |
//start my script running! | |
getData(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment