#!/usr/bin/env ruby # [2003/12/30] by rubikitch # YahooTVから今日の番組表を作成してpalmに入れる # 要plucker-build, w3m # usage: tvprog.rb (引数なし) # # 86行目付近を地域によって書き換える必要あり。 $KCODE='e' class TVProg def initialize(txt, channels) @txt = txt @channels=channels end def get_table #+----------------+の間を取る #@table = @txt.scan(/\n\+-+\+\s*\n(.+)\n\+-+\+\s*\n/m).to_s @table = @txt.scan(/\n┌[─┬]+┐\s*\n(.+)\n└[─┴]+┘\s*\n/m).to_s @table = @txt.scan(/\n[\+┌][-─┬]+[\+┐]\s*\n(.+)\n[\+└][-─┴]+[\+┘]\s*\n/m).to_s end def channel_column #| | 1ch | 3ch | 4ch | | 6ch | 8ch | 10ch | | 12ch || #幅の配列 @lines = @table.split(/\n/) ch_line = @lines.find{|line| line =~/ch/} @col_ary = @channels.collect{|ch| re = /\s+#{ch}ch\s+/ m = ch_line.match(re) [m.begin(0), m.end(0)-1] } end def texts #列の配列。チャンネルごとの番組表 @texts = @col_ary.collect{|b,e| a = @lines.collect{|line| x=line[b..e] x.strip! case x when /^\s+$/ "" when /^[-─]+$/ "\n" else x end }.join.grep(/^\d/) a.each_with_index do |line, i| # 終了時刻を埋め込む begin # ppp "[line, i]", binding end_time = a[i+1][0,5] line.insert(5,"-#{end_time}") rescue NameError line.insert(5,"- ") end end a } end def texts_with_channel # 番組表にチャンネルを埋め込む @out = [@channels, @texts].transpose.collect{|ch, progs| progs.collect{|prog| prog.insert(11," #{ch}ch ") } }.flatten end def start # 番組表を得る(行の配列) get_table channel_column texts texts_with_channel end end def main html_filename = Time.now.strftime "%Y%m%d24.html" opt = "-o http_proxy=http://127.0.0.1:8339" #opt = "" # ここは地域によって書き換えよう。数字はチャンネル vhf = TVProg.new(`w3m -dump -o graphic_char=0 -cols 300 #{opt} http://tv.yahoo.co.jp/vhf/tokyo/#{html_filename}`, %w[1 3 4 6 8 10 12]).start bs = TVProg.new(`w3m -dump -o graphic_char=0 -cols 300 #{opt} http://tv.yahoo.co.jp/uhf_bs/tokyo/#{html_filename}`,%w[7 11]).start # 時間でソート # FIXME: 今朝4時と明朝4時の区別がつかない lines = (vhf+bs).sort_by{|prog| ch=prog.scan(/(\d+)ch/).to_s.to_i h=prog[0,2].to_i m=prog[3,2].to_i h+=24 if h<=4 weight = h*10000+m*100+ch } #書き出し # mkhtml(lines, $>);exit # for debug ## 一時ファイルに書き出し html="/tmp/tvprog.html" File.open(html, "w") do |f| mkhtml(lines, f) end ## pluckerize if system "plucker-build --launchable -H #{html} -f tvprog" # File.unlink html end end def mkhtml(lines, output="") # HTMLをでっちあげて、outputに出力 output<<"" output<<"

#{Time.now.strftime '%y/%m/%d'}

\n" ((5..23).to_a+(0..4).to_a).each do |h| h = format "%02d", h output << %Q[#{h}\n] end output << "
\n"

  h=""
  lines.each do |line|
    hour=line[0,2]
    if h!=hour
      h=hour
      output << %Q[ \n]
    end
    output << "* #{line}"
  end

  output << "
\n" end if __FILE__==$0 main end