summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2011-05-11 19:53:37 +0200
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2011-05-11 19:53:37 +0200
commitd11e04603795fefc322ab90fc3f6abe4c1d10d8f (patch)
tree2caeee5157b596d98dd33f053e4e632f43e07958
parent7d2f6b2aa00632f55df231c72cec7661af8845f1 (diff)
weather: fall back from nws to wu
If a user uses NWS as default service and asks for a location that the our NWS implementation can't retrieve, fall back to the Weather Underground service. To simplify this management, rewrite the code to use the bot HTTP util and its caching functionality.
-rw-r--r--data/rbot/plugins/weather.rb49
1 files changed, 25 insertions, 24 deletions
diff --git a/data/rbot/plugins/weather.rb b/data/rbot/plugins/weather.rb
index cda68e10..cb4617f1 100644
--- a/data/rbot/plugins/weather.rb
+++ b/data/rbot/plugins/weather.rb
@@ -15,35 +15,33 @@ require 'rexml/document'
# Wraps NOAA National Weather Service information
class CurrentConditions
+ @@bot = Irc::Utils.bot
def initialize(station)
@station = station
@url = "http://www.nws.noaa.gov/data/current_obs/#{URI.encode @station.upcase}.xml"
- @etag = String.new
- @mtime = Time.mktime(0)
@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
@@ -161,6 +159,9 @@ class WeatherPlugin < Plugin
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 +170,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 +193,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