ニコニコ動画をダウンロードするRuby1.87対応スクリプトメモ。
ニコニコ動画をダウンロードするRuby1.87対応スクリプト
ruby1.9対応のスクリプトは、「ニコニコ動画ダウンロードするやつ」で他の方が公開してくださっています。
本記事では、上記のスクリプトをruby1.87で動くように少し修正したものを公開しています。
やったことは、ruby1.9にしか存在しないメソッドをruby1.87の対応するメソッドに変えたぐらいです。
# -- coding: utf-8 require "rubygems" require "mechanize" require "nokogiri" require "trollop" ID="mail" PW="pass" opts = Trollop.options do opt :dir, "save dir", :default => File.expand_path("../", __FILE__) end agent = Mechanize.new agent.post("https://secure.nicovideo.jp/secure/login?site=niconico", "mail" => ID, "password" => PW ARGV.each do |url_or_id| id = url_or_id[%r![^/]+$!] # for get nicohistory cookie # http://www.trinity-site.net/blog/?p=225 agent.get("http://www.nicovideo.jp/watch/#{id}") s = agent.post('http://flapi.nicovideo.jp/api/getflv', :v => id) video_url = URI.unescape(s.body).scan(/&url=(.+?)&/).first.to_s ext = case video_url[/smile\\?(.)=/, 1] when "m"; "mp4" when "s"; "swf" else "flv" end link = URI.unescape(s.body).scan(/&link=(.+?)&/).first.to_s title = (Nokogiri::HTML.parse agent.get(link).body).at_css('.video_title').text # ダメ文字全角化 http://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%90%8D title = title.gsub(/[\\/\\\\?*:|><]/) {|m| [m.ord + 65248].pack('U*')} cookie = agent.cookies.map(&:to_s).join("; ") save_path = "#{opts[:dir]}/#{id}-#{title}.#{ext}" cmd = %Q!wget -c -O '#{save_path}' --header="Cookie: #{cookie}" "#{video_url}"! system(cmd) end