=begin # download_radio ネットラジオから音声ファイルをダウンロードする。 ## 注意 win32apiを利用しているので、ActiveScriptRuby等でないと動作しない。 ## ダウンロード http://bmky.net/product/pragger/#download_radio ## 解説 フィードデータのリンク要素からラジオを視聴するためのURLを取得し、 そこからラジオ本体のURLを探してダウンローダーに渡す。 asxdirを指定すると、ダウンロード失敗時等の保険として asxファイルを保存する。 savedirを指定すると、ダウンロードした番組本体を リネームするためのバッチファイルを作成する。 ## 使い方 - module: myplugin::download_radio config: app: C:\NetTransport.exe # ダウンロードに使用するアプリケーション asxdir: asx/ # asxファイルを保存しておくディレクトリ savedir: c:\radio\ # リネーム用のバッチファイルを作成するディレクトリ ## 更新履歴 2008/05/01 : リリース =end require 'open-uri' require "Win32API" def download_radio( config, data ) return data unless config["app"] data.each do |item| next if item.link !~ /\.asx$/ asxurl = item.link asxname = asxurl.scan( /([^\/]*?\.asx)$/ )[0][0] asxtext = open( asxurl ) { |f| f.read.gsub( /\r/, "" ) } # asxをバックアップ if config["asxdir"] asxdir = ( config["asxdir"] + "/" ).sub!( /\/+$/, "/" ) open( asxdir + asxname, "w" ){ |w| w.puts asxtext } end asxtext.gsub( /"((?:mms|http):\/\/.*?(?:wma|asf|mp3|wsx|wmv))"/ ) do url = $1 # CMはダウンロードしない next if ( asxurl =~ /onsen/ and ( url =~ /cm/ ) != nil ) # メディファクとランティスは # NetTransportでダウンロードさせるためにプロトコルを変換 url.sub!( /mms/, "http" ) if asxurl =~ /mediafactory|lantis/ p "download " + url # ダウンローダーにURLを渡す shellexecute = Win32API.new( 'shell32.dll', 'ShellExecute', [ "P", "P", "P", "P", "P", "I" ], 'i' ) unless shellexecute.call( 0, "open", config["app"], url, 0, 1 ) > 0 raise "ShellExecute failed." end # リネーム用のバッチファイルを作成 if config["savedir"] if url =~ /([^\/]*?(\.(?:wma|asf|mp3|wsx|wmv)))/ wmaname = $1 ext = $2 wmaname.gsub!( /\./, "(1)." ) if asxurl =~ /mediafactory|lantis/ safe_title = item.title.gsub( /\s/, "_" ).gsub( /\//, "/" ).gsub( /\:/, ":" ).gsub( /\,/, "," ).gsub( /\;/, ";" ).gsub( /\*/, "*" ).gsub( /\?/, "?" ).gsub( /\/, ">" ).gsub( /\|/, "|" ) cmd = "REN " + wmaname + " " + safe_title + ext open( config["savedir"] + wmaname + ".bat", "w" ){ |w| w.puts cmd.tosjis } end end sleep( 2 ) end end return data end