+
+ # TODO allow units choice other than lang, find how the API does it
+ def google_weather(m, where)
+ botlang = @bot.config['core.language'].intern
+ if Language::Lang2Locale.key?(botlang)
+ lang = Language::Lang2Locale[botlang].sub(/.UTF.?8$/,'')
+ else
+ lang = botlang.to_s[0,2]
+ end
+
+ debug "Google weather with language #{lang}"
+ xml = @bot.httputil.get("http://www.google.com/ig/api?hl=#{lang}&weather=#{CGI.escape where}")
+ debug xml
+ weather = REXML::Document.new(xml).root.elements["weather"]
+ begin
+ error = weather.elements["problem_cause"]
+ if error
+ ermsg = error.attributes["data"]
+ ermsg = _("no reason specified") if ermsg.empty?
+ raise ermsg
+ end
+ city = weather.elements["forecast_information/city"].attributes["data"]
+ date = Time.parse(weather.elements["forecast_information/current_date_time"].attributes["data"])
+ units = weather.elements["forecast_information/unit_system"].attributes["data"].intern
+ current_conditions = weather.elements["current_conditions"]
+ foreconds = weather.elements.to_a("forecast_conditions")
+
+ conds = []
+ current_conditions.each { |el|
+ name = el.name.intern
+ value = el.attributes["data"].dup
+ debug [name, value]
+ case name
+ when :icon
+ next
+ when :temp_f
+ next if units == :SI
+ value << "°F"
+ when :temp_c
+ next if units == :US
+ value << "°C"
+ end
+ conds << value
+ }
+
+ forecasts = []
+ foreconds.each { |forecast|
+ cond = []
+ forecast.each { |el|
+ name = el.name.intern
+ value = el.attributes["data"]
+ case name
+ when :icon
+ next
+ when :high, :low
+ value << (units == :SI ? "°C" : "°F")
+ value << " |" if name == :low
+ when :condition
+ value = "(#{value})"
+ end
+ cond << value
+ }
+ forecasts << cond.join(' ')
+ }
+
+ m.reply _("Google weather info for %{city} on %{date}: %{conds}. Three-day forecast: %{forecast}") % {
+ :city => city,
+ :date => date,
+ :conds => conds.join(', '),
+ :forecast => forecasts.join('; ')
+ }
+ rescue => e
+ debug e
+ m.reply _("Google weather failed: %{e}") % { :e => e}
+ end
+
+ end
+