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