Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 46 additions & 30 deletions src/scripts/weather.coffee
Original file line number Diff line number Diff line change
@@ -1,59 +1,75 @@
# Get some weather action!
# Returns weather information from Google
#
# weather <city> - Get the weather for a location
# forecast <city> - Get the forecast for a location
JsDom = require 'jsdom'
jsdom = require 'jsdom'
env = process.env

module.exports = (robot) ->
robot.respond /forecast(?: me)?\s(.*)/, (msg) ->
robot.respond /forecast(?: me|for|in)?\s(.*)/, (msg) ->
query msg, (body, err) ->
return msg.send err if err

city = body.getElementsByTagName("city")[0]
return msg.send "Sorry, but I couldn't find that location." if not city or not city.getAttribute

city = city.getAttribute("data")

strings = []
for element in body.getElementsByTagName('forecast_conditions')
day = element.getElementsByTagName('day_of_week')[0].getAttribute('data');
low = element.getElementsByTagName('low')[0].getAttribute('data');
high = element.getElementsByTagName('high')[0].getAttribute('data');
condition = element.getElementsByTagName('condition')[0].getAttribute('data');
strings.push "#{day} #{condition} high of: #{convertTemp(high)} low of: #{convertTemp(low)}"

strings.push "The forecast for #{city} is as follows:\n"
for element in body.getElementsByTagName("forecast_conditions")
day = element.getElementsByTagName("day_of_week")[0].getAttribute("data")

if env.HUBOT_WEATHER_CELSIUS
low = convertTempToCelsius(element.getElementsByTagName("low")[0].getAttribute("data")) + "ºC"
else
low = element.getElementsByTagName("low")[0].getAttribute("data") + "ºF"

if env.HUBOT_WEATHER_CELSIUS
high = convertTempToCelsius(element.getElementsByTagName("high")[0].getAttribute("data")) + "ºC"
else
high = element.getElementsByTagName("high")[0].getAttribute("data") + "ºF"

condition = element.getElementsByTagName("condition")[0].getAttribute("data")
strings.push "#{day}: #{condition} with a high of #{high} and a low of #{low}."

msg.send strings.join "\n"

robot.respond /weather(?: me)?\s(.*)/, (msg) ->
robot.respond /weather(?: me|for|in)?\s(.*)/, (msg) ->
query msg, (body, err) ->
return msg.send err if err

city = body.getElementsByTagName('city')[0];
return msg.send 'No city -> no weather.' if not city or not city.getAttribute

strings = []
strings.push "Weather for #{city.getAttribute('data')}"
currentCondition = body.getElementsByTagName('current_conditions')[0];
conditions = currentCondition.getElementsByTagName('condition')[0];
temp = currentCondition.getElementsByTagName('temp_c')[0];
humidity = currentCondition.getElementsByTagName('humidity')[0];
city = body.getElementsByTagName("city")[0]
return msg.send "Sorry, but you didn't specify a location." if not city or not city.getAttribute

city = city.getAttribute("data")
currentCondition = body.getElementsByTagName("current_conditions")[0].getAttribute("data")
conditions = body.getElementsByTagName("current_conditions")[0].getElementsByTagName("condition")[0].getAttribute("data")
humidity = body.getElementsByTagName("current_conditions")[0].getElementsByTagName("humidity")[0].getAttribute("data").split(' ')[1]

strings.push("Current conditions: #{conditions.getAttribute('data')} " +
"#{temp.getAttribute('data')}ºc")

strings.push humidity.getAttribute('data')
msg.send strings.join "\n"
if env.HUBOT_WEATHER_CELSIUS
temp = body.getElementsByTagName("current_conditions")[0].getElementsByTagName("temp_c")[0].getAttribute("data") + "ºC"
else
temp = body.getElementsByTagName("current_conditions")[0].getElementsByTagName("temp_f")[0].getAttribute("data") + "ºF"

msg.send "Currently in #{city} it is #{conditions} and #{temp} with a humidity of #{humidity}.\n"

getDom = (xml) ->
body = JsDom.jsdom(xml)
throw Error('No xml') if body.getElementsByTagName('weather')[0].childNodes.length == 0
body = jsdom.jsdom(xml)
throw Error("No XML data returned.") if body.getElementsByTagName("weather")[0].childNodes.length == 0
body

convertTemp = (faren) ->
((5 / 9) * (faren - 32)).toFixed 0
convertTempToCelsius = (f) ->
((5 / 9) * (f - 32)).toFixed 0

query = (msg, cb) ->
location = msg.match[1]
msg.http('http://www.google.com/ig/api')
msg.http("http://www.google.com/ig/api")
.query(weather: location)
.get() (err, res, body) ->
try
body = getDom body
catch err
err = 'Could not fetch weather data :('
err = "Could not fetch weather data."
cb(body, err)