Changeset 7641
- Timestamp:
- 03/08/08 01:32:43 (9 months ago)
- Files:
-
- 1 modified
-
lang/perl/misc/fetch-nico-video.pl (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/misc/fetch-nico-video.pl
r7635 r7641 3 3 # copyed from http://coderepos.org/share/browser/lang/perl/plagger/lib/Plagger/Plugin/Filter/FetchNicoVideo.pm 4 4 5 package FetchNicoVideo; 6 5 7 use strict; 6 7 use Config::Pit; 8 use warnings; 9 10 use Carp qw(croak); 11 use Cwd (); 8 12 use CGI; 13 use Encode; 14 use File::Spec; 15 use HTTP::Request; 9 16 use LWP::UserAgent; 17 use Time::HiRes qw(sleep); 18 use URI::Escape; 19 20 our $VERSION = '0.01'; 21 22 sub new { 23 my $class = shift; 24 my $opt = ref($_[0]) eq "HASH" ? shift : {@_}; 25 26 eval { 27 require Config::Pit; 28 $opt->{pit} = Config::Pit::pit_get("nicovideo.jp"); 29 }; 30 31 for my $key (qw/mail password/) { 32 $opt->{$key} ||= $opt->{pit}->{$key} if $opt->{pit}->{$key}; 33 $opt->{_form}->{$key} = $opt->{$key}; 34 croak "conifg '$key' is not set." unless $opt->{$key}; 35 } 36 my $self = bless $opt, $class; 37 38 $self->_init_dir; 39 $self->_init_ua; 40 41 $self; 42 } 43 44 sub _init_dir { 45 my $self = shift; 46 47 unless ($self->{dir}) { 48 $self->{dir} = Cwd::cwd; 49 return; 50 } 51 52 if ($self->{dir} =~ /^[a-zA-Z]/ && $self->conf->{dir} !~ /:/) { 53 $self->{dir} = File::Spec->catfile(Cwd::cwd, $self->conf->{dir}); 54 } 55 56 unless (-e $self->{dir} && -d _) { 57 warn $self->{dir} . " does not exist. Creating" unless $self->{quiet}; 58 mkpath $self->{dir}; 59 } 60 } 61 62 sub _init_ua { 63 my $self = shift; 64 65 my $ua = LWP::UserAgent->new(keep_alive => 1); 66 $ua->cookie_jar({}); 67 $ua->post("https://secure.nicovideo.jp/secure/login?site=niconico" => $self->{_form}); 68 $self->{ua} = $ua; 69 70 $self; 71 } 10 72 11 73 sub download { 12 my $ua = shift;13 my $ entry = shift;14 15 # get video_id74 my ($self, $entry) = @_; 75 my $ua = $self->{ua}; 76 77 # get video_id 16 78 my ($video_id) = $entry =~ m!www.nicovideo.jp/watch/(.*)!; 17 18 #get flv url 79 croak "invalid entry ($entry)" unless $video_id; 80 81 if (not $self->{id_as_filename} and not exists($self->{title})) { 82 my $resentry = $ua->get($entry); 83 my $content = Encode::decode('utf-8', $resentry->content); 84 $self->{title} = ($content =~ m!<h1><a [^>]+>(.*?)</a></h1>!s)[0]; 85 } 86 87 # get flv url 19 88 my $res = $ua->get("http://www.nicovideo.jp/api/getflv?v=$video_id"); 20 my $q = CGI->new( $res->content);89 my $q = CGI->new($res->content); 21 90 my $flv_url = $q->param('url'); 22 91 23 unless ($flv_url) { 24 die "Not Found FLV URL : $video_id"; 25 } 26 warn "Found FLV URL $flv_url"; 92 return warn "Not Found FLV URL : $video_id" unless $flv_url; 93 94 print "Found FLV URL $flv_url" unless $self->{quiet}; 95 96 #set local path 97 my $filename; 98 if ($self->{id_as_filename}) { 99 $filename = $video_id; 100 101 } else { 102 $filename = $self->{title}; 103 utf8::encode($filename); 104 if ($self->{filename_encode}) { 105 Encode::from_to($filename, "utf-8", $self->{filename_encode}); 106 } 107 if ($self->{add_id_to_filename}) { 108 $filename = "$video_id $filename"; 109 } 110 } 27 111 28 112 $flv_url =~ m!^http://[^/]+(?:smilevideo|nicovideo)\.jp/smile\?(\w)=(?:[^.]+)\.\d+(?:low)?!; … … 33 117 ); 34 118 my $ext = exists( $video_type_of{$1} ) ? $video_type_of{$1} : "flv"; 35 my $path = "$video_id.$ext";36 37 unless ( -e $path) {38 # access video page119 my $path = File::Spec->catfile( $self->{dir}, $filename . ".$ext" ); 120 121 unless (-e $path) { 122 # access video page 39 123 $ua->get("http://www.nicovideo.jp/watch/$video_id"); 40 124 41 # download flv file125 # download flv file 42 126 my $req = HTTP::Request->new(GET => $flv_url); 43 warn "Fetching $video_id FLV File from " . $flv_url . "..."; 44 my $res = $ua->request($req, $path); 127 print "Fetching $video_id (save as '$filename.$ext') FLV File from " . $flv_url . "..." unless $self->{quiet};; 128 $res = $ua->request($req, $path); 129 print "\n" unless $self->{quiet}; 45 130 warn "Fetch FLV Error: $video_id" if $res->is_error; 46 } else{131 } else { 47 132 warn "Exist FLV File: $video_id"; 48 my $sleeping_time = 15;133 my $sleeping_time = $self->{interval} || 15; 49 134 warn "sleep $sleeping_time."; 50 sleep( $sleeping_time ); 51 } 52 } 53 54 sub ua { 55 my $conf = shift; 56 my $ua = LWP::UserAgent->new( keep_alive => 1 ); 57 $ua->cookie_jar( {} ); 58 $ua->post( "https://secure.nicovideo.jp/secure/login?site=niconico" => $conf ); 59 $ua; 60 } 61 62 sub conf { 63 my $conf = pit_get("nicovideo.jp", require => { 64 mail => "your username on example", 65 password => "your password on example", 66 }); 67 $conf; 68 } 135 sleep($sleeping_time); 136 } 137 } 138 139 package main; 140 141 use strict; 142 use warnings; 143 144 use Getopt::Long; 145 69 146 sub main { 70 my $conf = conf; 71 my $ua = ua($conf); 147 my %opt = (); 148 GetOptions(\%opt, 149 'mail=s', 150 'password=s', 151 'quiet', 152 'dir=s', 153 'filename_encode=s', 154 'id_as_filename', 155 'add_id_to_filename', 156 'title=s', 157 'interval=i', 158 ); 159 160 if (exists($opt{title}) and scalar(@ARGV) > 1) { 161 die "you cannot download multiple urls with --title option."; 162 } 163 164 my $nico = FetchNicoVideo->new(%opt); 72 165 for my $entry (@ARGV) { 73 download($ua,$entry);166 $nico->download($entry); 74 167 } 75 168 } 76 169 77 170 main; 171 172 __END__ 173 174 =head1 NAME 175 176 fetch-nico-video - command line downloader of NicoVideo 177 178 =head1 SYNOPSIS 179 180 fetch-nico-video [options] [urls ...] 181 182 =head1 DESCRIPTION 183 184 fetch-nico-video is a command line downloader of NicoVideo. 185 186 =head1 CONFIG 187 188 =over 4 189 190 =item ~/.pit/default.yaml (on your pit) 191 192 --- 193 'nicovideo.jp': 194 mail: 'your mail address' 195 password: 'your password' 196 197 =item --mail and --password (overrride values of pit) 198 199 --mail 'your mail address' 200 --password 'your password' 201 202 =item --dir 203 204 Directory to store downloaded enclosures. Optional. 205 206 =item --filename_encode 207 208 File name encode. Example: euc-jp / shift_jis. Optional. Default is utf-8. 209 210 =item --id_as_filename 211 212 IF set, set video id as flv file. Default file name is entry title. Optional. 213 214 =item --add_id_to_filename 215 216 IF set, add video id as prefix of filename. Optional. 217 218 =item --title 219 220 If set, override entry title. Optional. 221 222 =item --interval 223 224 Interval between each download. Default is 15. 225 226 =back 227 228 =head1 SEE ALSO 229 230 L<Plagger::Plugin::Filter::FetchNicoVideo>, L<Config::Pit> 231 232 =cut
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)