From 818942b8724e6de62318da80e7c3c3971b8a30a5 Mon Sep 17 00:00:00 2001 From: Enrique Vidal Date: Mon, 27 Aug 2012 22:44:42 -0700 Subject: [PATCH] Add earthquakes script --- src/scripts/quakes.coffee | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/scripts/quakes.coffee diff --git a/src/scripts/quakes.coffee b/src/scripts/quakes.coffee new file mode 100644 index 000000000..5852bf43a --- /dev/null +++ b/src/scripts/quakes.coffee @@ -0,0 +1,43 @@ +# Description: +# Ask hubot about the recent earthquakes in the last (hour, day, week or month). +# +# Dependencies: +# None +# +# Configuration: +# None +# +# Commands: +# hubot quakes (intensity|all|significant) (period) [limit] +# +# Author: +# EnriqueVidal + +lookup_site = "http://earthquake.usgs.gov" + +module.exports = (robot)-> + robot.respond /quakes (([12](\.[05])?)|all|significant)? (hour|day|week|month)( \d+)?$/i, (message)-> + check_for_rapture message, message.match[1], message.match[4], parseInt( message.match[5] ) + + check_for_rapture = (message, intensity, period, limit)-> + rapture_url = [ lookup_site, "earthquakes", "feed", "geojson", intensity, period ].join '/' + + message.http( rapture_url ).get() (error, response, body)-> + return message.send 'Sorry, something went wrong' if error + + list = JSON.parse( body ).features + count = 0 + + for quake in list + count++ + quake = quake.properties + time = build_time quake + url = [ lookup_site, quake.url ].join '' + + message.send "Magnitude: #{ quake.mag }, Location: #{ quake.place }, Time: #{ time } - #{ url }" + + break if count is limit + + build_time = ( object )-> + time = new Date object.time * 1000 + [ time.getHours(), time.getMinutes(), time.getSeconds() ].join ':'