Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add earthquakes script

  • Loading branch information...
commit 818942b8724e6de62318da80e7c3c3971b8a30a5 1 parent de4993b
Enrique Vidal EnriqueVidal authored
Showing with 43 additions and 0 deletions.
  1. +43 −0 src/scripts/quakes.coffee
43 src/scripts/quakes.coffee
View
@@ -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 ':'
Please sign in to comment.
Something went wrong with that request. Please try again.