NicoProxy キャッシュファイルのエンコーディングパッチ
NicoProxy ニコニコ動画SP1に対応しました - unnecessary words
使っている環境が Windows なため、キャッシュファイルが文字化けした状態で
保存されてしまうので、キャッシュファイルのエンコーディングタイプを選択
できるようにしてみました。できるとは言っても、Ruby(kconv) で変換できる
shift-jis、euc、utf8 の 3 種類だけですが。
Windows 環境なので、素直に exe 版を使えばいいのでしょうけれど。
設定は conf.yaml に file_encoding とエンコーディングタイプを指定するだけです。
設定例
proxy_port: 8080 cache_dir: ./cache file_encoding: shift-jis
file_encoding には以下のような名前を指定することができるようになっています。
パッチ
diff -crN nicoproxy.org/nico_proxy.rb nicoproxy/nico_proxy.rb *** nicoproxy.org/nico_proxy.rb Sun Mar 09 21:26:58 2008 --- nicoproxy/nico_proxy.rb Tue Apr 22 22:32:37 2008 *************** *** 7,12 **** --- 7,13 ---- config = { :BindAddress => '0.0.0.0', :Port => conf['proxy_port'], + :FileEncoding => conf['file_encoding'], } s = NicoProxyServer.new(config, conf['cache_dir']) diff -crN nicoproxy.org/nicoproxy/nicoproxyserver.rb nicoproxy/nicoproxy/nicoproxyserver.rb *** nicoproxy.org/nicoproxy/nicoproxyserver.rb Sun Mar 09 21:23:32 2008 --- nicoproxy/nicoproxy/nicoproxyserver.rb Tue Apr 22 22:16:44 2008 *************** *** 1,12 **** require 'webrick/httpproxy' require 'nicoproxy/apirequesthistory' require 'nicoproxy/cache' class NicoProxyServer < WEBrick::HTTPProxyServer - def initialize(conf, dir = '.') @arh = ApiRequestHistory.new @cache = Cache.new(dir) conf[:ProxyContentHandler] = Proc.new() do |req, res| --- 1,21 ---- require 'webrick/httpproxy' require 'nicoproxy/apirequesthistory' require 'nicoproxy/cache' + require 'kconv' class NicoProxyServer < WEBrick::HTTPProxyServer def initialize(conf, dir = '.') + encoding_tbl = { + 'sjis' => 'tosjis', + 'shift-jis' => 'tosjis', + 'euc' => 'toeuc', + 'euc-jp' => 'toeuc', + 'utf8' => 'toutf8', + } + @arh = ApiRequestHistory.new @cache = Cache.new(dir) + @file_encoding = encoding_tbl.key?(conf[:FileEncoding]) ? encoding_tbl[conf[:FileEncoding]] : 'to_s' conf[:ProxyContentHandler] = Proc.new() do |req, res| *************** *** 54,62 **** def cache_save(api_request, res) unless @cache.search(api_request.video_id) || api_request.low? begin ! open(@cache.path(api_request.filename), "wb"){|io| io.write(res.body)} rescue ! open(@cache.path(api_request.filename_simple), "wb"){|io| io.write(res.body)} end @logger.info("Save cache: #{api_request.video_id}\n") end --- 63,71 ---- def cache_save(api_request, res) unless @cache.search(api_request.video_id) || api_request.low? begin ! open(@cache.path(api_request.filename).send(@file_encoding), "wb"){|io| io.write(res.body)} rescue ! open(@cache.path(api_request.filename_simple).send(@file_encoding), "wb"){|io| io.write(res.body)} end @logger.info("Save cache: #{api_request.video_id}\n") end *************** *** 68,74 **** %r!http://.*\.nicovideo\.photozou.jp/pub/171/162171/video/.*! api_request = @arh.url_to_api(req.unparsed_uri) if path = @cache.search( api_request.video_id ) ! open(path, "rb"){|io| res.body = io.read} @logger.info("Play cache: #{path}\n") return end --- 77,83 ---- %r!http://.*\.nicovideo\.photozou.jp/pub/171/162171/video/.*! api_request = @arh.url_to_api(req.unparsed_uri) if path = @cache.search( api_request.video_id ) ! open(path.send(@file_encoding), "rb"){|io| res.body = io.read} @logger.info("Play cache: #{path}\n") return end