]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/tube.rb
* (plugins/ri) acknoledge the '!ri tell (whom)' command
[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 require 'rexml/document'
5 require 'uri/common'
6
7 class TubePlugin < Plugin
8   include REXML
9   def help(plugin, topic="")
10   "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"
11   end
12   
13   def tube(m, params)
14     line = params[:line]
15     tube_page = @bot.httputil.get('http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html')
16     unless tube_page
17       m.reply "Cannot contact Tube Service Status page"
18       return
19     end
20     next_line = false
21     tube_page.each_line {|l|
22       next if l == "\r\n"
23       next if l == "\n"
24       if (next_line)
25         if (l =~ /^<p>\s*(.*)<\/p>/i)
26           m.reply $1.split(/<[^>]+>|&nbsp;/i).join(" ")
27           return
28         elsif l =~ /ul|h3|"message"/
29           next
30         else
31           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."
32           return
33         end
34       end
35       next_line = true if (l =~ /li class="#{line}"/i)
36     }
37     m.reply "No Problems on the #{line} line."
38   end
39
40   def check_stations(m, params)
41     tube_page = @bot.httputil.get('http://www.tfl.gov.uk/tfl/service_rt_tube.shtml')
42     unless tube_page
43       m.reply "Cannot contact Tube Service Status page"
44       return
45     end
46     stations_array = Array.new
47     tube_page.each_line {|l|
48       if (l =~ /<tr valign=top> <td valign="middle" class="Station"><b>(.*)<\/b><\/td><\/tr>\s*/i)
49         stations_array.push $1
50       end
51     }
52     if stations_array.empty? 
53       m.reply "There are no station-specific announcements"
54       return
55     else
56       m.reply stations_array.join(", ")
57       return
58     end
59   end
60 end
61 plugin = TubePlugin.new
62 # plugin.map 'tube stations', :action => 'check_stations'
63 plugin.map 'tube :line'