2008/10/15

基本部はこちらを参照。
ニコニコ動画のサムネイル画像を取得する Perl スクリプト - Yet Another Hackadelic
http://d.hatena.ne.jp/ZIGOROu/20081014/1223991205
思いっきり個人用ぽいスクリプトになってしまいました...。
Plaggerでアレを適当なフォルダにガサーーーと落として、そのフォルダ上で走らせる。 ガサーーーのレシピはこんな感じ。
plugins:
  - module: Subscription::Config
    config:
      feed:
         - http://www.nicovideo.jp/mylist/7688389

  - module: Filter::FetchNicoVideo
    config:
      mail: xxxxx@example.com
      password: your-password
      dir: /path/to/download/
      download_comment: 1
      id_as_filename: 0
      # for windows
      #filename_encode: shift-jis
スクリプトはガサーーーの中に含まれるXMLをパースしてゴニョゴニョしている。
#!/usr/bin/perl

use strict;
use warnings;

use Encode;
use Template;
use XML::Simple;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new( keep_alive => 4 );
my $xs = XML::Simple->new;

sub save {
    my ($content, $filename) = @_;
    open my $fh, '>', $filename;
    binmode $fh;
    print $fh $content;
    close $fh;
}

sub build {
    my $file = $_;

    # 動画IDと、ソートに用いるサーバ時刻を取得する
    my $ref = $xs->XMLin($file);
    my $video_id = substr($ref->{view_counter}->{id}, 2);
    my $time = $ref->{thread}->{server_time};

    # 動画ファイル名を得る(s/xml/flv/)
    $file =~ s/\.xml$//;
    $file = decode('cp932', $file) if $^O eq 'MSWin32';
    my $movie_file = "$file.flv";

    # ref: http://d.hatena.ne.jp/ZIGOROu/20081014/1223991205
    my $server_id = $video_id % 2 + 1;
    my $thumb_url = "http://tn-skr$server_id.smilevideo.jp/smile?i=$video_id";
    my $thumb_file = "sm$video_id.jpg";
    my $video_url = "http://www.nicovideo.jp/watch/sm$video_id";

    # サムネイルをダウンロード
    unless (-e $thumb_file) {
        my $res = $ua->get($thumb_url);
        &save($res->content, $thumb_file);
    }

    # " - "でファイル名を分割して先頭を題名に、
    my @info = split(/ - /, $file);
    return {
        title   => $info[0] || $file,
        speaker => $info[1] || $file,
        thumb   => $thumb_file,
        file    => $movie_file,
        time    => $time,
        url     => $video_url,
    };
}

# XMLファイル一覧から動画情報を組み立て、サーバ時刻でソートする
my @movies = sort {$a->{time} <=> $b->{time}}
    map(build($_), glob('*.xml'));

# Templateを使用してHTML出力
binmode STDOUT, ':encoding(utf-8)';
my $tt = Template->new({ UNICODE => 1, ENCODING => 'utf-8' });
$tt->process(\*DATA, {movies => \@movies});

__DATA__
<html>
<head>
<title>第1回 Coderepos Con</title>
<style type="text/css">
body { font-family: meiryo; background-color: black; color: white }
a { color: #333399 }
.file { color: orange; }
.movie { font-size: 20px; font-weight: bold; padding: 0.5em; }
.movie img { margin-right: 0.5em; border: 1px solid blue; }
</style>
</head>
<body>
<h1>第1回 Coderepos Con</h1>
<hr />
<div id="content">
[% FOREACH movie IN movies %]
<div class="movie">
    <img src="[% movie.thumb %]" style="float:left;" />
    Name: <a class="file" href="[% movie.file %]">[% movie.title %]</a><br />
    Speaker: [% movie.speaker %]<br />
    URL: <a href="[% movie.url %]">[% movie.url %]</a><br />
    <br clear="all"/>
</div>
[% END %]
</div>
</body>
</html>
" - "でタイトルとか分けてる部分は、この動画ファイルの名づけ方に依存しているので使う人は好き勝手に変えて下さい。
ちなみに動かすとこんなHTMLが出来上がる。
nico-playlist
いやぁ久々ソースにコメント書いたね!(えっ
Posted at 16:49 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.