]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/tube.rb
4e6e60e0e16ae54aed71e0f5b7312e774bd77e9a
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / tube.rb
1 #Tube Status Enquiry plugin for rbot
2 #Plugin by Colm Linehan
3
4 class TubePlugin < Plugin
5   include REXML
6   def help(plugin, topic="")
7   "tube [district|circle|metropolitan|central|jubilee|bakerloo|waterlooandcity|hammersmithandcity|victoria|eastlondon|northern|piccadilly] => display tube service status for the specified line(Docklands Light Railway is not currently supported)" # , tube stations => list tube stations (not lines) with problems"
8   end
9   
10   def tube(m, params)
11     line = params[:line]
12     tube_page = @bot.httputil.get('http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html')
13     unless tube_page
14       m.reply "Cannot contact Tube Service Status page"
15       return
16     end
17     next_line = false
18     tube_page.each_line {|l|
19       next if l == "\r\n"
20       next if l == "\n"
21       if (next_line)
22         if (l =~ /^<p>\s*(.*)<\/p>/i)
23           m.reply $1.split(/<[^>]+>|&nbsp;/i).join(" ")
24           return
25         elsif l =~ /ul|h3|"message"/
26           next
27         else
28           m.reply "There are problems on the #{line} line, but I didn't understand the page format. You should check out http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html for more details."
29           return
30         end
31       end
32       next_line = true if (l =~ /li class="#{line}"/i)
33     }
34     m.reply "No Problems on the #{line} line."
35   end
36
37   def check_stations(m, params)
38     tube_page = @bot.httputil.get('http://www.tfl.gov.uk/tfl/service_rt_tube.shtml')
39     unless tube_page
40       m.reply "Cannot contact Tube Service Status page"
41       return
42     end
43     stations_array = Array.new
44     tube_page.each_line {|l|
45       if (l =~ /<tr valign=top> <td valign="middle" class="Station"><b>(.*)<\/b><\/td><\/tr>\s*/i)
46         stations_array.push $1
47       end
48     }
49     if stations_array.empty? 
50       m.reply "There are no station-specific announcements"
51       return
52     else
53       m.reply stations_array.join(", ")
54       return
55     end
56   end
57 end
58 plugin = TubePlugin.new
59 # plugin.map 'tube stations', :action => 'check_stations'
60 plugin.map 'tube :line'