summaryrefslogtreecommitdiff
path: root/data/rbot/plugins
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-10-25 16:12:20 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-10-25 16:12:20 +0000
commite610893f36c4f965489374c4d8b97da3175186cf (patch)
tree02e044094139e13452d25decb6e4ff20e6122ef7 /data/rbot/plugins
parent87bba678599323e8bd66ea63ac54c6bcb70ccf07 (diff)
new weather plugin that uses weatherunderground.com mobile interface. can be called with station code or location. doesn't remember last location (yet)
Diffstat (limited to 'data/rbot/plugins')
-rw-r--r--data/rbot/plugins/wuweather.rb100
1 files changed, 100 insertions, 0 deletions
diff --git a/data/rbot/plugins/wuweather.rb b/data/rbot/plugins/wuweather.rb
new file mode 100644
index 00000000..d02e9a63
--- /dev/null
+++ b/data/rbot/plugins/wuweather.rb
@@ -0,0 +1,100 @@
+# Weather plugin for rbot
+# Copyright (C) 2006 Giuseppe Bilotta
+#
+# Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
+
+require 'uri'
+
+class WuWeatherPlugin < Plugin
+
+ def help(plugin, topic="")
+ "weather <location> => display the current conditions at the location specified; you can use 'station <code>' to look up data by station code ( lookup your station code at http://www.weatherunderground.com/ )"
+ end
+
+ def initialize
+ super
+ @url="http://mobile.wunderground.com/cgi-bin/findweather/getForecast?brand=mobile&query=%s"
+ @station_url="http://mobile.wunderground.com/global/stations/%s.html"
+ end
+
+ def station(m, params)
+ where = params[:where]
+ begin
+ unless where
+ m.reply "I don't know where you are, #{m.sourcenick} (and you can't set it yet)"
+ end
+ xml = @bot.httputil.get_cached(@station_url % URI.escape(where))
+ case xml
+ when nil
+ m.reply "couldn't retrieve weather information, sorry"
+ return
+ when /<table border.*?>(.*?)<\/table>/m
+ data = $1
+ m.reply weather_filter(data)
+ else
+ debug xml
+ m.reply "something went wrong with the data for #{where}..."
+ end
+ rescue => e
+ m.reply "retrieving info about '#{where}' failed (#{e})"
+ end
+ end
+
+ def weather(m, params)
+ where = params[:where].to_s
+ begin
+ if where.empty?
+ m.reply "I don't know where you are, #{m.sourcenick} (and you can't set it yet)"
+ end
+ xml = @bot.httputil.get_cached(@url % URI.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 weather_filter(data)
+ 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(", ")
+ else
+ debug xml
+ m.reply "something went wrong with the data from #{where}..."
+ end
+ rescue => e
+ m.reply "retrieving info about '#{where}' failed (#{e})"
+ end
+ end
+
+ def weather_filter(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?\/?>/,'')
+
+ result = Array.new
+ if txt.match(/<\/a>\s*Updated:\s*(.*?)\s*Observed at\s*(.*?)\s*<\/td>/)
+ result << ("Weather info for %s (updated on %s)" % [$2, $1])
+ end
+ txt.scan(/<tr>\s*<td>\s*(.*?)\s*<\/td>\s*<td>\s*(.*?)\s*<\/td>\s*<\/tr>/) { |k, v|
+ unless v.empty? or v == "-" or k =="Raw METAR"
+ result << ("%s: %s" % [k, v])
+ end
+ }
+ return result.join('; ')
+ end
+
+end
+
+plugin = WuWeatherPlugin.new
+plugin.map 'weather station :where', :action => 'station', :defaults => {:where => false}
+plugin.map 'weather *where', :defaults => {:where => false}
+