Created
May 27, 2012 01:07
-
-
Save zpao/2795778 to your computer and use it in GitHub Desktop.
quick little node script to show growl alerts when the ground shakes
This file contains 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 csv = require("csv"); | |
var growl = require("growl"); | |
const URL = "http://earthquake.usgs.gov/earthquakes/catalogs/eqs1hour-M0.txt"; | |
const INTERVAL = 20000; | |
// this is what the keys map to | |
var keys = ['Src', 'Eqid', 'Version', 'Datetime', 'Lat', 'Lon', 'Magnitude', 'Depth', 'NST', 'Region']; | |
var quakes = {}; | |
function notifyNewQuake(quake) { | |
var title = "New Quake Detected"; | |
var msg = quake.Region + ": " + quake.Magnitude; | |
console.log(title, msg, buildQuakeUrl(quake)); | |
growl(msg, { title: title }); | |
} | |
function notifyUpdatedQuake(quake, oldQuake) { | |
var title = "Updated Quake Info"; | |
var msg = "Updated " + oldQuake.Region + ": " + oldQuake.Magnitude + " to " + quake.Magnitude; | |
console.log(title, msg, buildQuakeUrl(quake)); | |
growl(msg, { title: title }); | |
} | |
function buildQuakeUrl(quake) { | |
return "http://earthquake.usgs.gov/earthquakes/recenteqsus/Quakes/" + quake.Src + quake.Eqid + ".html"; | |
} | |
function parseQuakeData(data) { | |
var parsedData = {}; | |
data.forEach(function(d, i) { | |
parsedData[keys[i]] = d; | |
}); | |
return parsedData; | |
} | |
function handleQuakeData(data) { | |
var quake = parseQuakeData(data); | |
// if we already have this quake, look to see if we should update the data | |
// otherwise it's a new quake | |
if (quakes[quake.Eqid]) { | |
// console.log("got existing quake"); | |
var oldQuake = quakes[quake.Eqid]; | |
// check to see if it's a different version | |
if (oldQuake.Version != quake.Version) { | |
notifyUpdatedQuake(quake, oldQuake); | |
quakes[quake.Eqid] = quake; | |
} | |
} | |
else { | |
quakes[quake.Eqid] = quake; | |
notifyNewQuake(quake); | |
} | |
} | |
function getQuakeData() { | |
var currentQuakes = {}; | |
var c = csv().fromStream(request(URL)); | |
c.on("data", function(data, index) { | |
if (index > 0 && data) | |
handleQuakeData(data); | |
}); | |
} | |
// call getQuakeData once to seed, then follow on an interval | |
getQuakeData(); | |
setInterval(getQuakeData, INTERVAL); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment