Changeset 7641

Show
Ignore:
Timestamp:
03/08/08 01:32:43 (9 months ago)
Author:
woremacx
Message:

lang/perl/misc/fetch-nico-video.pl: now we have lots of options

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/misc/fetch-nico-video.pl

    r7635 r7641  
    33# copyed from http://coderepos.org/share/browser/lang/perl/plagger/lib/Plagger/Plugin/Filter/FetchNicoVideo.pm 
    44 
     5package FetchNicoVideo; 
     6 
    57use strict; 
    6  
    7 use Config::Pit; 
     8use warnings; 
     9 
     10use Carp qw(croak); 
     11use Cwd (); 
    812use CGI; 
     13use Encode; 
     14use File::Spec; 
     15use HTTP::Request; 
    916use LWP::UserAgent; 
     17use Time::HiRes qw(sleep); 
     18use URI::Escape; 
     19 
     20our $VERSION      = '0.01'; 
     21 
     22sub 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 
     44sub _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 
     62sub _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} 
    1072 
    1173sub download { 
    12     my $ua    = shift; 
    13     my $entry = shift; 
    14  
    15     #get video_id 
     74    my ($self, $entry) = @_; 
     75    my $ua = $self->{ua}; 
     76 
     77    # get video_id 
    1678    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 
    1988    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); 
    2190    my $flv_url = $q->param('url'); 
    2291 
    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    } 
    27111 
    28112    $flv_url =~ m!^http://[^/]+(?:smilevideo|nicovideo)\.jp/smile\?(\w)=(?:[^.]+)\.\d+(?:low)?!; 
     
    33117    ); 
    34118    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 page 
     119    my $path = File::Spec->catfile( $self->{dir}, $filename . ".$ext" ); 
     120 
     121    unless (-e $path) { 
     122        # access video page 
    39123        $ua->get("http://www.nicovideo.jp/watch/$video_id"); 
    40124 
    41         #download flv file 
     125        # download flv file 
    42126        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}; 
    45130        warn "Fetch FLV Error: $video_id" if $res->is_error; 
    46     }else{ 
     131    } else { 
    47132        warn "Exist FLV File: $video_id"; 
    48         my $sleeping_time = 15; 
     133        my $sleeping_time = $self->{interval} || 15; 
    49134        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 
     139package main; 
     140 
     141use strict; 
     142use warnings; 
     143 
     144use Getopt::Long; 
     145 
    69146sub 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); 
    72165    for my $entry (@ARGV) { 
    73         download($ua, $entry); 
     166        $nico->download($entry); 
    74167    } 
    75168} 
    76169 
    77170main; 
     171 
     172__END__ 
     173 
     174=head1 NAME 
     175 
     176fetch-nico-video - command line downloader of NicoVideo 
     177 
     178=head1 SYNOPSIS 
     179 
     180    fetch-nico-video [options] [urls ...] 
     181 
     182=head1 DESCRIPTION 
     183 
     184fetch-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 
     204Directory to store downloaded enclosures. Optional. 
     205 
     206=item --filename_encode 
     207 
     208File name encode. Example: euc-jp / shift_jis. Optional. Default is utf-8. 
     209 
     210=item --id_as_filename 
     211 
     212IF set, set video id as flv file. Default file name is entry title. Optional. 
     213 
     214=item --add_id_to_filename 
     215 
     216IF set, add video id as prefix of filename. Optional. 
     217 
     218=item --title 
     219 
     220If set, override entry title. Optional. 
     221 
     222=item --interval 
     223 
     224Interval between each download. Default is 15. 
     225 
     226=back 
     227 
     228=head1 SEE ALSO 
     229 
     230L<Plagger::Plugin::Filter::FetchNicoVideo>, L<Config::Pit> 
     231 
     232=cut