neue variante hinzugefügt

This commit is contained in:
Frink 2008-02-29 14:28:33 +00:00
parent 8222fa6aa5
commit fdd83a30a8
1 changed files with 113 additions and 0 deletions

View File

@ -131,6 +131,119 @@ end
Und dann:
telnet localhost 65023
=NCurses Monitor=
Der VVO stellt jetzt ja für seine Widgets eine JSON(?)-Variante der Daten zur Verfügung.
<source lang="ruby">
require 'net/http'
require 'ncurses'
class DvbAbfahrt
def initialize
@BASEURI = "http://widgets.vvo-online.de/abfahrtsmonitor/Abfahrten.do?ort=ORT&hst=HST&vz=VZ"
end
def fetch(ort, hst, vz=0)
# TODO exceptionhandling: timeout
vz = vz.to_s
ort = URI.escape ort
hst = URI.escape hst
uri = @BASEURI.gsub(/ORT/, ort).gsub(/HST/,hst).gsub(/VZ/, vz)
res = Net::HTTP.get(URI.parse(uri))
res = umlauts_hack res
arr = res.scan(/(\d+),([^,]{1,}),(\d+)/)
end
def umlauts_hack(s)
repl = [
["&quot;", ''],
["&#252;", "ü"],
["&#246;", "ö"],
["&#223;", "ß"]
]
repl.each do |r|
s.gsub!(r[0], r[1])
end
return s
end
end
class Monitor
def initialize(ort, hst, vz=0)
Ncurses::initscr
Ncurses::start_color
Ncurses::init_pair(1, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK);
Ncurses::attron(Ncurses::COLOR_PAIR(1));
Ncurses::curs_set(0)
Ncurses::move(0,0)
Ncurses::printw "Loading..."
Ncurses::refresh
@sep = "|"
@timeout = 30
@wlno = 3
@wlname = 20
@weta = 3
@ort = ort
@hst = hst
@vz = vz
@dvb = DvbAbfahrt.new
@lines = 0
end
def print_title
Ncurses::attron(Ncurses::A_REVERSE | Ncurses::A_BOLD);
Ncurses::mvprintw(0,0, @hst.center(@wlno+@wlname+@weta+2)) # +2 seperators
Ncurses::attroff(Ncurses::A_REVERSE | Ncurses::A_BOLD);
end
def mainloop
print_title
while true
info = @dvb.fetch(@ort, @hst, @vz)
# clearing old lines if needed
if @lines > info.size
Ncurses::clear
print_title
end
@lines = info.size
info.each_index do |i|
Ncurses::mvprintw(i+1, 0, info[i][0].rjust(@wlno) + @sep + info[i][1][0..@wlname].center(@wlname) + @sep + info[i][2].rjust(@weta))
end
Ncurses::refresh
Ncurses::move(0,0)
sleep @timeout
end
end
end
if __FILE__ == $0
begin
if !ARGV.empty?
m = Monitor.new("Dresden", ARGV[0])
m.mainloop
else
puts "USAGE #{$0} <HALTESTELLENNAME>"
end
ensure
Ncurses::attroff(Ncurses::COLOR_PAIR(1));
Ncurses::endwin
end
end
</source>
[[Kategorie:Ruby]]