Permalink
Browse files

Create darksky.coffee

Adds a script to get the current weather and forecast from the Dark Sky API
  • Loading branch information...
1 parent 64d78a6 commit 8acc74ec653caa29bad07a7ae4e1591d12b57b54 @kyleslattery kyleslattery committed Jan 10, 2013
Showing with 54 additions and 0 deletions.
  1. +54 −0 src/scripts/darksky.coffee
View
@@ -0,0 +1,54 @@
+# Description
+# Grabs the current forecast from Dark Sky
+#
+# Dependencies
+# None
+#
+# Configuration
+# HUBOT_DARK_SKY_API_KEY
+# HUBOT_DARK_SKY_DEFAULT_LOCATION
+#
+# Commands:
+# hubot weather - Get the weather for HUBOT_DARK_SKY_DEFAULT_LOCATION
+# hubot weather <location> - Get the weather for <location>
+#
+# Notes:
+# If HUBOT_DARK_SKY_DEFAULT_LOCATION is blank, weather commands without a location will be ignored
+#
+# Author:
+# kyleslattery
+module.exports = (robot) ->
+ robot.respond /weather ?(.+)?/i, (msg) ->
+ location = msg.match[1] || process.env.HUBOT_DARK_SKY_DEFAULT_LOCATION
+ return if not location
+
+ googleurl = "http://maps.googleapis.com/maps/api/geocode/json"
+ q = sensor: false, address: location
+ msg.http(googleurl)
+ .query(q)
+ .get() (err, res, body) ->
+ result = JSON.parse(body)
+
+ if result.results.length > 0
+ lat = result.results[0].geometry.location.lat
+ lng = result.results[0].geometry.location.lng
+ darkSkyMe msg, lat,lng , (darkSkyText) ->
+ response = "Weather for #{result.results[0].formatted_address}\n#{darkSkyText}"
+ msg.send response
+ else
+ msg.send "Couldn't find #{location}"
+
+darkSkyMe = (msg, lat, lng, cb) ->
+ url = "https://api.darkskyapp.com/v1/brief_forecast/#{process.env.HUBOT_DARK_SKY_API_KEY}/#{lat},#{lng}/"
+ msg.http(url)
+ .get() (err, res, body) ->
+ result = JSON.parse(body)
+
+ if result.error
+ cb "#{result.error}"
+ return
+
+ response = "Currently: #{result.currentSummary} (#{result.currentTemp}F)"
+ response += "\nNext hour: #{result.hourSummary}"
+ response += "\nToday: #{result.daySummary}"
+ cb response

0 comments on commit 8acc74e

Please sign in to comment.