#!/usr/bin/ruby1.8 require 'rubygems' require 'mechanize' require 'iconv' require 'rss/1.0' require 'rss/2.0' require 'open-uri' def wrap(s) o="" until s.length<=16 p=s[0..16] ok=false 16.downto(0) do |i| if p[i..i]==" " o<
Ganzen Tag
"+@desc1+"
Morgens, Nachmittags
"+@desc1+"
Abends
"+@desc3+"
Morgens
"+@desc1+"
Nachmittags, Abends
"+@desc2+"
Morgens, Abends
"+@desc1+"
Nachmittags
"+@desc2+"
Morgens
"+@desc1+"
Nachmittags
"+@desc2+"
Abends
"+@desc3+"
Wetter "+@name+"
"+temp+"
" @result = Iconv.iconv("UTF-8", "ISO-8859-1", @result).to_s f=File.new(cache, "w") f.write @result f.close @result end end class BahnPage def initialize(station, arr_dep, time) @station = station @arr_dep = arr_dep @time = time end def get_page cache="bahn_cache" cachetime=Time.new-(2*60) if File.exist?(cache) and (cachetime<=>File.mtime(cache))==-1 # Cache is good, use it return IO.read(cache) end @url = "http://www.bahn.de/p/view/ankunft.shtml" a = WWW::Mechanize.new a.get(@url) do |page| results = page.form('formschnellsuche') do |f| f.field("input").value = @station f.field("boardType").value = @arr_dep f.checkboxes.each { |c| c.checked = (c.name=="GUIREQProduct_0") } f.field("time").value = @time end.submit @restime="" @row=1 @seentimes="" until @restime==@time @restime=results.search('//td[@class="hafasContent"]/table[2]/tr['+@row.to_s+']/td[1]').innerHTML.gsub(/\s/,"") @seentimes << @restime+"\n" @row=@row+1 raise "Time not found, "+@seentimes if @row==20 end @row=(@row-1).to_s @train=results.search('//td[@class="hafasContent"]/table[2]/tr['+@row+']/td[3]/a').text.gsub(/\s/,"") if results.search('//td[@class="hafasContent"]/table[2]/tr[1]/th[5]').innerHTML=="Aktuelles" @status=results.search('//td[@class="hafasContent"]/table[2]/tr['+@row+']/td[6]/span').innerHTML.gsub(/ /," ").gsub(/ä/,"ä").gsub(/ö/,"ö").strip @status=wrap(@status) @reason=results.search('//td[@class="hafasContent"]/table[2]/tr['+@row+']/td[7]').innerHTML.gsub(/ /," ").gsub(/ä/,"ä").gsub(/ö/,"ö").strip @reason=wrap(@reason) else @status="Noch kein Info" t=Time.parse(@time) t=t-3600 @reason="Info erst um "+t.strftime("%H:%M") end end @result = ""+@time+"
"+@train+"
"+@status.gsub(/\n/,"
")+"
"+@reason.gsub(/\n/,"
")+"
"+wrap(rss.items[0].title).gsub(/\n/,"
")+"
"+@name+"
" f=File.new(cache, "w") f.write @result f.close @result end end t = Time.new pages=[] pages << BahnPage.new("Dortmund Hbf", "arr", "18:41") if t.wday==5 and t.hour>=17 and t.hour<19 pages << BahnPage.new("Dortmund Hbf", "dep", "07:14") if t.wday==1 and t.hour>=6 and t.hour<8 if pages.length==0 pages << WeatherPage.new(1, "Heute") pages << WeatherPage.new(2, "Morgen") if t.hour>=9 if t.hour>=8 pages << RSSPage.new("http://www.spiegel.de/schlagzeilen/rss/0,5291,20,00.xml", "Spiegel Online") pages << RSSPage.new("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml", "BBC News") if t.wday>=1 and (t.wday<=4 or (t.wday==5 and t.hour<17)) pages << RSSPage.new("http://rss.slashdot.org/Slashdot/slashdot", "Slashdot") end end end page=IO.read("pageno") page="0" if page.nil? page=page.to_i page=0 if page>=pages.length @result = pages[page].get_page page=page+1 f=File.new("pageno", "w") f.puts page f.close puts "Content-Type: application/xhtml+xml\n\n" puts "\n" puts "\n" puts "\n" puts "\n" puts "\n" puts "\n" puts "\n" puts @result puts "\n" puts "\n"