]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/tube.rb
853167184533ffe8330f701850ac15cb6704870f
[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|waterloo_city|hammersmith_city|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   begin
16     tube_page = @bot.httputil.get(URI.parse("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml"), 1, 1)
17   rescue URI::InvalidURIError, URI::BadURIError => e
18     m.reply "Cannot contact Tube Service Status page"
19     return
20   end
21   unless tube_page
22     m.reply "Cannot contact Tube Service Status page"
23     return
24   end
25   next_line = false
26   tube_page.each_line {|l|
27     next if l == "\r\n"
28     next if l == "\n"
29     if (next_line)
30       if (l =~ /^<tr valign=top> <td>\s*(.*)<\/td><\/tr>/i)
31         m.reply $1.split(/<[^>]+>|&nbsp;/i).join(" ")
32         return
33       else
34         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/service_rt_tube.shtml for more details."
35         return
36       end
37     end
38     next_line = true if (l =~ /class="#{line}"/i)
39     }
40   m.reply "No Problems on the #{line} line."
41   end
42
43   def check_stations(m, params)
44     begin
45       tube_page = @bot.httputil.get(URI.parse("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml"))
46     rescue URI::InvalidURIError, URI::BadURIError => e
47       m.reply "Cannot contact Tube Service Status page"
48       return
49     end
50     unless tube_page
51       m.reply "Cannot contact Tube Service Status page"
52       return
53     end
54     stations_array = Array.new
55     tube_page.each_line {|l|
56         if (l =~ /<tr valign=top> <td valign="middle" class="Station"><b>(.*)<\/b><\/td><\/tr>\s*/i)
57           stations_array.push $1
58         end
59         }
60     if stations_array.empty? 
61       m.reply "There are no station-specific announcements"
62       return
63     else
64       m.reply stations_array.join(", ")
65       return
66     end
67   end
68 end
69 plugin = TubePlugin.new
70 plugin.map 'tube stations', :action => 'check_stations'
71 plugin.map 'tube :line'