ニコニコ動画のランキングデータをRSSから取得する

RSSはログインなしで読めるらしいので、読んでみた。

use strict;
use utf8;
use XML::FeedPP;
use constant RANKING_ALL => 'http://www.nicovideo.jp/ranking/mylist/daily/all?rss=atom';
use Data::Dumper;

binmode(STDOUT, ':encoding(ms932)'); # MS-CMD

TEST: {
    print_todays_niconico_ranking(RANKING_ALL);
}

sub print_todays_niconico_ranking
{
    my $rss = shift;
    my $nicofeed = new XML::FeedPP($rss);
    
    # 適当に出しとく
    foreach my $item ($nicofeed->get_item()) {
        my $det = get_niconico_item_details($item);
        printf("%d: %s\n", $det->{rank}, $det->{title});
        ## めんどううううううう
        $det->{rank} = $det->{title} = undef;
        print Dumper($det), "\n";
    }
}

sub get_niconico_item_details
{
    my $item = shift;
    my $info = {
        rank => undef,
        title => undef,
        url => undef,
        thumbnail => undef,
        published => undef,
        length => undef,
        points => undef,
        view => undef,
        favorites => undef,
        comments => undef
    };
    my $title = $item->title();
    my $content = $item->description();
    
    Encode::_utf8_on($title);
    Encode::_utf8_on($content);
    
    # rank
    if ($title =~ /(\d+)/) {
        $info->{rank} = $1;
    }
    # link
    $info->{url} = $item->link();
    # title
    $title =~ s/\d+位://g;
    $info->{title} = $title;
    # published
    $info->{published} = $item->pubDate();
    # thumbnail
    if ($content =~ /src="([^"]+)/) {
        $info->{thumbnail} = $1;
    }
    # points
    if ($content =~ m{<strong class="nico-info-number">([\d,]+)</strong>pts}) {
        $info->{points} = $1;
    }
    # length
    if ($content =~ m{<strong class="nico-info-length">(\d+)(\d+)秒</strong>}) {
        $info->{length} = sprintf("%02d:%02d", $1, $2); # 時もある??
    }
    # vies
    if ($content =~ m{<strong class="nico-info-total-view">([\d,]+)</strong>}) {
        $info->{view} = $1;
    }
    # comment
    if ($content =~ m{<strong class="nico-info-total-res">([\d,]+)</strong>}) {
        $info->{comments} = $1;
    }
    # mylist
    if ($content =~ m{<strong class="nico-info-total-mylist">([\d,]+)</strong>}) {
        $info->{favorites} = $1;
    }
    
    return $info;
}
perl nicorank.pl
1: 超面食いが選んだコスプレ集 色々編
$VAR1 = {
          'favorites' => '6,246',
          'thumbnail' => 'http://tn-skr2.smilevideo.jp/smile?i=3291631',
          'points' => '5,815',
          'comments' => '48,444',
          'published' => '2008-05-14T04:00:00+09:00',
          'length' => '09:39',
          'view' => '175,270',
          'url' => 'http://www.nicovideo.jp/watch/sm3291631',
          'title' => undef,
          'rank' => undef
        };

2: 勇者の代わりにバラモス倒しに行くことになった 第1章その1
$VAR1 = {
          'favorites' => '17,760',
          'thumbnail' => 'http://tn-skr2.smilevideo.jp/smile?i=325909',
          'points' => '3,796',
          'comments' => '84,814',
          'published' => '2008-05-14T04:00:00+09:00',
          'length' => '16:20',
          'view' => '576,941',
          'url' => 'http://www.nicovideo.jp/watch/sm325909',
(略)

やる気がないけど読めたぜやったー。contentには、HTMLが入っているので、まじめにparseするか迷ったけど、正規表現で抜けるならまあいいかー、という感じのやる気のなさ。
これで毎日のランキングを保存できる。
WebService::NicoNicoDougaとかほしい。タグを別コネクションで取りに行くのが面倒なので、RSSに含めてくれるとうれしい。