#!/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


\n"+ "

"+@desc1+"


\n"+ "


\n"+ "


\n"+ "


\n"+ "


\n" elsif @desc1==@desc2 "

Morgens, Nachmittags


\n"+ "

"+@desc1+"


\n"+ "

Abends


\n"+ "

"+@desc3+"


\n"+ "


\n"+ "


\n" elsif @desc2==@desc3 "

Morgens


\n"+ "

"+@desc1+"


\n"+ "

Nachmittags, Abends


\n"+ "

"+@desc2+"


\n"+ "


\n"+ "


\n" elsif @desc1==@desc3 "

Morgens, Abends


\n"+ "

"+@desc1+"


\n"+ "

Nachmittags


\n"+ "

"+@desc2+"


\n"+ "


\n"+ "


\n" else "

Morgens


\n"+ "

"+@desc1+"


\n"+ "

Nachmittags


\n"+ "

"+@desc2+"


\n"+ "

Abends


\n"+ "

"+@desc3+"


\n" end end def temp @min+"-"+@max+"C" end def get_page cache=@name.downcase+"_cache" cachetime=Time.new-(15*60) if File.exist?(cache) and (cachetime<=>File.mtime(cache))==-1 # Cache is good, use it return IO.read(cache) end @url = "http://www.wdr.de/themen/wetter/nrw/prognose_wuppertal.jhtml" a = WWW::Mechanize.new a.get(@url) do |page| page.search("acronym").remove scrape(page) end @result = "" @result = "

Wetter "+@name+"


\n"+desc+"

"+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+"


\n" @result = @result+"

"+@status.gsub(/\n/,"


\n

")+"


\n" @result = @result+"


\n" @result = @result+"

"+@reason.gsub(/\n/,"


\n

")+"


\n" @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 RSSPage def initialize(url,name) @url=url @name=name end def get_page cache=@name.gsub(/\s/,"").downcase+"_cache" cachetime=Time.new-(30*60) if File.exist?(cache) and (cachetime<=>File.mtime(cache))==-1 # Cache is good, use it # return IO.read(cache) end a = WWW::Mechanize.new { |agent| agent.user_agent_alias = "Windows Mozilla" } content=a.get_file(@url) rss = RSS::Parser.parse(content, false) @result = "

"+wrap(rss.items[0].title).gsub(/\n/,"


\n

")+"


\n" lines=0 @result.scan("
") do lines=lines+1 end lines.upto 6 do |l| @result << "


\n" end @result << "

"+@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"