X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fweather.rb;h=7c1336f0d017dfd62655bb2d4d859e7a563bd649;hb=e5ff70eece675e05e6cdd1e2740f1f7a1316c697;hp=0d3abee27071cfc61de190ba0c47ef7dc7f92da2;hpb=29f36f867923faccc8e99863e059ee4a36b279b3;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/weather.rb b/data/rbot/plugins/weather.rb index 0d3abee2..7c1336f0 100644 --- a/data/rbot/plugins/weather.rb +++ b/data/rbot/plugins/weather.rb @@ -15,51 +15,55 @@ require 'rexml/document' # Wraps NOAA National Weather Service information class CurrentConditions - def initialize(station) + def initialize(station, bot) @station = station - @url = "http://www.nws.noaa.gov/data/current_obs/#{@station.upcase}.xml" - @etag = String.new - @mtime = Time.mktime(0) + @bot = bot + @url = "http://www.nws.noaa.gov/data/current_obs/#{URI.encode @station.upcase}.xml" @current_conditions = String.new - @iscached = false end def update - begin - open(@url,"If-Modified-Since" => @mtime.rfc2822) do |feed| - # open(@url,"If-None-Match"=>@etag) do |feed| - @etag = feed.meta['etag'] - @mtime = feed.last_modified - cc_doc = (REXML::Document.new feed).root - @iscached = false - @current_conditions = parse(cc_doc) - end - rescue OpenURI::HTTPError => e - case e - when /304/ - @iscached = true - when /404/ - raise "Data for #{@station} not found" - else - raise "Error retrieving data: #{e}" - end + begin + resp = @bot.httputil.get_response(@url) + case resp + when Net::HTTPSuccess + cc_doc = (REXML::Document.new resp.body).root + @current_conditions = parse(cc_doc) + else + raise Net::HTTPError.new(_("couldn't get data for %{station} (%{message})") % { + :station => @station, :message => resp.message + }, resp) + end + rescue => e + if Net::HTTPError === e + raise + else + error e + raise "error retrieving data: #{e}" end - @current_conditions # +" Cached? "+ ((@iscached) ? "Y" : "N") + end + @current_conditions end def parse(cc_doc) cc = Hash.new cc_doc.elements.each do |c| cc[c.name.to_sym] = c.text end - "At #{cc[:observation_time_rfc822]}, the wind was #{cc[:wind_string]} at #{cc[:location]} (#{cc[:station_id]}). The temperature was #{cc[:temperature_string]}#{heat_index_or_wind_chill(cc)}, and the pressure was #{cc[:pressure_string]}. The relative humidity was #{cc[:relative_humidity]}%. Current conditions are #{cc[:weather]} with #{cc[:visibility_mi]}mi visibility." + cc[:time] = cc[:observation_time_rfc822] + cc[:wind] = cc[:wind_string] + cc[:temperature] = cc[:temperature_string] + cc[:heatindexorwindchill] = heat_index_or_wind_chill(cc) + cc[:pressure] = cc[:pressure_string] + + _("At %{time} the conditions at %{location} (%{station_id}) were %{weather} with a visibility of %{visibility_mi}mi. The wind was %{wind} with %{relative_humidity}%% relative humidity. The temperature was %{temperature}%{heatindexorwindchill}, and the pressure was %{pressure}.") % cc end private def heat_index_or_wind_chill(cc) hi = cc[:heat_index_string] wc = cc[:windchill_string] - if hi != 'NA' then - " with a heat index of #{hi}" - elsif wc != 'NA' then - " with a windchill of #{wc}" + if hi and hi != 'NA' then + _(" with a heat index of %{hi}") % { :hi => hi } + elsif wc and wc != 'NA' then + _(" with a windchill of %{wc}") % { :wc => wc } else "" end @@ -126,7 +130,9 @@ class WeatherPlugin < Plugin wu_units = String.new - case (units || @bot.config['weather.units']).to_sym + units = @bot.config['weather.units'] unless units + + case units.to_sym when :english, :metric wu_units = "_#{units}" when :both @@ -155,12 +161,15 @@ class WeatherPlugin < Plugin if @nws_cache.has_key?(where) then met = @nws_cache[where] else - met = CurrentConditions.new(where) + met = CurrentConditions.new(where, @bot) end if met begin m.reply met.update @nws_cache[where] = met + rescue Net::HTTPError => e + m.reply _("%{error}, will try WU service") % { :error => e.message } + wu_weather(m, where) rescue => e m.reply e.message end @@ -169,7 +178,7 @@ class WeatherPlugin < Plugin end end - def wu_station(m, where, units) + def wu_station(m, where, units="") begin xml = @bot.httputil.get(@wu_station_url % [units, CGI.escape(where)]) case xml @@ -192,7 +201,7 @@ class WeatherPlugin < Plugin end end - def wu_weather(m, where, units) + def wu_weather(m, where, units="") begin xml = @bot.httputil.get(@wu_url % [units, CGI.escape(where)]) case xml