]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/weather.rb
weather plugin: refactor HTML cleanup code
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / weather.rb
index 88194e9067ca5ace3c15c60d23d8c9d4f029a95a..c241daa832f0e8a97f7fd317592478006aca8c0a 100644 (file)
@@ -11,7 +11,6 @@
 #
 # License:: GPL v2
 
-require 'uri'
 require 'rexml/document'
 
 # Wraps NOAA National Weather Service information
@@ -165,7 +164,7 @@ class WeatherPlugin < Plugin
 
   def wu_station(m, where, units)
     begin
-      xml = @bot.httputil.get_cached(@wu_station_url % [units, URI.escape(where)])
+      xml = @bot.httputil.get(@wu_station_url % [units, CGI.escape(where)])
       case xml
       when nil
         m.reply "couldn't retrieve weather information, sorry"
@@ -187,17 +186,22 @@ class WeatherPlugin < Plugin
 
   def wu_weather(m, where, units)
     begin
-      xml = @bot.httputil.get_cached(@wu_url % [units, URI.escape(where)])
+      xml = @bot.httputil.get(@wu_url % [units, CGI.escape(where)])
       case xml
       when nil
         m.reply "couldn't retrieve weather information, sorry"
-        return
       when /City Not Found/
         m.reply "no such location found (#{where})"
-        return
-      when /<table border.*?>(.*?)<\/table>/m
-        data = $1
-        m.reply wu_weather_filter(data)
+      when /Current<\/a>/
+        data = ""
+        xml.scan(/<table border.*?>(.*?)<\/table>/m).each do |match|
+          data += wu_weather_filter(match.first)
+        end
+        if data.length > 0
+          m.reply data
+        else
+          m.reply "couldn't parse weather data from #{where}"
+        end
       when /<a href="\/global\/stations\//
         stations = xml.scan(/<a href="\/global\/stations\/(.*?)\.html">/)
         m.reply "multiple stations available, use 'weather station <code>' where code is one of " + stations.join(", ")
@@ -210,16 +214,20 @@ class WeatherPlugin < Plugin
     end
   end
 
-  def wu_weather_filter(stuff)
+  def wu_clean(stuff)
     txt = stuff
     txt.gsub!(/[\n\s]+/,' ')
-    data = Hash.new
     txt.gsub!(/&nbsp;/, ' ')
     txt.gsub!(/&#176;/, ' ') # degree sign
     txt.gsub!(/<\/?b>/,'')
     txt.gsub!(/<\/?span[^<>]*?>/,'')
     txt.gsub!(/<img\s*[^<>]*?>/,'')
     txt.gsub!(/<br\s?\/?>/,'')
+    txt
+  end
+
+  def wu_weather_filter(stuff)
+    txt = wu_clean(stuff)
 
     result = Array.new
     if txt.match(/<\/a>\s*Updated:\s*(.*?)\s*Observed at\s*(.*?)\s*<\/td>/)