X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=data%2Frbot%2Fplugins%2Fweather.rb;h=8f59a9ba693eea6f98c61a3f42d69fe53f4a29a2;hb=052217de30c59206d7025b582d4604557a747470;hp=d8f7579c95c45cda0ce9d93480b164f4e260d116;hpb=783ffa4235330029d661752b1023db635b26f2b3;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git
diff --git a/data/rbot/plugins/weather.rb b/data/rbot/plugins/weather.rb
index d8f7579c..8f59a9ba 100644
--- a/data/rbot/plugins/weather.rb
+++ b/data/rbot/plugins/weather.rb
@@ -35,9 +35,9 @@ class CurrentConditions
end
rescue OpenURI::HTTPError => e
case e
- when /304/:
+ when /304/
@iscached = true
- when /404/:
+ when /404/
raise "Data for #{@station} not found"
else
raise "Error retrieving data: #{e}"
@@ -71,6 +71,11 @@ class WeatherPlugin < Plugin
Config.register Config::BooleanValue.new('weather.advisory',
:default => true,
:desc => "Should the bot report special weather advisories when any is present?")
+ Config.register Config::EnumValue.new('weather.units',
+ :values => ['metric', 'english', 'both'],
+ :default => 'both',
+ :desc => "Units to be used by default in Weather Underground reports")
+
def help(plugin, topic="")
case topic
@@ -93,59 +98,57 @@ class WeatherPlugin < Plugin
end
def weather(m, params)
- if params[:where].empty?
- if @registry.has_key?(m.sourcenick)
- where = @registry[m.sourcenick]
- debug "Loaded weather info #{where.inspect} for #{m.sourcenick}"
-
- service = where.first.to_sym
- loc = where[1].to_s
- units = params[:units] || where[2] rescue nil
- else
- debug "No weather info for #{m.sourcenick}"
- m.reply "I don't know where you are yet, #{m.sourcenick}. See 'help weather nws' or 'help weather wu' for additional help"
- return
- end
- else
- where = params[:where]
- if ['nws','station'].include?(where.first)
- service = where.first.to_sym
- loc = where[1].to_s
+ where = params[:where].to_s
+ service = params[:service].to_sym rescue nil
+ units = params[:units]
+
+ if where.empty? or !service or !units and @registry.has_key?(m.sourcenick)
+ reg = @registry[m.sourcenick]
+ debug "loaded weather info #{reg.inspect} for #{m.sourcenick}"
+ service = reg.first.to_sym if !service
+ where = reg[1].to_s if where.empty?
+ units = reg[2] rescue nil
+ end
+
+ if !service
+ if where.sub!(/^station\s+/,'')
+ service = :nws
else
service = :wu
- loc = where.to_s
end
- units = params[:units]
end
- if loc.empty?
+ if where.empty?
debug "No weather location found for #{m.sourcenick}"
m.reply "I don't know where you are yet, #{m.sourcenick}. See 'help weather nws' or 'help weather wu' for additional help"
return
end
wu_units = String.new
- if units
- case units.to_sym
- when :english, :metric
- wu_units = "_#{units}"
- when :both
- else
- m.reply "Ignoring unknown units #{units}"
- wu_units = String.new
- end
+
+ case (units || @bot.config['weather.units']).to_sym
+ when :english, :metric
+ wu_units = "_#{units}"
+ when :both
+ else
+ m.reply "Ignoring unknown units #{units}"
end
case service
when :nws
- nws_describe(m, loc)
+ nws_describe(m, where)
when :station
- wu_station(m, loc, wu_units)
+ wu_station(m, where, wu_units)
when :wu
- wu_weather(m, loc, wu_units)
+ wu_weather(m, where, wu_units)
+ when :google
+ google_weather(m, where)
+ else
+ m.reply "I don't know the weather service #{service}, sorry"
+ return
end
- @registry[m.sourcenick] = [service, loc, units]
+ @registry[m.sourcenick] = [service, where, units]
end
def nws_describe(m, where)
@@ -219,18 +222,6 @@ class WeatherPlugin < Plugin
end
end
- def wu_clean(stuff)
- txt = stuff
- txt.gsub!(/[\n\s]+/,' ')
- txt.gsub!(/ /, ' ')
- txt.gsub!(/°/, ' ') # degree sign
- txt.gsub!(/<\/?b>/,'')
- txt.gsub!(/<\/?span[^<>]*?>/,'')
- txt.gsub!(/]*?>/,'')
- txt.gsub!(/
/,'')
- txt
- end
-
def wu_weather_multi(m, xml)
# debug xml
stations = xml.scan(/