This page must use the Rübÿ Spëëd Mëtäl Cödïng template!!!111
This commit is contained in:
parent
478e595f0c
commit
045236affd
|
@ -1,21 +1,16 @@
|
|||
=Telnet-Haltestellenmonitor=
|
||||
|
||||
<pre>#!/usr/bin/env ruby
|
||||
|
||||
require 'net/http'
|
||||
require 'socket'
|
||||
require 'rexml/document'
|
||||
|
||||
class MultipleStations < RuntimeError
|
||||
def initialize(stations)
|
||||
@stations = stations
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Mehrere mögliche Haltestellen: " + @stations.join(', ')
|
||||
end
|
||||
end
|
||||
|
||||
class StationResult
|
||||
def initialize(card)
|
||||
strong_n = 0
|
||||
|
@ -26,16 +21,13 @@ class StationResult
|
|||
when 1
|
||||
@time = e.text
|
||||
end
|
||||
|
||||
strong_n += 1
|
||||
}
|
||||
|
||||
@trams = []
|
||||
card.to_s.scan(/br\/>(\d+:\d+\*?) (.+?)<br\/>-> (.+?)</) { |time,tram,direction|
|
||||
@trams << [time, tram, direction]
|
||||
}
|
||||
end
|
||||
|
||||
def to_s
|
||||
column_widths = [4, 5, 4]
|
||||
@trams.each { |a|
|
||||
|
@ -43,17 +35,13 @@ class StationResult
|
|||
column_widths[i] = b.size if b.size > column_widths[i]
|
||||
}
|
||||
}
|
||||
|
||||
"\n\n#{@name}, #{@time}:\n\n" +
|
||||
|
||||
'Zeit'.ljust(column_widths[0]) + ' | ' +
|
||||
'Linie'.ljust(column_widths[1]) + ' | ' +
|
||||
'Ziel'.ljust(column_widths[2]) + "\n" +
|
||||
|
||||
('-' * column_widths[0]) + '-+-' +
|
||||
('-' * column_widths[1]) + '-+-' +
|
||||
('-' * column_widths[2]) + "\n" +
|
||||
|
||||
@trams.collect { |time,tram,direction|
|
||||
time.ljust(column_widths[0]) + ' | ' +
|
||||
tram.ljust(column_widths[1]) + ' | ' +
|
||||
|
@ -61,12 +49,10 @@ class StationResult
|
|||
}.join("\n")
|
||||
end
|
||||
end
|
||||
|
||||
class ClientHandler
|
||||
def initialize(socket)
|
||||
@socket = socket
|
||||
puts "#{address} connected"
|
||||
|
||||
Thread.new {
|
||||
begin
|
||||
handle
|
||||
|
@ -77,7 +63,6 @@ class ClientHandler
|
|||
end
|
||||
}
|
||||
end
|
||||
|
||||
def address
|
||||
if @socket.peeraddr[0] == 'AF_INET6'
|
||||
"[#{@socket.peeraddr[3]}]"
|
||||
|
@ -86,7 +71,6 @@ class ClientHandler
|
|||
end +
|
||||
":#{@socket.peeraddr[1]}"
|
||||
end
|
||||
|
||||
def ask_haltestellenmonitor(station)
|
||||
param = { :station => station,
|
||||
:action => :check,
|
||||
|
@ -98,7 +82,6 @@ class ClientHandler
|
|||
res = Net::HTTP.start('wap.dvbag.de') { |http|
|
||||
http.get('/wapVVO/wap-rbl.php?' + param_s)
|
||||
}
|
||||
|
||||
if res.kind_of? Net::HTTPSuccess
|
||||
wml = REXML::Document.new(res.body).root
|
||||
card = nil
|
||||
|
@ -109,7 +92,6 @@ class ClientHandler
|
|||
card.each_element('p/select/option') { |option|
|
||||
stations << option.text
|
||||
}
|
||||
|
||||
raise MultipleStations.new(stations)
|
||||
elsif card.attributes['id'] == 'result'
|
||||
StationResult.new(card).to_s
|
||||
|
@ -123,31 +105,27 @@ class ClientHandler
|
|||
raise "#{res.class}"
|
||||
end
|
||||
end
|
||||
|
||||
def handle
|
||||
@socket.print "Hallo #{address}\n\nHaltestelle: "
|
||||
@socket.flush
|
||||
|
||||
haltestelle = @socket.gets
|
||||
if haltestelle
|
||||
haltestelle.strip!
|
||||
|
||||
puts "#{address} asks for #{haltestelle.inspect}"
|
||||
@socket.puts "Anfrage nach #{haltestelle}..."
|
||||
|
||||
@socket.puts ask_haltestellenmonitor(haltestelle)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
serv = TCPServer.new('0.0.0.0', 65023)
|
||||
while client = serv.accept
|
||||
ClientHandler.new(client)
|
||||
end
|
||||
</pre>
|
||||
|
||||
Und dann:
|
||||
telnet localhost 65023
|
||||
|
||||
|
||||
[[Kategorie:Ruby]]
|
||||
|
||||
|
||||
|
||||
{{Rübÿ Spëëd Mëtäl Cödïng}}
|
||||
|
|
Loading…
Reference in New Issue