ニコニコ動画のダウンロードスクリプト
- Yusukebe::Techで公開されていたPerlのダウンロードスクリプトを試してみたがうまく動かなかった。原因を調べた所、どうも8月中旬にニコニコ動画のログイン方法が変更された為のようだ。動くように修正したコードを以下に置きます。
また、debianで下記スクリプトを動かす為には以下パッケージを導入する必要がある。
- libwww-perl (LWP::UserAgent, HTTP::Cookies, HTTP::Request, HTTP::Headers)
- liburi-perl (URI::Escape)
- libcrypt-ssleay-perl (LWPでSSLのサイトにアクセスする時に必要となる)
#!/usr/bin/perl #nico-dl.pl use strict; use LWP::UserAgent; use HTTP::Cookies; use URI::Escape; use HTTP::Request; use HTTP::Headers; my $video_id = $ARGV[0] || "sm1210028"; my $mail = "yourmailaddress"; my $password = "yourpassword"; my $ua = LWP::UserAgent->new; $ua->cookie_jar( HTTP::Cookies->new( file => 'cookie.lwp', autosave => 1, )); &login(); $ua->get( "http://www.nicovideo.jp/watch/$video_id" ); my $res = $ua->get( "http://www.nicovideo.jp/api/getflv?v=$video_id" ); my $guha =$res->content; my $content = uri_unescape($res->content); my %data; my @temp = split("&",$content); foreach my $prop (@temp){ if($prop =~ /(.*?)=(.*)/){ $data{$1} = $2; } } #save flv $res = $ua->get( $data{url} ); &save($res->content, $video_id . ".flv"); #save xml my $post_data = "<thread res_from=\"-500\" version=\"20061206\" thread=\"" . $data{thread_id} ."\" />"; my $header = HTTP::Headers->new; $header->header('Content-Type' => 'text/xml'); my $req = HTTP::Request->new('POST', $data{ms}, $header, $post_data ); $res = $ua->request($req); &save($res->content, $video_id . ".xml"); sub login{ $ua->post( "https://secure.nicovideo.jp/secure/login?site=niconico", [ mail => $mail, password => $password, ]); } sub save{ my ($content, $filename) = @_; open FH, ">$filename"; binmode FH; print FH $content; close FH; }
I tried the download script of nicovideo. I looked into the reason of the error and found that the login method had changed since the middle of the August. The above script is the fixed one.