Created
September 14, 2010 10:42
-
-
Save ryu22e/578850 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use utf8; | |
use File::Spec; | |
use FindBin; | |
use LWP::Simple; | |
use XML::RSS; | |
use DateTime::Format::DateParse; | |
use DateTime; | |
use YAML; | |
use Encode; | |
use Log::Dispatch::Config; | |
# ニコニコ生放送RSS(http://live.nicovideo.jp/rss)からiCalendarファイルを生成するデータを抜き出すためのスクリプト。 | |
# Plagger::Plugin::Subscription::Configで利用する。 | |
use constant URL => 'http://live.nicovideo.jp/rss'; | |
use constant LOG_CONFIG_NAME => 'niconico-live-log.conf'; | |
# ログ出力用オブジェクトの生成を行う。 | |
my $config = File::Spec->catfile( $FindBin::Bin, LOG_CONFIG_NAME ); | |
Log::Dispatch::Config->configure($config); | |
my $Logger = Log::Dispatch::Config->instance; | |
# RSSフィードのパースを行う。 | |
my $document; | |
eval { $document = LWP::Simple::get(URL); }; | |
if ($@) { | |
$Logger->error( sprintf( "Failed to access %s: %s", URL, $@ ) ); | |
die $@; | |
} | |
elsif ( !$document ) { | |
my $message = sprintf( "Accessed %s, but response is empty.", URL ); | |
$Logger->error($message); | |
die($message); | |
} | |
my $rss = XML::RSS->new( version => '2.0' ); | |
$rss->add_module( prefix => 'nicolive', uri => 'http://live.nicovideo.jp/' ); | |
eval { $rss->parse($document); }; | |
if ($@) { | |
$Logger->error( sprintf( "Failed to parse %s: %s", URL, $@ ) ); | |
die $@; | |
} | |
# パースしたRSSフィードを加工してYAML形式で出力する。 | |
binmode STDOUT, ":utf8"; | |
print YAML::Dump +{ | |
title => 'ニコニコ生放送', | |
link => 'http://live.nicovideo.jp/', | |
entry => [ | |
sort { $a->{date} cmp $b->{date} } | |
map { | |
# nicolive:open_timeから放送開始日時を取得する。 | |
my $nicolive_open_time = $_->{nicolive}->{open_time}; | |
my $startDate = DateTime::Format::DateParse->parse_datetime( $nicolive_open_time, 'Asia/Tokyo' ); | |
if ($startDate) { | |
+{ | |
title => $_->{title}, | |
date => $startDate->strftime('%a, %d %b %Y %H:%M:%S %z'), | |
summary => $_->{link}, | |
body => $_->{description} | |
}; | |
} | |
else { | |
# ログに放送予定日を取得できなかった旨をを出力する | |
$Logger->warn( | |
sprintf( "'nicolive:open_time' is not found in '%s'.", | |
encode( 'utf8', $_->{title} ) ) | |
); | |
} | |
} @{ $rss->{items} }, | |
], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
niconico-live.plと同じディレクトリにniconico-live-log.conf(Log::Dispatch::Config用の設定ファイル)を置く必要がある。
niconico-live-log.confの記入例は下記の通り。
詳細はperldoc Log::Dispatch::Configを参照すること。