1 # Weather plugin for rbot
\r
2 # Copyright (C) 2006 Giuseppe Bilotta
\r
4 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
\r
8 class WuWeatherPlugin < Plugin
\r
10 def help(plugin, topic="")
\r
11 "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/ )"
\r
16 @url="http://mobile.wunderground.com/cgi-bin/findweather/getForecast?brand=mobile&query=%s"
\r
17 @station_url="http://mobile.wunderground.com/global/stations/%s.html"
\r
20 def station(m, params)
\r
21 where = params[:where]
\r
24 m.reply "I don't know where you are, #{m.sourcenick} (and you can't set it yet)"
\r
26 xml = @bot.httputil.get_cached(@station_url % URI.escape(where))
\r
29 m.reply "couldn't retrieve weather information, sorry"
\r
31 when /<table border.*?>(.*?)<\/table>/m
\r
33 m.reply weather_filter(data)
\r
36 m.reply "something went wrong with the data for #{where}..."
\r
39 m.reply "retrieving info about '#{where}' failed (#{e})"
\r
43 def weather(m, params)
\r
44 where = params[:where].to_s
\r
47 m.reply "I don't know where you are, #{m.sourcenick} (and you can't set it yet)"
\r
49 xml = @bot.httputil.get_cached(@url % URI.escape(where))
\r
52 m.reply "couldn't retrieve weather information, sorry"
\r
54 when /City Not Found/
\r
55 m.reply "no such location found (#{where})"
\r
57 when /<table border.*?>(.*?)<\/table>/m
\r
59 m.reply weather_filter(data)
\r
60 when /<a href="\/global\/stations\//
\r
61 stations = xml.scan(/<a href="\/global\/stations\/(.*?)\.html">/)
\r
62 m.reply "multiple stations available, use 'weather station <code>' where code is one of " + stations.join(", ")
\r
65 m.reply "something went wrong with the data from #{where}..."
\r
68 m.reply "retrieving info about '#{where}' failed (#{e})"
\r
72 def weather_filter(stuff)
\r
74 txt.gsub!(/[\n\s]+/,' ')
\r
76 txt.gsub!(/ /, ' ')
\r
77 txt.gsub!(/°/, ' ') # degree sign
\r
78 txt.gsub!(/<\/?b>/,'')
\r
79 txt.gsub!(/<\/?span[^<>]*?>/,'')
\r
80 txt.gsub!(/<img\s*[^<>]*?>/,'')
\r
81 txt.gsub!(/<br\s?\/?>/,'')
\r
84 if txt.match(/<\/a>\s*Updated:\s*(.*?)\s*Observed at\s*(.*?)\s*<\/td>/)
\r
85 result << ("Weather info for %s (updated on %s)" % [$2, $1])
\r
87 txt.scan(/<tr>\s*<td>\s*(.*?)\s*<\/td>\s*<td>\s*(.*?)\s*<\/td>\s*<\/tr>/) { |k, v|
\r
88 unless v.empty? or v == "-" or k =="Raw METAR"
\r
89 result << ("%s: %s" % [k, v])
\r
92 return result.join('; ')
\r
97 plugin = WuWeatherPlugin.new
\r
98 plugin.map 'weather station :where', :action => 'station', :defaults => {:where => false}
\r
99 plugin.map 'weather *where', :defaults => {:where => false}
\r