Fix a bug in quiz.rb listen()
[rbot] / data / rbot / plugins / wuweather.rb
1 # Weather plugin for rbot\r
2 # Copyright (C) 2006 Giuseppe Bilotta\r
3 #\r
4 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>\r
5 \r
6 require 'uri'\r
7 \r
8 class WuWeatherPlugin < Plugin\r
9   \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
12   end\r
13   \r
14   def initialize\r
15     super\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
18   end\r
19 \r
20   def station(m, params)\r
21     where = params[:where]\r
22     begin\r
23       unless where\r
24         m.reply "I don't know where you are, #{m.sourcenick} (and you can't set it yet)"\r
25       end\r
26       xml = @bot.httputil.get_cached(@station_url % URI.escape(where))\r
27       case xml\r
28       when nil\r
29         m.reply "couldn't retrieve weather information, sorry"\r
30         return\r
31       when /<table border.*?>(.*?)<\/table>/m\r
32         data = $1\r
33         m.reply weather_filter(data)\r
34       else\r
35         debug xml\r
36         m.reply "something went wrong with the data for #{where}..."\r
37       end\r
38     rescue => e\r
39       m.reply "retrieving info about '#{where}' failed (#{e})"\r
40     end\r
41   end\r
42 \r
43   def weather(m, params)\r
44     where = params[:where].to_s\r
45     begin\r
46       if where.empty?\r
47         m.reply "I don't know where you are, #{m.sourcenick} (and you can't set it yet)"\r
48       end\r
49       xml = @bot.httputil.get_cached(@url % URI.escape(where))\r
50       case xml\r
51       when nil\r
52         m.reply "couldn't retrieve weather information, sorry"\r
53         return\r
54       when /City Not Found/\r
55         m.reply "no such location found (#{where})"\r
56         return\r
57       when /<table border.*?>(.*?)<\/table>/m\r
58         data = $1\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
63       else\r
64         debug xml\r
65         m.reply "something went wrong with the data from #{where}..."\r
66       end\r
67     rescue => e\r
68       m.reply "retrieving info about '#{where}' failed (#{e})"\r
69     end\r
70   end\r
71 \r
72   def weather_filter(stuff)\r
73     txt = stuff\r
74     txt.gsub!(/[\n\s]+/,' ')\r
75     data = Hash.new\r
76     txt.gsub!(/&nbsp;/, ' ')\r
77     txt.gsub!(/&#176;/, ' ') # 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
82 \r
83     result = Array.new\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
86     end\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
90       end\r
91     }\r
92     return result.join('; ')\r
93   end\r
94 \r
95 end\r
96 \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
100 \r