#!/usr/bin/ruby #------------------------------------------ # # RADIO Server timer record manager # for ruby # # Nov.28.2000 # Ver1.00 Dec.07.2000 # # Programed by K.Kunikane(rero2) # #------------------------------------------- #--------------------------------------------------- # 設定事項 #--------------------------------------------------- #----- キャプチャーコマンド $capcom = "/home/rero2/radioserver/capt2mp3" #----- チャプチャーディレクトリ $capdir = "/home/radio_server/tmp/" #----- ストックディレクトリ $strdir = "/home/radio_server/" #--------------------------------------------------- # サブルーチン設計 #--------------------------------------------------- #--------------------------------------------------- # クラス設計 #--------------------------------------------------- #---------------------------------- # 予約アイテムクラス # class RecordItem #------------------------------------- #----- 初期化時パラメータ設定 # line = 録音予約データ一つ分 def initialize(line) @item_val = line.split @item_val[2] = @item_val[2].to_i @item_val[4] = @item_val[4].to_i @item_val[3] =~ /([0-9]+):([0-9]+)/ @item_val[5] = $1.to_i @item_val[6] = $2.to_i end #------------------------------------- #----- 最も近い未来の放送時刻を返す(sec) def onair #--- 現在の時刻 nowtime = Time::now #--- 時間差 ad_d = 0 nt = (((nowtime.hour * 60) + nowtime.min) * 60) + nowtime.sec it = (((@item_val[5] * 60) + @item_val[6]) * 60) if nt > it dt = (24*60*60 + it) - nt ad_d = 1 else dt = it - nt end #--- 週から何日離れているかを計算 if nowtime.wday > (@item_val[2] - ad_d) dw = ((@item_val[2] - ad_d + 7) - nowtime.wday) * (24 * 60 * 60) else dw = (@item_val[2] - ad_d - nowtime.wday) * (24 * 60 * 60) end (dw + dt) end #------------------------------------- #----- ラジオのファイル名サフィックスを返す def radio_title @item_val[1] end #------------------------------------- #----- ラジオの放送時間を返す(min) def radio_length @item_val[4] end end #---------------------------------- # 録音予約リストクラス # class RecordList #------------------------------------- #----- 初期化時パラメータ設定 # listname は録音リストのファイル名 def initialize(listname) unless File.exist?(listname) print "listfile not found. ("+listname+")\n" exit end @item = [] @timeadjust = 0 File.foreach(listname) do |line| if (line =~ /^[\s\t]*$/) || (line =~ /^#/) next end @item.push(RecordItem.new(line)); end end #------------------------------------- #----- 最も近い番組を返す def nearest near = (24*60*60*7) @item.each do |pitem| if near > pitem.onair near = pitem.onair @nitem = pitem end end # -- nitem = 最も放送に近い番組 @nitem end #------------------------------------- #----- 実際に録音する作業 def capture(nitem) name = nitem.radio_title time = (nitem.radio_length * 60) - 30 command = sprintf("%s %s %d", $capcom, name, time) system(command) stock(name) end #------------------------------------- #----- データを整理する def stock(label) # -- ディレクトリがなかったら掘る outdir = sprintf("%s%s", $strdir, label) unless File.exist?(outdir) Dir::mkdir(outdir) end # -- 移動 command = sprintf("mv %s%s*.mp3 %s%s/", $capdir, label, $strdir, label) system(command) # -- RealAudio はテンポラリ # command = sprintf("rm -f %srealstock/%s*.rm", $strdir, label) # system(command) # command = sprintf("mv %s%s*.rm %srealstock/", $capdir, label, $strdir) # system(command) end #------------------------------------- #----- 録音タイミングの判断 def do_capture work = 0 target = nearest if (target.onair < 900) sleep(5) while (target.onair > 15) sleep(1) while (target.onair > 4) capture(target) work = 1 end work end end #--------------------------------------------------- # メインフロー #--------------------------------------------------- #--- 録音リストの読み込み if File.exist?("spotlist.txt") request_list = RecordList.new("spotlist.txt") #--- 録音作業 request_list.do_capture end #--- 録音リストの読み込み request_list = RecordList.new(ARGV[0]) #--- 録音作業 request_list.do_capture