# Description: # utilità per il meteo # # Dependencies: # "moment":"2.10.2" # # Configuration: # None # # Commands: # hubot meteo/che tempo fa/c'è a ? - ottieni il meteo # # Author: # Enrico Fasoli (fazo96) # Michele Guerini Rocco (rnhmjoj) moment = require 'moment' url1 = 'http://ip-api.com/json/' url2 = 'http://api.openweathermap.org/data/2.5/weather?lang=it&units=metric&q=' err1 = 'non so dove sono: chiedimi un posto in particolare' err2 = 'errore nel guardare il cielo' module.exports = (robot) -> robot.respond /(?:(?:d(?:a|i)mmi il )?meteo(?: per (.+))?|che tempo (?:fa|c'è)(?: (?:a|in) (.+))?)\??$/i, (res) -> find_location = (ip, callback) -> robot.http(url1 + ip).get() (err, r, body) -> try city = JSON.parse(body).city catch e # Fallback callback 'Crema' return if err or !city? or city is undefined # Fallback callback 'Crema' return callback city send_weather = (city) -> robot.http(url2 + city).get() (err, r, body) -> return res.send "#{err2}\n #{err}" if err try body = JSON.parse body catch e return res.send "#{err2}: #{e}" unless body?.sys? and body?.main? and body?.weather?.push? return res.send "#{err2}:\n#{JSON.stringify body}" dawn = (moment.unix body.sys.sunrise).format 'H:MM' dusk = (moment.unix body.sys.sunset ).format 'H:MM' res.send "meteo per #{body.name}:\n #{Math.round body.main.temp} °C, #{body.weather[0].description} con #{body.main.humidity}% di umidità. l'alba è alle #{dawn} mentre il tramonto alle #{dusk}" city = res.match[1] || res.match[2] if not city? find_location '', send_weather else send_weather city