| 1 |
package Plagger::Plugin::Filter::FetchNicoVideo; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
our $VERSION = 0.02; |
|---|
| 6 |
|
|---|
| 7 |
use URI::Escape; |
|---|
| 8 |
use File::Spec; |
|---|
| 9 |
use HTTP::Request; |
|---|
| 10 |
use Time::HiRes qw(sleep); |
|---|
| 11 |
use Plagger::Enclosure; |
|---|
| 12 |
use Plagger::UserAgent; |
|---|
| 13 |
use CGI; |
|---|
| 14 |
|
|---|
| 15 |
sub register { |
|---|
| 16 |
my ( $self, $context ) = @_; |
|---|
| 17 |
$context->register_hook( $self, 'update.entry.fixup' => \&filter, ); |
|---|
| 18 |
my $ua = Plagger::UserAgent->new( keep_alive => 1 ); |
|---|
| 19 |
$ua->cookie_jar( {} ); |
|---|
| 20 |
$ua->post( "https://secure.nicovideo.jp/secure/login?site=niconico" => $self->conf ); |
|---|
| 21 |
$self->{ua} = $ua; |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
sub init { |
|---|
| 25 |
my $self = shift; |
|---|
| 26 |
$self->SUPER::init(@_); |
|---|
| 27 |
|
|---|
| 28 |
defined $self->conf->{mail} |
|---|
| 29 |
or Plagger->context->error("conifg 'mail' is not set."); |
|---|
| 30 |
defined $self->conf->{password} |
|---|
| 31 |
or Plagger->context->error("config 'password' is not set."); |
|---|
| 32 |
defined $self->conf->{dir} |
|---|
| 33 |
or Plagger->context->error("config 'dir' is not set."); |
|---|
| 34 |
|
|---|
| 35 |
if ( $self->conf->{dir} =~ /^[a-zA-Z]/ && $self->conf->{dir} !~ /:/ ) { |
|---|
| 36 |
$self->conf->{dir} = |
|---|
| 37 |
File::Spec->catfile( Cwd::cwd, $self->conf->{dir} ); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
unless ( -e $self->conf->{dir} && -d _ ) { |
|---|
| 41 |
Plagger->context->log( |
|---|
| 42 |
warn => $self->conf->{dir} . " does not exist. Creating" ); |
|---|
| 43 |
mkpath $self->conf->{dir}; |
|---|
| 44 |
} |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
sub filter { |
|---|
| 48 |
my ( $self, $context, $args ) = @_; |
|---|
| 49 |
my $ua = $self->{ua}; |
|---|
| 50 |
my $entry = $args->{entry}; |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
my ($video_id) = $entry->link =~ m!www.nicovideo.jp/watch/(.*)!; |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
my $res = $ua->get("http://www.nicovideo.jp/api/getflv?v=$video_id"); |
|---|
| 57 |
my $q = CGI->new( $res->content ); |
|---|
| 58 |
my $flv_url = $q->param('url'); |
|---|
| 59 |
|
|---|
| 60 |
unless ($flv_url) { |
|---|
| 61 |
$context->log( warn => "Not Found FLV URL : $video_id" ); |
|---|
| 62 |
return; |
|---|
| 63 |
} |
|---|
| 64 |
$context->log( info => "Found FLV URL $flv_url" ); |
|---|
| 65 |
|
|---|
| 66 |
my $enclosure = Plagger::Enclosure->new; |
|---|
| 67 |
$enclosure->url( URI->new($flv_url) ); |
|---|
| 68 |
$enclosure->media_type("video/x-flv"); |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
my $filename = $self->conf->{id_as_filename} ? $video_id : $entry->title; |
|---|
| 72 |
utf8::encode($filename); |
|---|
| 73 |
if ( $self->conf->{filename_encode} ) { |
|---|
| 74 |
Encode::from_to( $filename, "utf-8", $self->conf->{filename_encode} ); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
$enclosure->url =~ m!^http://[^/]+(?:smilevideo|nicovideo)\.jp/smile\?(\w)=(?:[^.]+)\.\d+(?:low)?!; |
|---|
| 78 |
my %video_type_of = ( |
|---|
| 79 |
v => 'flv', |
|---|
| 80 |
m => 'mp4', |
|---|
| 81 |
s => 'swf', |
|---|
| 82 |
); |
|---|
| 83 |
my $ext = exists( $video_type_of{$1} ) ? $video_type_of{$1} : "flv"; |
|---|
| 84 |
|
|---|
| 85 |
my $path = File::Spec->catfile( $self->conf->{dir}, $filename . ".$ext" ); |
|---|
| 86 |
|
|---|
| 87 |
unless ( -e $path ) { |
|---|
| 88 |
|
|---|
| 89 |
$ua->get("http://www.nicovideo.jp/watch/$video_id"); |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
my $req = HTTP::Request->new(GET => $enclosure->url); |
|---|
| 93 |
$context->log(info => "Fetching $video_id FLV File from " . $enclosure->url . "..." ); |
|---|
| 94 |
my $res = $ua->request($req, $path); |
|---|
| 95 |
$context->log(warn => "Fetch FLV Error: $video_id" ) if $res->is_error; |
|---|
| 96 |
}else{ |
|---|
| 97 |
$context->log(info => "Exist FLV File: $video_id"); |
|---|
| 98 |
my $sleeping_time = $self->conf->{interval} || 15; |
|---|
| 99 |
$context->log(info => "sleep $sleeping_time."); |
|---|
| 100 |
sleep( $sleeping_time ); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
if ( $self->conf->{download_comment} ) { |
|---|
| 105 |
$path = File::Spec->catfile( $self->conf->{dir}, $filename . ".xml" ); |
|---|
| 106 |
unless ( -e $path ) { |
|---|
| 107 |
my $thread_id = $q->param('thread_id'); |
|---|
| 108 |
my $post_data = |
|---|
| 109 |
qq!<thread res_from="-500" version="20061206" thread="$thread_id" />"!; |
|---|
| 110 |
my $header = HTTP::Headers->new; |
|---|
| 111 |
$header->header( 'Content-Type' => 'text/xml' ); |
|---|
| 112 |
my $req = HTTP::Request->new( 'POST', $q->param('ms'), $header, $post_data ); |
|---|
| 113 |
$context->log( info => "Fetching $video_id XML File ... " ); |
|---|
| 114 |
$res = $ua->request( $req, $path ); |
|---|
| 115 |
$context->log( warn => "Fetch XML Error: $video_id" ) if $res->is_error; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
$enclosure->filename($filename); |
|---|
| 120 |
$enclosure->local_path($path); # set to be used in later plugins |
|---|
| 121 |
if ($res->header('Content-Length') ) { |
|---|
| 122 |
$enclosure->length( $res->header('Content-Length') ); |
|---|
| 123 |
} |
|---|
| 124 |
$enclosure->type( Plagger::Util::mime_type_of($path) ); |
|---|
| 125 |
$entry->add_enclosure($enclosure); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
1; |
|---|
| 129 |
|
|---|
| 130 |
__END__ |
|---|
| 131 |
|
|---|
| 132 |
=head1 NAME |
|---|
| 133 |
|
|---|
| 134 |
Plagger::Plugin::Filter::FetchNicoVideo - Fetch flv file from NicoVideo link |
|---|
| 135 |
|
|---|
| 136 |
=head1 SYNOPSIS |
|---|
| 137 |
|
|---|
| 138 |
- module: Filter::FetchNicoVideo |
|---|
| 139 |
config: |
|---|
| 140 |
mail: your@mailadddres |
|---|
| 141 |
password: yourpassword |
|---|
| 142 |
dir: /path/to/files |
|---|
| 143 |
download_xml: 1 #optional default is 0 |
|---|
| 144 |
filename_encode: euc-jp #optional default is utf-8 |
|---|
| 145 |
interval: 60 #optional default is 15 |
|---|
| 146 |
|
|---|
| 147 |
=head1 DESCRIPTION |
|---|
| 148 |
|
|---|
| 149 |
This plugin downloads flv file for each entry which has NicoVideo link. |
|---|
| 150 |
|
|---|
| 151 |
=head1 CONFIG |
|---|
| 152 |
|
|---|
| 153 |
=over 4 |
|---|
| 154 |
|
|---|
| 155 |
=item mail password |
|---|
| 156 |
|
|---|
| 157 |
Your NicoVideo login mail address and password. |
|---|
| 158 |
|
|---|
| 159 |
=item dir |
|---|
| 160 |
|
|---|
| 161 |
Directory to store downloaded enclosures. Required. |
|---|
| 162 |
|
|---|
| 163 |
=item download_xml |
|---|
| 164 |
|
|---|
| 165 |
IF set, download comment xml file. Optional. Default is off. |
|---|
| 166 |
|
|---|
| 167 |
=item filename_encode |
|---|
| 168 |
|
|---|
| 169 |
File name encode. Example: euc-jp / shift_jis. Optional. Default is utf-8. |
|---|
| 170 |
|
|---|
| 171 |
=item id_as_filename |
|---|
| 172 |
|
|---|
| 173 |
IF set, set video id as flv file. Default file name is entry title. Optional. |
|---|
| 174 |
|
|---|
| 175 |
=back |
|---|
| 176 |
|
|---|
| 177 |
=head1 AUTHOR |
|---|
| 178 |
|
|---|
| 179 |
Yusuke Wada |
|---|
| 180 |
|
|---|
| 181 |
=head1 SEE ALSO |
|---|
| 182 |
|
|---|
| 183 |
L<Plagger>, L<http://www.nicovideo.jp/> |
|---|
| 184 |
|
|---|
| 185 |
=cut |
|---|
| 186 |
|
|---|
| 187 |
|
|---|