2015-04-25 18:37:13 +02:00
|
|
|
# Description:
|
|
|
|
# utilità per il meteo
|
|
|
|
#
|
|
|
|
# Dependencies:
|
2015-04-25 18:58:11 +02:00
|
|
|
# "moment":"2.10.2"
|
2015-04-25 18:37:13 +02:00
|
|
|
#
|
|
|
|
# Configuration:
|
|
|
|
# None
|
|
|
|
#
|
|
|
|
# Commands:
|
2015-08-24 03:47:55 +02:00
|
|
|
# hubot meteo/che tempo fa/c'è a <luogo>? - ottieni il meteo
|
2015-04-25 18:37:13 +02:00
|
|
|
#
|
|
|
|
# Author:
|
|
|
|
# Enrico Fasoli (fazo96)
|
2015-08-24 03:47:55 +02:00
|
|
|
# Michele Guerini Rocco (rnhmjoj)
|
2015-04-25 18:37:13 +02:00
|
|
|
|
2015-04-25 18:58:11 +02:00
|
|
|
moment = require 'moment'
|
|
|
|
|
2015-08-24 03:47:55 +02:00
|
|
|
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'
|
|
|
|
|
2015-04-25 18:37:13 +02:00
|
|
|
module.exports = (robot) ->
|
2015-08-24 03:47:55 +02:00
|
|
|
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) ->
|
|
|
|
city = (JSON.parse body).city
|
|
|
|
return res.send if err or not city?
|
|
|
|
console.log 'found ' + city
|
|
|
|
callback city
|
|
|
|
|
|
|
|
send_weather = (city) ->
|
|
|
|
robot.http(url2 + city).get() (err, r, body) ->
|
|
|
|
return res.send "#{err2}\n #{err}" if err
|
|
|
|
|
2015-04-25 18:58:11 +02:00
|
|
|
try
|
|
|
|
body = JSON.parse body
|
|
|
|
catch e
|
2015-08-24 03:47:55 +02:00
|
|
|
return res.send "#{err2}: #{e}"
|
2015-04-28 19:52:29 +02:00
|
|
|
unless body?.sys? and body?.main? and body?.weather?.push?
|
2015-08-24 03:47:55 +02:00
|
|
|
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
|
|
|
|
|