Update script to openweathermap api change

This commit is contained in:
rnhmjoj 2016-03-19 15:44:42 +01:00
parent c4dc0f2d27
commit ca944bd41b

View File

@ -5,7 +5,7 @@
# "moment":"2.10.2" # "moment":"2.10.2"
# #
# Configuration: # Configuration:
# None # WEATHER_API_KEY
# #
# Commands: # Commands:
# hubot meteo/che tempo fa/c'è a <luogo>? - ottieni il meteo # hubot meteo/che tempo fa/c'è a <luogo>? - ottieni il meteo
@ -16,49 +16,57 @@
moment = require 'moment' moment = require 'moment'
regex = /(?:(?:d(?:a|i)mmi il )?meteo(?: per (.+))?|che tempo (?:fa|c'è)(?: (?:a|in) (.+))?)\??$/i
url1 = 'http://ip-api.com/json/' url1 = 'http://ip-api.com/json/'
url2 = 'http://api.openweathermap.org/data/2.5/weather?lang=it&units=metric&q=' url2 = 'http://api.openweathermap.org/data/2.5/weather?lang=it&units=metric'
err1 = 'non so dove sono: chiedimi un posto in particolare' err1 = 'non so dove sono: chiedimi un posto in particolare'
err2 = 'errore nel guardare il cielo' err2 = 'errore nel guardare il cielo'
module.exports = (robot) -> module.exports = (robot) ->
robot.respond /(?:(?:d(?:a|i)mmi il )?meteo(?: per (.+))?|che tempo (?:fa|c'è)(?: (?:a|in) (.+))?)\??$/i, (res) -> get_location = (callback) ->
find_location = (ip, callback) -> robot.http(url1).get() (err, r, body) ->
robot.http(url1 + ip).get() (err, r, body) -> try
try city = JSON.parse(body).city
city = JSON.parse(body).city catch e
catch e # Fallback
# Fallback callback 'Crema'
callback 'Crema' return
return if err or !city? or city is undefined
if err or !city? or city is undefined # Fallback
# Fallback callback 'Crema'
callback 'Crema' return
return callback city
callback city
send_weather = (city) -> get_weather = (query, callback) ->
robot.http(url2 + city).get() (err, r, body) -> key = process.env.WEATHER_API_KEY
return res.send "#{err2}\n #{err}" if err if not key
try return callback 'non ho la chiave per openweathermap :('
body = JSON.parse body robot.http(url2 + '&appid='+key + '&q='+query).get() (err, r, body) ->
catch e callback err, body
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' send_weather = (res) -> (err, body) ->
dusk = (moment.unix body.sys.sunset ).format 'H:MM' 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}"
res.send "meteo per #{body.name}:\n dawn = (moment.unix body.sys.sunrise).format 'H:MM'
#{Math.round body.main.temp} °C, #{body.weather[0].description} con dusk = (moment.unix body.sys.sunset ).format 'H:MM'
#{body.main.humidity}% di umidità. l'alba è alle #{dawn} mentre il
tramonto alle #{dusk}"
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}"
robot.respond regex, (res) ->
city = res.match[1] || res.match[2] city = res.match[1] || res.match[2]
if city?
if not city? get_weather city, send_weather res
find_location '', send_weather else
else send_weather city get_location (location) ->
get_weather location, send_weather res