forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
darksky.coffee
66 lines (60 loc) · 2.13 KB
/
darksky.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Description
# Grabs the current forecast from Dark Sky
#
# Dependencies
# None
#
# Configuration
# HUBOT_DARK_SKY_API_KEY
# HUBOT_DARK_SKY_DEFAULT_LOCATION
# HUBOT_DARK_SKY_UNITS (optional - us, si, ca, or uk)
#
# 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}. #{darkSkyText}"
msg.send response
else
msg.send "Couldn't find #{location}"
darkSkyMe = (msg, lat, lng, cb) ->
url = "https://api.forecast.io/forecast/#{process.env.HUBOT_DARK_SKY_API_KEY}/#{lat},#{lng}/"
if process.env.HUBOT_DARK_SKY_UNITS
url += "?units=#{process.env.HUBOT_DARK_SKY_UNITS}"
msg.http(url)
.get() (err, res, body) ->
result = JSON.parse(body)
if result.error
cb "#{result.error}"
return
isFahrenheit = process.env.HUBOT_DARK_SKY_UNITS == "us"
if isFahrenheit
fahrenheit = result.currently.temperature
celsius = (fahrenheit - 32) * (5 / 9)
else
celsius = result.currently.temperature
fahrenheit = celsius * (9 / 5) + 32
response = "Currently: #{result.currently.summary} (#{fahrenheit}°F/"
response += "#{celsius}°C). "
response += "Today: #{result.hourly.summary} "
response += "Coming week: #{result.daily.summary}"
cb response