ニコニコ動画(γ)のSMILEVIDEOでもなんでもflvファイルをダウンロードするPerlコード

根本的に変わったのでこちらを参考にHTTP::Cookies::Mozillaを使っていたちごっこに付き合ってみる。Mechanizeとか使うのがいいんだろうけど、めんどくさいのでログインはブラウザから手動で方式。IEとかSafariの人は自分で書くかFirefoxを使うといいです。
仕様変更があったので1文字直した。
また仕様変更があったので4文字増やした。
修正忘れてたの直した。

usage

perl nicodl.pl http://www.nicovideo.jp/watch/sm10391 cookies.txt

nicodl.pl

#!/usr/bin/perl
use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Cookies::Mozilla;
use URI::Escape;

my $permalink = shift;
my $cookie = shift;

my $cookie_jar = HTTP::Cookies::Mozilla->new;
$cookie_jar->load($cookie);

my $ua = LWP::UserAgent->new;
$ua->cookie_jar($cookie_jar);

my $content = $ua->simple_request(
    HTTP::Request->new('GET', $permalink)
)->content;
$content =~ m|flvplayer\.swf\?v=(\w+)|m;
my $id = $1;

my $query_string = $ua->simple_request(
    HTTP::Request->new(
        'GET',
        "http://www.nicovideo.jp/api/getflv?ts=$id"
    )
)->content;
my %q = map {map {uri_unescape($_)} split /\=/, $_} split /\&/, $query_string;

$ua->mirror($q{url}, "$id.flv");