| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::YouTube::Download; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 7 |  |  | 7 |  | 480490 | use strict; | 
|  | 7 |  |  |  |  | 84 |  | 
|  | 7 |  |  |  |  | 218 |  | 
| 4 | 7 |  |  | 7 |  | 36 | use warnings; | 
|  | 7 |  |  |  |  | 11 |  | 
|  | 7 |  |  |  |  | 190 |  | 
| 5 | 7 |  |  | 7 |  | 169 | use 5.008001; | 
|  | 7 |  |  |  |  | 24 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.64'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 7 |  |  | 7 |  | 50 | use Carp qw(croak); | 
|  | 7 |  |  |  |  | 13 |  | 
|  | 7 |  |  |  |  | 356 |  | 
| 10 | 7 |  |  | 7 |  | 3976 | use URI (); | 
|  | 7 |  |  |  |  | 47958 |  | 
|  | 7 |  |  |  |  | 176 |  | 
| 11 | 7 |  |  | 7 |  | 4682 | use LWP::UserAgent; | 
|  | 7 |  |  |  |  | 273185 |  | 
|  | 7 |  |  |  |  | 285 |  | 
| 12 | 7 |  |  | 7 |  | 3465 | use JSON::MaybeXS 'JSON'; | 
|  | 7 |  |  |  |  | 40407 |  | 
|  | 7 |  |  |  |  | 516 |  | 
| 13 | 7 |  |  | 7 |  | 3647 | use HTML::Entities qw/decode_entities/; | 
|  | 7 |  |  |  |  | 40091 |  | 
|  | 7 |  |  |  |  | 503 |  | 
| 14 | 7 |  |  | 7 |  | 56 | use HTTP::Request; | 
|  | 7 |  |  |  |  | 13 |  | 
|  | 7 |  |  |  |  | 201 |  | 
| 15 | 7 |  |  | 7 |  | 3038 | use MIME::Type; | 
|  | 7 |  |  |  |  | 8621 |  | 
|  | 7 |  |  |  |  | 338 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | $Carp::Internal{ (__PACKAGE__) }++; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 7 |  |  | 7 |  | 50 | use constant DEFAULT_FMT => 18; | 
|  | 7 |  |  |  |  | 19 |  | 
|  | 7 |  |  |  |  | 1143 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | my $base_url = 'https://www.youtube.com/watch?v='; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub new { | 
| 24 | 32 |  |  | 32 | 1 | 15806 | my $class = shift; | 
| 25 | 32 |  |  |  |  | 74 | my %args = @_; | 
| 26 |  |  |  |  |  |  | $args{ua} = LWP::UserAgent->new( | 
| 27 |  |  |  |  |  |  | agent      => __PACKAGE__.'/'.$VERSION, | 
| 28 |  |  |  |  |  |  | parse_head => 0, | 
| 29 | 32 | 50 |  |  |  | 205 | ) unless exists $args{ua}; | 
| 30 | 32 |  |  |  |  | 6412 | bless \%args, $class; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | for my $name (qw[video_id video_url title user fmt fmt_list suffix]) { | 
| 34 |  |  |  |  |  |  | ## no critic (TestingAndDebugging::ProhibitNoStrict) | 
| 35 | 7 |  |  | 7 |  | 49 | no strict 'refs'; | 
|  | 7 |  |  |  |  | 15 |  | 
|  | 7 |  |  |  |  | 361 |  | 
| 36 |  |  |  |  |  |  | *{"get_$name"} = sub { | 
| 37 | 7 |  |  | 7 |  | 40 | use strict 'refs'; | 
|  | 7 |  |  |  |  | 15 |  | 
|  | 7 |  |  |  |  | 42102 |  | 
| 38 | 0 |  |  | 0 |  | 0 | my ($self, $video_id) = @_; | 
| 39 | 0 | 0 |  |  |  | 0 | croak "Usage: $self->get_$name(\$video_id|\$watch_url)" unless $video_id; | 
| 40 | 0 |  |  |  |  | 0 | my $data = $self->prepare_download($video_id); | 
| 41 | 0 |  |  |  |  | 0 | return $data->{$name}; | 
| 42 |  |  |  |  |  |  | }; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub playback_url { | 
| 46 | 0 |  |  | 0 | 1 | 0 | my ($self, $video_id, $args) = @_; | 
| 47 | 0 | 0 |  |  |  | 0 | croak "Usage: $self->playback_url('[video_id|video_url]')" unless $video_id; | 
| 48 | 0 |  | 0 |  |  | 0 | $args ||= {}; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  | 0 | my $data = $self->prepare_download($video_id); | 
| 51 | 0 |  | 0 |  |  | 0 | my $fmt  = $args->{fmt} || $data->{fmt} || DEFAULT_FMT; | 
| 52 | 0 |  | 0 |  |  | 0 | my $video_url = $data->{video_url_map}{$fmt}{url} || croak "this video does not offer format (fmt) $fmt"; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 |  |  |  |  | 0 | return $video_url; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub download { | 
| 58 | 0 |  |  | 0 | 1 | 0 | my ($self, $video_id, $args) = @_; | 
| 59 | 0 | 0 |  |  |  | 0 | croak "Usage: $self->download('[video_id|video_url]')" unless $video_id; | 
| 60 | 0 |  | 0 |  |  | 0 | $args ||= {}; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  | 0 | my $data = $self->prepare_download($video_id); | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  | 0 |  |  | 0 | my $fmt = $args->{fmt} || $data->{fmt} || DEFAULT_FMT; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  | 0 |  |  | 0 | my $video_url = $data->{video_url_map}{$fmt}{url} || croak "this video has not supported fmt: $fmt"; | 
| 67 | 0 |  | 0 |  |  | 0 | $args->{filename} ||= $args->{file_name}; | 
| 68 |  |  |  |  |  |  | my $filename = $self->_format_filename($args->{filename}, { | 
| 69 |  |  |  |  |  |  | video_id   => $data->{video_id}, | 
| 70 |  |  |  |  |  |  | title      => $data->{title}, | 
| 71 |  |  |  |  |  |  | user       => $data->{user}, | 
| 72 |  |  |  |  |  |  | fmt        => $fmt, | 
| 73 |  |  |  |  |  |  | suffix     => $data->{video_url_map}{$fmt}{suffix} || _suffix($fmt), | 
| 74 | 0 |  | 0 |  |  | 0 | resolution => $data->{video_url_map}{$fmt}{resolution} || '0x0', | 
|  |  |  | 0 |  |  |  |  | 
| 75 |  |  |  |  |  |  | }); | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | $args->{cb} = $self->_default_cb({ | 
| 78 |  |  |  |  |  |  | filename  => $filename, | 
| 79 |  |  |  |  |  |  | verbose   => $args->{verbose}, | 
| 80 |  |  |  |  |  |  | progress  => $args->{progress}, | 
| 81 |  |  |  |  |  |  | overwrite => defined $args->{overwrite} ? $args->{overwrite} : 1, | 
| 82 | 0 | 0 |  |  |  | 0 | }) unless ref $args->{cb} eq 'CODE'; | 
|  |  | 0 |  |  |  |  |  | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  | 0 | my $res = $self->ua->get($video_url, ':content_cb' => $args->{cb}); | 
| 85 | 0 | 0 |  |  |  | 0 | croak "!! $video_id download failed: ", $res->status_line if $res->is_error; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub _format_filename { | 
| 89 | 0 |  |  | 0 |  | 0 | my ($self, $filename, $data) = @_; | 
| 90 | 0 | 0 |  |  |  | 0 | return "$data->{video_id}.$data->{suffix}" unless defined $filename; | 
| 91 | 0 | 0 |  |  |  | 0 | $filename =~ s#{([^}]+)}#$data->{$1} || "{$1}"#eg; | 
|  | 0 |  |  |  |  | 0 |  | 
| 92 | 0 |  |  |  |  | 0 | return $filename; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub _is_supported_fmt { | 
| 96 | 0 |  |  | 0 |  | 0 | my ($self, $video_id, $fmt) = @_; | 
| 97 | 0 |  |  |  |  | 0 | my $data = $self->prepare_download($video_id); | 
| 98 | 0 | 0 |  |  |  | 0 | defined($data->{video_url_map}{$fmt}{url}) ? 1 : 0; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub _progress { | 
| 102 | 0 |  |  | 0 |  | 0 | my ($self, $args, $total) = @_; | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 | 0 |  |  |  | 0 | if (not defined $args->{_progress}) { | 
| 105 | 0 | 0 |  |  |  | 0 | eval "require Term::ProgressBar" or return;    ## no critic | 
| 106 | 0 |  |  |  |  | 0 | $args->{_progress} = Term::ProgressBar->new( { count => $total, ETA => 'linear', remove => 0, fh => \*STDOUT } ); | 
| 107 | 0 | 0 |  |  |  | 0 | $args->{_progress}->minor( $total > 50_000_000 ? 1 : 0 ); | 
| 108 | 0 |  |  |  |  | 0 | $args->{_progress}->max_update_rate(1); | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 0 |  |  |  |  | 0 | $args->{_progress}->message("Total $total"); | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 |  |  |  |  | 0 | return $args->{_progress}; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub _default_cb { | 
| 117 | 0 |  |  | 0 |  | 0 | my ($self, $args) = @_; | 
| 118 | 0 |  |  |  |  | 0 | my ($file, $verbose, $overwrite, $progress) = @$args{qw/filename verbose overwrite progress/}; | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 0 | 0 | 0 |  |  | 0 | croak "file exists! $file" if -f $file and !$overwrite; | 
| 121 | 0 | 0 |  |  |  | 0 | open my $wfh, '>', $file or croak $file, " $!"; | 
| 122 | 0 |  |  |  |  | 0 | binmode $wfh; | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 | 0 |  |  |  | 0 | print "Downloading `$file`\n" if $verbose; | 
| 125 |  |  |  |  |  |  | return sub { | 
| 126 | 0 |  |  | 0 |  | 0 | my ($chunk, $res, $proto) = @_; | 
| 127 | 0 |  |  |  |  | 0 | print $wfh $chunk; # write file | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 | 0 | 0 |  |  | 0 | if ($verbose || $self->{verbose}) { | 
| 130 | 0 |  |  |  |  | 0 | my $size = tell $wfh; | 
| 131 | 0 |  |  |  |  | 0 | my $total = $res->header('Content-Length'); | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 0 | 0 |  |  |  | 0 | if ($progress) { | 
| 134 | 0 | 0 |  |  |  | 0 | if (my $p = $self->_progress($args, $total)){ | 
| 135 | 0 |  |  |  |  | 0 | $p->update($size); | 
| 136 | 0 |  |  |  |  | 0 | return; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  | else{ | 
| 139 | 0 |  |  |  |  | 0 | print "(You need Term::ProgressBar module to show progress bar with -P switch)\n"; | 
| 140 | 0 |  |  |  |  | 0 | $progress = 0; | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 0 |  |  |  |  | 0 | printf "%d/%d (%.2f%%)\r", $size, $total, $size / $total * 100; | 
| 145 | 0 | 0 |  |  |  | 0 | print "\n" if $total == $size; | 
| 146 |  |  |  |  |  |  | } | 
| 147 | 0 |  |  |  |  | 0 | }; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | sub prepare_download { | 
| 151 | 1 |  |  | 1 | 1 | 7 | my ($self, $video_id) = @_; | 
| 152 | 1 | 50 |  |  |  | 3 | croak "Usage: $self->prepare_download('[video_id|watch_url]')" unless $video_id; | 
| 153 | 1 |  |  |  |  | 3 | $video_id = $self->video_id($video_id); | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 1 | 50 |  |  |  | 6 | return $self->{cache}{$video_id} if ref $self->{cache}{$video_id} eq 'HASH'; | 
| 156 |  |  |  |  |  |  |  | 
| 157 | 1 |  |  |  |  | 3 | my ($title, $user, $video_url_map); | 
| 158 | 1 |  |  |  |  | 4 | my $content = $self->_get_content($video_id); | 
| 159 | 1 | 50 |  |  |  | 218 | if ($self->_is_new($content)) { | 
| 160 | 1 |  |  |  |  | 6 | my $args        = $self->_get_args($content); | 
| 161 | 1 |  |  |  |  | 4 | my $player_resp = JSON()->new->decode($args->{player_response}); | 
| 162 | 1 |  |  |  |  | 585 | $video_url_map  = $self->_decode_player_response($player_resp); | 
| 163 | 1 |  |  |  |  | 10 | $title          = decode_entities $player_resp->{videoDetails}{title}; | 
| 164 | 1 |  |  |  |  | 146 | $user           = decode_entities $player_resp->{videoDetails}{author}; | 
| 165 |  |  |  |  |  |  | } else { | 
| 166 | 0 |  |  |  |  | 0 | $title         = $self->_fetch_title($content); | 
| 167 | 0 |  |  |  |  | 0 | $user          = $self->_fetch_user($content); | 
| 168 | 0 |  |  |  |  | 0 | $video_url_map = $self->_fetch_video_url_map($content); | 
| 169 |  |  |  |  |  |  | } | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 1 |  |  |  |  | 6 | my $fmt_list = []; | 
| 172 |  |  |  |  |  |  | my $sorted = [ | 
| 173 |  |  |  |  |  |  | map { | 
| 174 | 2 |  |  |  |  | 6 | push @$fmt_list, $_->[0]->{fmt}; | 
| 175 | 2 |  |  |  |  | 6 | $_->[0] | 
| 176 |  |  |  |  |  |  | } sort { | 
| 177 | 1 |  |  |  |  | 5 | $b->[1] <=> $a->[1] | 
| 178 |  |  |  |  |  |  | } map { | 
| 179 | 1 |  |  |  |  | 6 | my $resolution = $_->{resolution}; | 
|  | 2 |  |  |  |  | 4 |  | 
| 180 | 2 |  |  |  |  | 13 | $resolution =~ s/(\d+)x(\d+)/$1 * $2/e; | 
|  | 2 |  |  |  |  | 11 |  | 
| 181 | 2 |  |  |  |  | 10 | [ $_, $resolution ] | 
| 182 |  |  |  |  |  |  | } values %$video_url_map, | 
| 183 |  |  |  |  |  |  | ]; | 
| 184 |  |  |  |  |  |  |  | 
| 185 | 1 |  |  |  |  | 3 | my $hq_data = $sorted->[0]; | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | return $self->{cache}{$video_id} = { | 
| 188 |  |  |  |  |  |  | video_id      => $video_id, | 
| 189 |  |  |  |  |  |  | video_url     => $hq_data->{url}, | 
| 190 |  |  |  |  |  |  | title         => $title, | 
| 191 |  |  |  |  |  |  | user          => $user, | 
| 192 |  |  |  |  |  |  | video_url_map => $video_url_map, | 
| 193 |  |  |  |  |  |  | fmt           => $hq_data->{fmt}, | 
| 194 |  |  |  |  |  |  | fmt_list      => $fmt_list, | 
| 195 |  |  |  |  |  |  | suffix        => $hq_data->{suffix}, | 
| 196 |  |  |  |  |  |  | resolution    => $hq_data->{resolution}, | 
| 197 | 1 |  |  |  |  | 25 | }; | 
| 198 |  |  |  |  |  |  | } | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | sub _mime_to_suffix { | 
| 201 | 2 |  |  | 2 |  | 14 | (my $mime = shift) =~ s{^([^;]+);.*}{$1}; | 
| 202 | 2 |  |  |  |  | 12 | my $stype = MIME::Type->new(type => $mime)->subType; | 
| 203 | 2 | 0 |  |  |  | 145 | return $stype eq 'webm' ? 'webm' | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | : $stype eq 'mp4'  ? 'mp4' | 
| 205 |  |  |  |  |  |  | : $stype eq '3gp'  ? '3gp' | 
| 206 |  |  |  |  |  |  | :                   'flv' | 
| 207 |  |  |  |  |  |  | ; | 
| 208 |  |  |  |  |  |  | } | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | sub _decode_player_response { | 
| 211 | 1 |  |  | 1 |  | 3 | my ($self, $player_data) = @_; | 
| 212 |  |  |  |  |  |  | # need to decode $player_data->{streamingData}{formats}; | 
| 213 | 1 |  |  |  |  | 2 | my $fmt_map = { map { $_->{mimeType} => join 'x', $_->{width}, $_->{height} } @{$player_data->{streamingData}{formats}} }; | 
|  | 2 |  |  |  |  | 21 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 214 | 1 |  |  |  |  | 2 | my $fmt_url_map = { map { $_->{mimeType} => $_->{url} } @{$player_data->{streamingData}{formats}} }; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 215 |  |  |  |  |  |  | # more formats in $player_data->{streamingData}{adaptiveFormats}; | 
| 216 |  |  |  |  |  |  | return +{ | 
| 217 |  |  |  |  |  |  | map { | 
| 218 | 2 |  |  |  |  | 8 | $_->{fmt} => $_, | 
| 219 |  |  |  |  |  |  | } map +{ | 
| 220 |  |  |  |  |  |  | fmt        => $_, | 
| 221 |  |  |  |  |  |  | resolution => $fmt_map->{$_}, | 
| 222 | 1 |  |  |  |  | 8 | url        => $fmt_url_map->{$_}, | 
| 223 |  |  |  |  |  |  | suffix     => _mime_to_suffix($_), | 
| 224 |  |  |  |  |  |  | }, keys %$fmt_map | 
| 225 |  |  |  |  |  |  | }; | 
| 226 |  |  |  |  |  |  | } | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | sub _fetch_title { | 
| 229 | 0 |  |  | 0 |  | 0 | my ($self, $content) = @_; | 
| 230 |  |  |  |  |  |  |  | 
| 231 | 0 | 0 |  |  |  | 0 | my ($title) = $content =~ // or return; | 
| 232 | 0 |  |  |  |  | 0 | return decode_entities($title); | 
| 233 |  |  |  |  |  |  | } | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | sub _fetch_user { | 
| 236 | 1 |  |  | 1 |  | 11 | my ($self, $content) = @_; | 
| 237 |  |  |  |  |  |  |  | 
| 238 | 1 | 50 |  |  |  | 10 | if( $content =~ / |  | 0 |  |  |  |  |  | 
| 239 | 1 |  |  |  |  | 11 | return decode_entities($1); | 
| 240 |  |  |  |  |  |  | }elsif( $content =~ /","author":"([^"]+)","/ ){ | 
| 241 | 0 |  |  |  |  | 0 | return decode_entities($1); | 
| 242 |  |  |  |  |  |  | }else{ | 
| 243 | 0 |  |  |  |  | 0 | return; | 
| 244 |  |  |  |  |  |  | } | 
| 245 |  |  |  |  |  |  | } | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | sub _fetch_video_url_map { | 
| 248 | 0 |  |  | 0 |  | 0 | my ($self, $content) = @_; | 
| 249 |  |  |  |  |  |  |  | 
| 250 | 0 |  |  |  |  | 0 | my $args = $self->_get_args($content); | 
| 251 | 0 | 0 | 0 |  |  | 0 | unless ($args->{fmt_list} and $args->{url_encoded_fmt_stream_map}) { | 
| 252 | 0 |  |  |  |  | 0 | croak 'failed to find video urls'; | 
| 253 |  |  |  |  |  |  | } | 
| 254 |  |  |  |  |  |  |  | 
| 255 | 0 |  |  |  |  | 0 | my $fmt_map     = _parse_fmt_map($args->{fmt_list}); | 
| 256 | 0 |  |  |  |  | 0 | my $fmt_url_map = _parse_stream_map($args->{url_encoded_fmt_stream_map}); | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | my $video_url_map = +{ | 
| 259 |  |  |  |  |  |  | map { | 
| 260 | 0 |  |  |  |  | 0 | $_->{fmt} => $_, | 
| 261 |  |  |  |  |  |  | } map +{ | 
| 262 |  |  |  |  |  |  | fmt        => $_, | 
| 263 |  |  |  |  |  |  | resolution => $fmt_map->{$_}, | 
| 264 | 0 |  |  |  |  | 0 | url        => $fmt_url_map->{$_}, | 
| 265 |  |  |  |  |  |  | suffix     => _suffix($_), | 
| 266 |  |  |  |  |  |  | }, keys %$fmt_map | 
| 267 |  |  |  |  |  |  | }; | 
| 268 |  |  |  |  |  |  |  | 
| 269 | 0 |  |  |  |  | 0 | return $video_url_map; | 
| 270 |  |  |  |  |  |  | } | 
| 271 |  |  |  |  |  |  |  | 
| 272 |  |  |  |  |  |  | sub _get_content { | 
| 273 | 0 |  |  | 0 |  | 0 | my ($self, $video_id) = @_; | 
| 274 |  |  |  |  |  |  |  | 
| 275 | 0 |  |  |  |  | 0 | my $url = "$base_url$video_id"; | 
| 276 |  |  |  |  |  |  |  | 
| 277 | 0 |  |  |  |  | 0 | my $req = HTTP::Request->new; | 
| 278 | 0 |  |  |  |  | 0 | $req->method('GET'); | 
| 279 | 0 |  |  |  |  | 0 | $req->uri($url); | 
| 280 | 0 |  |  |  |  | 0 | $req->header('Accept-Language' => 'en-US'); | 
| 281 |  |  |  |  |  |  |  | 
| 282 | 0 |  |  |  |  | 0 | my $res = $self->ua->request($req); | 
| 283 | 0 | 0 |  |  |  | 0 | croak "GET $url failed. status: ", $res->status_line if $res->is_error; | 
| 284 |  |  |  |  |  |  |  | 
| 285 | 0 |  |  |  |  | 0 | return $res->content; | 
| 286 |  |  |  |  |  |  | } | 
| 287 |  |  |  |  |  |  |  | 
| 288 |  |  |  |  |  |  | sub _get_args { | 
| 289 | 2 |  |  | 2 |  | 7 | my ($self, $content) = @_; | 
| 290 |  |  |  |  |  |  |  | 
| 291 | 2 |  |  |  |  | 3 | my $data; | 
| 292 | 2 |  |  |  |  | 231 | for my $line (split "\n", $content) { | 
| 293 | 2 | 50 |  |  |  | 8 | next unless $line; | 
| 294 | 2 | 50 |  |  |  | 1560 | if ($line =~ /the uploader has not made this video available in your country/i) { | 
|  |  | 50 |  |  |  |  |  | 
| 295 | 0 |  |  |  |  | 0 | croak 'Video not available in your country'; | 
| 296 |  |  |  |  |  |  | } | 
| 297 |  |  |  |  |  |  | elsif ($line =~ /^.+ytplayer\.config\s*=\s*(\{.*})/) { | 
| 298 | 2 |  |  |  |  | 9 | ($data, undef) = JSON->new->utf8(1)->decode_prefix($1); | 
| 299 | 2 |  |  |  |  | 810 | last; | 
| 300 |  |  |  |  |  |  | } | 
| 301 |  |  |  |  |  |  | } | 
| 302 |  |  |  |  |  |  |  | 
| 303 | 2 | 50 |  |  |  | 11 | croak 'failed to extract JSON data' unless $data->{args}; | 
| 304 |  |  |  |  |  |  |  | 
| 305 | 2 |  |  |  |  | 8 | return $data->{args}; | 
| 306 |  |  |  |  |  |  | } | 
| 307 |  |  |  |  |  |  |  | 
| 308 |  |  |  |  |  |  | sub _is_new { | 
| 309 | 1 |  |  | 1 |  | 4 | my ($self, $content) = @_; | 
| 310 | 1 |  |  |  |  | 3 | my $args = $self->_get_args($content); | 
| 311 | 1 | 50 | 33 |  |  | 25 | return 1 unless ($args->{fmt_list} and $args->{url_encoded_fmt_stream_map}); | 
| 312 | 0 |  |  |  |  | 0 | return 0; | 
| 313 |  |  |  |  |  |  | } | 
| 314 |  |  |  |  |  |  |  | 
| 315 |  |  |  |  |  |  | sub _parse_fmt_map { | 
| 316 | 0 |  |  | 0 |  | 0 | my $param = shift; | 
| 317 | 0 |  |  |  |  | 0 | my $fmt_map = {}; | 
| 318 | 0 |  |  |  |  | 0 | for my $stuff (split ',', $param) { | 
| 319 | 0 |  |  |  |  | 0 | my ($fmt, $resolution) = split '/', $stuff; | 
| 320 | 0 |  |  |  |  | 0 | $fmt_map->{$fmt} = $resolution; | 
| 321 |  |  |  |  |  |  | } | 
| 322 |  |  |  |  |  |  |  | 
| 323 | 0 |  |  |  |  | 0 | return $fmt_map; | 
| 324 |  |  |  |  |  |  | } | 
| 325 |  |  |  |  |  |  |  | 
| 326 |  |  |  |  |  |  | sub _sigdecode { | 
| 327 | 0 |  |  | 0 |  | 0 | my @s = @_; | 
| 328 |  |  |  |  |  |  |  | 
| 329 |  |  |  |  |  |  | # based on youtube_dl/extractor/youtube.py from yt-dl.org | 
| 330 | 0 | 0 |  |  |  | 0 | if (@s == 93) { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 331 | 0 |  |  |  |  | 0 | return (reverse(@s[30..86]), $s[88], reverse(@s[6..28])); | 
| 332 |  |  |  |  |  |  | } elsif (@s == 92) { | 
| 333 | 0 |  |  |  |  | 0 | return ($s[25], @s[3..24], $s[0], @s[26..41], $s[79], @s[43..78], $s[91], @s[80..82]); | 
| 334 |  |  |  |  |  |  | } elsif (@s == 91) { | 
| 335 | 0 |  |  |  |  | 0 | return (reverse(@s[28..84]), $s[86], reverse(@s[6..26])); | 
| 336 |  |  |  |  |  |  | } elsif (@s == 90) { | 
| 337 | 0 |  |  |  |  | 0 | return ($s[25], @s[3..24], $s[2], @s[26..39], $s[77], @s[41..76], $s[89], @s[78..80]); | 
| 338 |  |  |  |  |  |  | } elsif (@s == 89) { | 
| 339 | 0 |  |  |  |  | 0 | return (reverse(@s[79..84]), $s[87], reverse(@s[61..77]), $s[0], reverse(@s[4..59])); | 
| 340 |  |  |  |  |  |  | } elsif (@s == 88) { | 
| 341 | 0 |  |  |  |  | 0 | return (@s[7..27], $s[87], @s[29..44], $s[55], @s[46..54], $s[2], @s[56..86], $s[28]); | 
| 342 |  |  |  |  |  |  | } elsif (@s == 87) { | 
| 343 | 0 |  |  |  |  | 0 | return (@s[6..26], $s[4], @s[28..38], $s[27], @s[40..58], $s[2], @s[60..86]); | 
| 344 |  |  |  |  |  |  | } elsif (@s == 86) { | 
| 345 | 0 |  |  |  |  | 0 | return (@s[4..30], $s[3], @s[32..84]); | 
| 346 |  |  |  |  |  |  | } elsif (@s == 85) { | 
| 347 | 0 |  |  |  |  | 0 | return (@s[3..10], $s[0], @s[12..54], $s[84], @s[56..83]); | 
| 348 |  |  |  |  |  |  | } elsif (@s == 84) { | 
| 349 | 0 |  |  |  |  | 0 | return (reverse(@s[71..78]), $s[14], reverse(@s[38..69]), $s[70], reverse(@s[15..36]), $s[80], reverse(@s[0..13])); | 
| 350 |  |  |  |  |  |  | } elsif (@s == 83) { | 
| 351 | 0 |  |  |  |  | 0 | return (reverse(@s[64..80]), $s[0], reverse(@s[1..62]), $s[63]); | 
| 352 |  |  |  |  |  |  | } elsif (@s == 82) { | 
| 353 | 0 |  |  |  |  | 0 | return (reverse(@s[38..80]), $s[7], reverse(@s[8..36]), $s[0], reverse(@s[1..6]), $s[37]); | 
| 354 |  |  |  |  |  |  | } elsif (@s == 81) { | 
| 355 | 0 |  |  |  |  | 0 | return ($s[56], reverse(@s[57..79]), $s[41], reverse(@s[42..55]), $s[80], reverse(@s[35..40]), $s[0], reverse(@s[30..33]), $s[34], reverse(@s[10..28]), $s[29], reverse(@s[1..8]), $s[9]); | 
| 356 |  |  |  |  |  |  | } elsif (@s == 80) { | 
| 357 | 0 |  |  |  |  | 0 | return (@s[1..18], $s[0], @s[20..67], $s[19], @s[69..79]); | 
| 358 |  |  |  |  |  |  | } elsif (@s == 79) { | 
| 359 | 0 |  |  |  |  | 0 | return ($s[54], reverse(@s[55..77]), $s[39], reverse(@s[40..53]), $s[78], reverse(@s[35..38]), $s[0], reverse(@s[30..33]), $s[34], reverse(@s[10..28]), $s[29], reverse(@s[1..8]), $s[9]); | 
| 360 |  |  |  |  |  |  | } | 
| 361 |  |  |  |  |  |  |  | 
| 362 | 0 |  |  |  |  | 0 | return ();    # fail | 
| 363 |  |  |  |  |  |  | } | 
| 364 |  |  |  |  |  |  |  | 
| 365 |  |  |  |  |  |  | sub _getsig { | 
| 366 | 0 |  |  | 0 |  | 0 | my $sig = shift; | 
| 367 | 0 | 0 |  |  |  | 0 | croak 'Unable to find signature' unless $sig; | 
| 368 | 0 |  |  |  |  | 0 | my @sig = _sigdecode(split(//, $sig)); | 
| 369 | 0 | 0 |  |  |  | 0 | croak "Unable to decode signature $sig of length " . length($sig) unless @sig; | 
| 370 | 0 |  |  |  |  | 0 | return join('', @sig); | 
| 371 |  |  |  |  |  |  | } | 
| 372 |  |  |  |  |  |  |  | 
| 373 |  |  |  |  |  |  | sub _parse_stream_map { | 
| 374 | 0 |  |  | 0 |  | 0 | my $param       = shift; | 
| 375 | 0 |  |  |  |  | 0 | my $fmt_url_map = {}; | 
| 376 | 0 |  |  |  |  | 0 | for my $stuff (split ',', $param) { | 
| 377 | 0 |  |  |  |  | 0 | my $uri = URI->new; | 
| 378 | 0 |  |  |  |  | 0 | $uri->query($stuff); | 
| 379 | 0 |  |  |  |  | 0 | my $query = +{ $uri->query_form }; | 
| 380 | 0 |  | 0 |  |  | 0 | my $sig = $query->{sig} || ($query->{s} ? _getsig($query->{s}) : undef); | 
| 381 | 0 |  |  |  |  | 0 | my $url = $query->{url}; | 
| 382 | 0 | 0 |  |  |  | 0 | $fmt_url_map->{$query->{itag}} = $url . ($sig ? '&signature='.$sig : ''); | 
| 383 |  |  |  |  |  |  | } | 
| 384 |  |  |  |  |  |  |  | 
| 385 | 0 |  |  |  |  | 0 | return $fmt_url_map; | 
| 386 |  |  |  |  |  |  | } | 
| 387 |  |  |  |  |  |  |  | 
| 388 |  |  |  |  |  |  | sub ua { | 
| 389 | 0 |  |  | 0 | 1 | 0 | my ($self, $ua) = @_; | 
| 390 | 0 | 0 |  |  |  | 0 | return $self->{ua} unless $ua; | 
| 391 | 0 | 0 |  |  |  | 0 | croak "Usage: $self->ua(\$LWP_LIKE_OBJECT)" unless eval { $ua->isa('LWP::UserAgent') }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 392 | 0 |  |  |  |  | 0 | $self->{ua} = $ua; | 
| 393 |  |  |  |  |  |  | } | 
| 394 |  |  |  |  |  |  |  | 
| 395 |  |  |  |  |  |  | sub _suffix { | 
| 396 | 0 |  |  | 0 |  | 0 | my $fmt = shift; | 
| 397 | 0 | 0 |  |  |  | 0 | return $fmt =~ /43|44|45|46/ ? 'webm' | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 398 |  |  |  |  |  |  | : $fmt =~ /18|22|37|38/ ? 'mp4' | 
| 399 |  |  |  |  |  |  | : $fmt =~ /13|17/       ? '3gp' | 
| 400 |  |  |  |  |  |  | :                         'flv' | 
| 401 |  |  |  |  |  |  | ; | 
| 402 |  |  |  |  |  |  | } | 
| 403 |  |  |  |  |  |  |  | 
| 404 |  |  |  |  |  |  | sub video_id { | 
| 405 | 17 |  |  | 17 | 1 | 37 | my ($self, $stuff) = @_; | 
| 406 | 17 | 50 |  |  |  | 41 | return unless $stuff; | 
| 407 | 17 | 100 |  |  |  | 153 | if ($stuff =~ m{/.*?[?&;!](?:v|video_id)=([^?=/;]+)}) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 408 | 8 |  |  |  |  | 39 | return $1; | 
| 409 |  |  |  |  |  |  | } | 
| 410 |  |  |  |  |  |  | elsif ($stuff =~ m{/(?:e|v|embed)/([^?=/;]+)}) { | 
| 411 | 4 |  |  |  |  | 19 | return $1; | 
| 412 |  |  |  |  |  |  | } | 
| 413 |  |  |  |  |  |  | elsif ($stuff =~ m{#p/(?:u|search)/\d+/([^&?/]+)}) { | 
| 414 | 1 |  |  |  |  | 5 | return $1; | 
| 415 |  |  |  |  |  |  | } | 
| 416 |  |  |  |  |  |  | elsif ($stuff =~ m{youtu.be/([^?=/;]+)}) { | 
| 417 | 1 |  |  |  |  | 5 | return $1; | 
| 418 |  |  |  |  |  |  | } | 
| 419 |  |  |  |  |  |  | else { | 
| 420 | 3 |  |  |  |  | 11 | return $stuff; | 
| 421 |  |  |  |  |  |  | } | 
| 422 |  |  |  |  |  |  | } | 
| 423 |  |  |  |  |  |  |  | 
| 424 |  |  |  |  |  |  | sub playlist_id { | 
| 425 | 10 |  |  | 10 | 1 | 34 | my ($self, $stuff) = @_; | 
| 426 | 10 | 50 |  |  |  | 27 | return unless $stuff; | 
| 427 | 10 | 100 |  |  |  | 75 | if ($stuff =~ m{/.*?[?&;!]list=([^?=/;]+)}) { | 
|  |  | 100 |  |  |  |  |  | 
| 428 | 4 |  |  |  |  | 21 | return $1; | 
| 429 |  |  |  |  |  |  | } | 
| 430 |  |  |  |  |  |  | elsif ($stuff =~ m{^\s*([FP]L[\w\-]+)\s*$}) { | 
| 431 | 3 |  |  |  |  | 15 | return $1; | 
| 432 |  |  |  |  |  |  | } | 
| 433 | 3 |  |  |  |  | 10 | return $stuff; | 
| 434 |  |  |  |  |  |  | } | 
| 435 |  |  |  |  |  |  |  | 
| 436 |  |  |  |  |  |  | sub user_id { | 
| 437 | 4 |  |  | 4 | 1 | 12 | my ($self, $stuff) = @_; | 
| 438 | 4 | 50 |  |  |  | 12 | return unless $stuff; | 
| 439 | 4 | 100 |  |  |  | 23 | if ($stuff =~ m{/user/([^?=/;]+)}) { | 
| 440 | 3 |  |  |  |  | 16 | return $1; | 
| 441 |  |  |  |  |  |  | } | 
| 442 | 1 |  |  |  |  | 4 | return $stuff; | 
| 443 |  |  |  |  |  |  | } | 
| 444 |  |  |  |  |  |  |  | 
| 445 |  |  |  |  |  |  | sub playlist { | 
| 446 | 0 |  |  | 0 | 1 |  | my ( $self, $id, $args ) = @_; | 
| 447 |  |  |  |  |  |  |  | 
| 448 | 0 |  |  |  |  |  | my $fetchCnt = 1; | 
| 449 | 0 |  |  |  |  |  | my $html     = $self->_fetch_playlist($id); | 
| 450 |  |  |  |  |  |  |  | 
| 451 | 0 |  |  |  |  |  | my ( $videos, $nextUrl ) = $self->_find_playlist_videos($html); | 
| 452 |  |  |  |  |  |  |  | 
| 453 | 0 | 0 |  |  |  |  | return $videos unless $nextUrl; | 
| 454 | 0 | 0 | 0 |  |  |  | return $videos if $args && $args->{limit} && $fetchCnt >= $args->{limit}; | 
|  |  |  | 0 |  |  |  |  | 
| 455 |  |  |  |  |  |  |  | 
| 456 | 0 |  |  |  |  |  | while ($nextUrl) { | 
| 457 | 0 |  |  |  |  |  | $fetchCnt++; | 
| 458 | 0 |  |  |  |  |  | my $json = $self->_fetch_playlist_json($nextUrl); | 
| 459 |  |  |  |  |  |  |  | 
| 460 | 0 |  |  |  |  |  | ( my $moreVideos, $nextUrl ) | 
| 461 |  |  |  |  |  |  | = $self->_find_playlist_json_videos($json); | 
| 462 |  |  |  |  |  |  |  | 
| 463 | 0 |  |  |  |  |  | push( @$videos, @$moreVideos ); | 
| 464 |  |  |  |  |  |  |  | 
| 465 | 0 | 0 |  |  |  |  | last unless $nextUrl; | 
| 466 | 0 | 0 | 0 |  |  |  | last if $args && $args->{limit} && $fetchCnt >= $args->{limit}; | 
|  |  |  | 0 |  |  |  |  | 
| 467 |  |  |  |  |  |  | } | 
| 468 |  |  |  |  |  |  |  | 
| 469 | 0 |  |  |  |  |  | return $videos; | 
| 470 |  |  |  |  |  |  | } | 
| 471 |  |  |  |  |  |  |  | 
| 472 |  |  |  |  |  |  | sub _fetch_playlist { | 
| 473 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 474 | 0 |  |  |  |  |  | my $id   = shift; | 
| 475 | 0 |  |  |  |  |  | my $url  = 'https://www.youtube.com/playlist?list=' . $id; | 
| 476 |  |  |  |  |  |  |  | 
| 477 | 0 | 0 |  |  |  |  | print 'Fetching playlist page ' . $url . ' ... ' if $self->{verbose}; | 
| 478 |  |  |  |  |  |  |  | 
| 479 | 0 |  |  |  |  |  | my $res = $self->ua->get($url); | 
| 480 |  |  |  |  |  |  |  | 
| 481 | 0 | 0 |  |  |  |  | croak(    'Error: fetch_playlist failed for url:' | 
| 482 |  |  |  |  |  |  | . $url | 
| 483 |  |  |  |  |  |  | . ' status:' | 
| 484 |  |  |  |  |  |  | . $res->status_line ) | 
| 485 |  |  |  |  |  |  | unless $res->is_success; | 
| 486 |  |  |  |  |  |  |  | 
| 487 | 0 | 0 |  |  |  |  | print "ok \n" if $self->{verbose}; | 
| 488 |  |  |  |  |  |  |  | 
| 489 | 0 |  |  |  |  |  | return $res->decoded_content; | 
| 490 |  |  |  |  |  |  | } | 
| 491 |  |  |  |  |  |  |  | 
| 492 |  |  |  |  |  |  | sub _find_playlist_videos { | 
| 493 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 494 | 0 |  |  |  |  |  | my $html = shift; | 
| 495 |  |  |  |  |  |  |  | 
| 496 |  |  |  |  |  |  | ## find JSON blob | 
| 497 | 0 |  |  |  |  |  | my $line; | 
| 498 | 0 |  |  |  |  |  | for ( split( "\n", $html ) ) { | 
| 499 | 0 | 0 |  |  |  |  | if ( $_ =~ /window\["ytInitialData"\]/ ) { | 
| 500 | 0 |  |  |  |  |  | $line = $_; | 
| 501 | 0 |  |  |  |  |  | last; | 
| 502 |  |  |  |  |  |  | } | 
| 503 |  |  |  |  |  |  | } | 
| 504 |  |  |  |  |  |  |  | 
| 505 | 0 | 0 |  |  |  |  | die "Error: could not find YT JSON data!" unless $line; | 
| 506 |  |  |  |  |  |  |  | 
| 507 | 0 |  |  |  |  |  | my $start = index( $line, '{' ); | 
| 508 | 0 |  |  |  |  |  | my $end   = rindex( $line, '}' ); | 
| 509 | 0 |  |  |  |  |  | my $json  = substr( $line, $start, $end - $start + 1 ); | 
| 510 |  |  |  |  |  |  |  | 
| 511 | 0 |  |  |  |  |  | my $ref = JSON()->new->utf8(0)->decode($json); | 
| 512 |  |  |  |  |  |  |  | 
| 513 |  |  |  |  |  |  | ## drill into YT's data structure | 
| 514 |  |  |  |  |  |  | die "Error: YT JSON data not structed as expected!" | 
| 515 |  |  |  |  |  |  | unless $ref->{contents} | 
| 516 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer} | 
| 517 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs} | 
| 518 |  |  |  |  |  |  | && ref( $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs} ) eq 'ARRAY' | 
| 519 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0] | 
| 520 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer} | 
| 521 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content} | 
| 522 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer} | 
| 523 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents} | 
| 524 |  |  |  |  |  |  | && ref( | 
| 525 |  |  |  |  |  |  | $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0] | 
| 526 |  |  |  |  |  |  | ->{tabRenderer}->{content}->{sectionListRenderer}->{contents} ) | 
| 527 |  |  |  |  |  |  | eq 'ARRAY' | 
| 528 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0] | 
| 529 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0]->{itemSectionRenderer} | 
| 530 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0]->{itemSectionRenderer}->{contents} | 
| 531 |  |  |  |  |  |  | && ref( | 
| 532 |  |  |  |  |  |  | $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0]->{itemSectionRenderer}->{contents} ) eq 'ARRAY' | 
| 533 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0]->{itemSectionRenderer}->{contents}->[0] | 
| 534 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0]->{itemSectionRenderer}->{contents}->[0]->{playlistVideoListRenderer} | 
| 535 |  |  |  |  |  |  | && $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0]->{itemSectionRenderer}->{contents}->[0]->{playlistVideoListRenderer}->{contents} | 
| 536 |  |  |  |  |  |  | && ref( | 
| 537 | 0 | 0 | 0 |  |  |  | $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0]->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0]->{itemSectionRenderer}->{contents}->[0]->{playlistVideoListRenderer}->{contents} ) eq 'ARRAY'; | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 538 |  |  |  |  |  |  |  | 
| 539 |  |  |  |  |  |  | my $playlistVideoListRenderer | 
| 540 |  |  |  |  |  |  | = $ref->{contents}->{twoColumnBrowseResultsRenderer}->{tabs}->[0] | 
| 541 |  |  |  |  |  |  | ->{tabRenderer}->{content}->{sectionListRenderer}->{contents}->[0] | 
| 542 | 0 |  |  |  |  |  | ->{itemSectionRenderer}->{contents}->[0]->{playlistVideoListRenderer}; | 
| 543 |  |  |  |  |  |  |  | 
| 544 |  |  |  |  |  |  | ## weed out videos | 
| 545 | 0 |  |  |  |  |  | my @videos; | 
| 546 | 0 |  |  |  |  |  | my $cnt = 0; | 
| 547 | 0 |  |  |  |  |  | for my $renderer ( @{ $playlistVideoListRenderer->{contents} } ) { | 
|  | 0 |  |  |  |  |  |  | 
| 548 | 0 |  |  |  |  |  | $cnt++; | 
| 549 | 0 |  |  |  |  |  | my $video = $renderer->{playlistVideoRenderer}; | 
| 550 |  |  |  |  |  |  |  | 
| 551 |  |  |  |  |  |  | push( | 
| 552 |  |  |  |  |  |  | @videos, | 
| 553 |  |  |  |  |  |  | { | 
| 554 |  |  |  |  |  |  | id      => $video->{videoId}, | 
| 555 |  |  |  |  |  |  | runtime => $video->{lengthSeconds}, | 
| 556 | 0 |  | 0 |  |  |  | title   => ( $video->{title}->{simpleText} || 'undef' ) | 
| 557 |  |  |  |  |  |  | ,    # encoded in Perl internal | 
| 558 |  |  |  |  |  |  | } | 
| 559 |  |  |  |  |  |  | ); | 
| 560 |  |  |  |  |  |  |  | 
| 561 | 0 | 0 |  |  |  |  | if ( $self->{verbose} ) { | 
| 562 | 0 |  |  |  |  |  | print " $cnt id:" . $video->{videoId}; | 
| 563 | 0 |  | 0 |  |  |  | print " runtime:" . ( $video->{lengthSeconds} || 'undef' ); | 
| 564 |  |  |  |  |  |  | print " title:" | 
| 565 |  |  |  |  |  |  | . Encode::encode_utf8( $video->{title}->{simpleText} | 
| 566 | 0 |  | 0 |  |  |  | || 'undef' ); | 
| 567 | 0 |  |  |  |  |  | print "\n"; | 
| 568 |  |  |  |  |  |  | } | 
| 569 |  |  |  |  |  |  | } | 
| 570 |  |  |  |  |  |  |  | 
| 571 |  |  |  |  |  |  | ## next page | 
| 572 | 0 |  |  |  |  |  | my $nextUrl; | 
| 573 | 0 | 0 | 0 |  |  |  | if (   $playlistVideoListRenderer->{continuations} | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 574 |  |  |  |  |  |  | && ref( $playlistVideoListRenderer->{continuations} ) eq 'ARRAY' | 
| 575 |  |  |  |  |  |  | && $playlistVideoListRenderer->{continuations}->[0] | 
| 576 |  |  |  |  |  |  | && $playlistVideoListRenderer->{continuations}->[0]->{nextContinuationData} ) { | 
| 577 |  |  |  |  |  |  | my $next = $playlistVideoListRenderer->{continuations}->[0] | 
| 578 | 0 |  |  |  |  |  | ->{nextContinuationData}; | 
| 579 | 0 |  |  |  |  |  | my $continuation = $next->{continuation}; | 
| 580 | 0 |  |  |  |  |  | my $ctoken       = $continuation; | 
| 581 | 0 |  |  |  |  |  | my $itct         = $next->{clickTrackingParams}; | 
| 582 |  |  |  |  |  |  |  | 
| 583 | 0 | 0 |  |  |  |  | if ( $self->{verbose} ) { | 
| 584 | 0 |  |  |  |  |  | print " next page continuation data:\n cont:" | 
| 585 |  |  |  |  |  |  | . $continuation . "\n"; | 
| 586 | 0 |  |  |  |  |  | print " itct:" . $itct . "\n"; | 
| 587 | 0 |  |  |  |  |  | print "\n"; | 
| 588 |  |  |  |  |  |  | } | 
| 589 |  |  |  |  |  |  |  | 
| 590 |  |  |  |  |  |  | $nextUrl | 
| 591 | 0 |  |  |  |  |  | = 'https://www.youtube.com/browse_ajax?ctoken=' | 
| 592 |  |  |  |  |  |  | . $ctoken | 
| 593 |  |  |  |  |  |  | . '&continuation=' | 
| 594 |  |  |  |  |  |  | . $continuation | 
| 595 |  |  |  |  |  |  | . '&itct=' | 
| 596 |  |  |  |  |  |  | . $itct; | 
| 597 |  |  |  |  |  |  | } | 
| 598 |  |  |  |  |  |  |  | 
| 599 | 0 |  |  |  |  |  | return ( \@videos, $nextUrl ); | 
| 600 |  |  |  |  |  |  | } | 
| 601 |  |  |  |  |  |  |  | 
| 602 |  |  |  |  |  |  | sub _fetch_playlist_json { | 
| 603 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 604 | 0 |  |  |  |  |  | my $url = shift; | 
| 605 |  |  |  |  |  |  |  | 
| 606 | 0 | 0 |  |  |  |  | print 'Fetching playlist next page '. $url .' ... ' if $self->{verbose}; | 
| 607 |  |  |  |  |  |  |  | 
| 608 | 0 |  |  |  |  |  | my $res = $self->ua->get($url, | 
| 609 |  |  |  |  |  |  | # thanks youtube-dl for the hint towards these headers | 
| 610 |  |  |  |  |  |  | 'x-youtube-client-name' => '1', | 
| 611 |  |  |  |  |  |  | 'x-youtube-client-version' => '2.20200825.04.00', # version '1.20200609.04.02' will return a HTML blob in content_html, this version returns only JSON | 
| 612 |  |  |  |  |  |  | ); | 
| 613 |  |  |  |  |  |  |  | 
| 614 | 0 | 0 |  |  |  |  | croak('Error: fetch_playlist_json failed for url:'. $url .' status:'. $res->status_line) unless $res->is_success; | 
| 615 |  |  |  |  |  |  |  | 
| 616 | 0 | 0 |  |  |  |  | print "ok \n" if $self->{verbose}; | 
| 617 |  |  |  |  |  |  |  | 
| 618 | 0 |  |  |  |  |  | return $res->decoded_content; | 
| 619 |  |  |  |  |  |  | } | 
| 620 |  |  |  |  |  |  |  | 
| 621 |  |  |  |  |  |  | sub _find_playlist_json_videos { | 
| 622 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 623 | 0 |  |  |  |  |  | my $json = shift; | 
| 624 |  |  |  |  |  |  |  | 
| 625 | 0 |  |  |  |  |  | my $ref = JSON()->new->utf8(1)->decode($json); | 
| 626 |  |  |  |  |  |  |  | 
| 627 |  |  |  |  |  |  | ## drill into YT's data structure | 
| 628 |  |  |  |  |  |  | die "Error: YT JSON data not structed as expected!" | 
| 629 |  |  |  |  |  |  | unless ref($ref) eq 'ARRAY' | 
| 630 |  |  |  |  |  |  | && $ref->[1] | 
| 631 |  |  |  |  |  |  | && $ref->[1]->{response} | 
| 632 |  |  |  |  |  |  | && $ref->[1]->{response}->{continuationContents} | 
| 633 |  |  |  |  |  |  | && $ref->[1]->{response}->{continuationContents}->{playlistVideoListContinuation} | 
| 634 |  |  |  |  |  |  | && $ref->[1]->{response}->{continuationContents}->{playlistVideoListContinuation}->{contents} | 
| 635 | 0 | 0 | 0 |  |  |  | && ref( $ref->[1]->{response}->{continuationContents}->{playlistVideoListContinuation}->{contents} ) eq 'ARRAY'; | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 636 |  |  |  |  |  |  |  | 
| 637 |  |  |  |  |  |  | my $playlistVideoListContinuation | 
| 638 |  |  |  |  |  |  | = $ref->[1]->{response}->{continuationContents} | 
| 639 | 0 |  |  |  |  |  | ->{playlistVideoListContinuation}; | 
| 640 |  |  |  |  |  |  |  | 
| 641 |  |  |  |  |  |  | ## weed out videos | 
| 642 | 0 |  |  |  |  |  | my @videos; | 
| 643 | 0 |  |  |  |  |  | my $cnt = 0; | 
| 644 | 0 |  |  |  |  |  | for my $renderer ( @{ $playlistVideoListContinuation->{contents} } ) { | 
|  | 0 |  |  |  |  |  |  | 
| 645 | 0 |  |  |  |  |  | $cnt++; | 
| 646 | 0 |  |  |  |  |  | my $video = $renderer->{playlistVideoRenderer}; | 
| 647 |  |  |  |  |  |  |  | 
| 648 |  |  |  |  |  |  | push( | 
| 649 |  |  |  |  |  |  | @videos, | 
| 650 |  |  |  |  |  |  | { | 
| 651 |  |  |  |  |  |  | id      => $video->{videoId}, | 
| 652 |  |  |  |  |  |  | runtime => $video->{lengthSeconds}, | 
| 653 | 0 |  | 0 |  |  |  | title   => ( $video->{title}->{simpleText} || 'undef' ) | 
| 654 |  |  |  |  |  |  | ,    # encoded in Perl internal | 
| 655 |  |  |  |  |  |  | } | 
| 656 |  |  |  |  |  |  | ); | 
| 657 |  |  |  |  |  |  |  | 
| 658 | 0 | 0 |  |  |  |  | if ( $self->{verbose} ) { | 
| 659 | 0 |  |  |  |  |  | print " $cnt id:" . $video->{videoId}; | 
| 660 | 0 |  | 0 |  |  |  | print " runtime:" . ( $video->{lengthSeconds} || 'undef' ); | 
| 661 |  |  |  |  |  |  | print " title:" | 
| 662 |  |  |  |  |  |  | . Encode::encode_utf8( $video->{title}->{simpleText} | 
| 663 | 0 |  | 0 |  |  |  | || 'undef' ); | 
| 664 | 0 |  |  |  |  |  | print "\n"; | 
| 665 |  |  |  |  |  |  | } | 
| 666 |  |  |  |  |  |  | } | 
| 667 |  |  |  |  |  |  |  | 
| 668 |  |  |  |  |  |  | ## next page | 
| 669 | 0 |  |  |  |  |  | my $nextUrl; | 
| 670 | 0 | 0 | 0 |  |  |  | if (   $playlistVideoListContinuation->{continuations} | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 671 |  |  |  |  |  |  | && ref( $playlistVideoListContinuation->{continuations} ) eq 'ARRAY' | 
| 672 |  |  |  |  |  |  | && $playlistVideoListContinuation->{continuations}->[0] | 
| 673 |  |  |  |  |  |  | && $playlistVideoListContinuation->{continuations}->[0]->{nextContinuationData} ) { | 
| 674 | 0 |  |  |  |  |  | my $next = $playlistVideoListContinuation->{continuations}->[0]->{nextContinuationData}; | 
| 675 | 0 |  |  |  |  |  | my $continuation = $next->{continuation}; | 
| 676 | 0 |  |  |  |  |  | my $ctoken       = $continuation; | 
| 677 | 0 |  |  |  |  |  | my $itct         = $next->{clickTrackingParams}; | 
| 678 |  |  |  |  |  |  |  | 
| 679 | 0 | 0 |  |  |  |  | if ( $self->{verbose} ) { | 
| 680 | 0 |  |  |  |  |  | print " next page continuation data:\n cont:" | 
| 681 |  |  |  |  |  |  | . $continuation . "\n"; | 
| 682 | 0 |  |  |  |  |  | print " itct:" . $itct . "\n"; | 
| 683 | 0 |  |  |  |  |  | print "\n"; | 
| 684 |  |  |  |  |  |  | } | 
| 685 |  |  |  |  |  |  |  | 
| 686 |  |  |  |  |  |  | $nextUrl | 
| 687 | 0 |  |  |  |  |  | = 'https://www.youtube.com/browse_ajax?ctoken=' | 
| 688 |  |  |  |  |  |  | . $ctoken | 
| 689 |  |  |  |  |  |  | . '&continuation=' | 
| 690 |  |  |  |  |  |  | . $continuation | 
| 691 |  |  |  |  |  |  | . '&itct=' | 
| 692 |  |  |  |  |  |  | . $itct; | 
| 693 |  |  |  |  |  |  | } | 
| 694 |  |  |  |  |  |  |  | 
| 695 | 0 |  |  |  |  |  | return ( \@videos, $nextUrl ); | 
| 696 |  |  |  |  |  |  | } | 
| 697 |  |  |  |  |  |  |  | 
| 698 |  |  |  |  |  |  | 1; | 
| 699 |  |  |  |  |  |  |  | 
| 700 |  |  |  |  |  |  | =pod | 
| 701 |  |  |  |  |  |  |  | 
| 702 |  |  |  |  |  |  | =encoding UTF-8 | 
| 703 |  |  |  |  |  |  |  | 
| 704 |  |  |  |  |  |  | =head1 NAME | 
| 705 |  |  |  |  |  |  |  | 
| 706 |  |  |  |  |  |  | WWW::YouTube::Download - WWW::YouTube::Download - Very simple YouTube video download interface | 
| 707 |  |  |  |  |  |  |  | 
| 708 |  |  |  |  |  |  | =head1 VERSION | 
| 709 |  |  |  |  |  |  |  | 
| 710 |  |  |  |  |  |  | version 0.64 | 
| 711 |  |  |  |  |  |  |  | 
| 712 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 713 |  |  |  |  |  |  |  | 
| 714 |  |  |  |  |  |  | use WWW::YouTube::Download; | 
| 715 |  |  |  |  |  |  |  | 
| 716 |  |  |  |  |  |  | my $client = WWW::YouTube::Download->new; | 
| 717 |  |  |  |  |  |  | $client->download($video_id); | 
| 718 |  |  |  |  |  |  |  | 
| 719 |  |  |  |  |  |  | my $video_url = $client->get_video_url($video_id); | 
| 720 |  |  |  |  |  |  | my $title     = $client->get_title($video_id);     # maybe encoded utf8 string. | 
| 721 |  |  |  |  |  |  | my $fmt       = $client->get_fmt($video_id);       # maybe highest quality. | 
| 722 |  |  |  |  |  |  | my $suffix    = $client->get_suffix($video_id);    # maybe highest quality file suffix | 
| 723 |  |  |  |  |  |  |  | 
| 724 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 725 |  |  |  |  |  |  |  | 
| 726 |  |  |  |  |  |  | WWW::YouTube::Download is a library to download videos from YouTube. It relies entirely on | 
| 727 |  |  |  |  |  |  | scraping a video's webpage and does not use YT's /get_video_info URL space. | 
| 728 |  |  |  |  |  |  |  | 
| 729 |  |  |  |  |  |  | =head1 METHODS | 
| 730 |  |  |  |  |  |  |  | 
| 731 |  |  |  |  |  |  | =over | 
| 732 |  |  |  |  |  |  |  | 
| 733 |  |  |  |  |  |  | =item B | 
| 734 |  |  |  |  |  |  |  | 
| 735 |  |  |  |  |  |  | $client = WWW::YouTube::Download->new; | 
| 736 |  |  |  |  |  |  |  | 
| 737 |  |  |  |  |  |  | Creates a WWW::YouTube::Download instance. | 
| 738 |  |  |  |  |  |  |  | 
| 739 |  |  |  |  |  |  | =item B | 
| 740 |  |  |  |  |  |  |  | 
| 741 |  |  |  |  |  |  | $client->download($video_id); | 
| 742 |  |  |  |  |  |  | $client->download($video_id, { | 
| 743 |  |  |  |  |  |  | fmt      => 37, | 
| 744 |  |  |  |  |  |  | filename => 'sample.mp4', # save file name | 
| 745 |  |  |  |  |  |  | }); | 
| 746 |  |  |  |  |  |  | $client->download($video_id, { | 
| 747 |  |  |  |  |  |  | filename => '{title}.{suffix}', # maybe `video_title.mp4` | 
| 748 |  |  |  |  |  |  | }); | 
| 749 |  |  |  |  |  |  | $client->download($video_id, { | 
| 750 |  |  |  |  |  |  | cb => \&callback, | 
| 751 |  |  |  |  |  |  | }); | 
| 752 |  |  |  |  |  |  |  | 
| 753 |  |  |  |  |  |  | Download the video file. | 
| 754 |  |  |  |  |  |  | The first parameter is passed to YouTube video url. | 
| 755 |  |  |  |  |  |  |  | 
| 756 |  |  |  |  |  |  | Allowed arguments: | 
| 757 |  |  |  |  |  |  |  | 
| 758 |  |  |  |  |  |  | =over | 
| 759 |  |  |  |  |  |  |  | 
| 760 |  |  |  |  |  |  | =item C | 
| 761 |  |  |  |  |  |  |  | 
| 762 |  |  |  |  |  |  | Set a callback subroutine, SEE L ':content_cb' | 
| 763 |  |  |  |  |  |  | for details. | 
| 764 |  |  |  |  |  |  |  | 
| 765 |  |  |  |  |  |  | =item C | 
| 766 |  |  |  |  |  |  |  | 
| 767 |  |  |  |  |  |  | Set the filename, possibly using placeholders to be filled with | 
| 768 |  |  |  |  |  |  | information gathered about the video. | 
| 769 |  |  |  |  |  |  |  | 
| 770 |  |  |  |  |  |  | C<< filename >> supported format placeholders: | 
| 771 |  |  |  |  |  |  |  | 
| 772 |  |  |  |  |  |  | {video_id} | 
| 773 |  |  |  |  |  |  | {title} | 
| 774 |  |  |  |  |  |  | {user} | 
| 775 |  |  |  |  |  |  | {fmt} | 
| 776 |  |  |  |  |  |  | {suffix} | 
| 777 |  |  |  |  |  |  | {resolution} | 
| 778 |  |  |  |  |  |  |  | 
| 779 |  |  |  |  |  |  | Output filename is set to C<{video_id}.{suffix}> by default. | 
| 780 |  |  |  |  |  |  |  | 
| 781 |  |  |  |  |  |  | =item C | 
| 782 |  |  |  |  |  |  |  | 
| 783 |  |  |  |  |  |  | B<< DEPRECATED >> alternative for C. | 
| 784 |  |  |  |  |  |  |  | 
| 785 |  |  |  |  |  |  | =item C | 
| 786 |  |  |  |  |  |  |  | 
| 787 |  |  |  |  |  |  | set the format to download. Defaults to the best video quality | 
| 788 |  |  |  |  |  |  | (inferred by the available resolutions). | 
| 789 |  |  |  |  |  |  |  | 
| 790 |  |  |  |  |  |  | =back | 
| 791 |  |  |  |  |  |  |  | 
| 792 |  |  |  |  |  |  | =item B | 
| 793 |  |  |  |  |  |  |  | 
| 794 |  |  |  |  |  |  | $client->playback_url($video_id); | 
| 795 |  |  |  |  |  |  | $client->playback_url($video_id, { fmt => 37 }); | 
| 796 |  |  |  |  |  |  |  | 
| 797 |  |  |  |  |  |  | Return playback URL of the video. This is direct link to the movie file. | 
| 798 |  |  |  |  |  |  | Function supports only "fmt" option. | 
| 799 |  |  |  |  |  |  |  | 
| 800 |  |  |  |  |  |  | =item B | 
| 801 |  |  |  |  |  |  |  | 
| 802 |  |  |  |  |  |  | Gather data about the video. A hash reference is returned, with the following | 
| 803 |  |  |  |  |  |  | keys: | 
| 804 |  |  |  |  |  |  |  | 
| 805 |  |  |  |  |  |  | =over | 
| 806 |  |  |  |  |  |  |  | 
| 807 |  |  |  |  |  |  | =item C | 
| 808 |  |  |  |  |  |  |  | 
| 809 |  |  |  |  |  |  | the default, suggested format. It is inferred by selecting the | 
| 810 |  |  |  |  |  |  | alternative with the highest resolution. | 
| 811 |  |  |  |  |  |  |  | 
| 812 |  |  |  |  |  |  | =item C | 
| 813 |  |  |  |  |  |  |  | 
| 814 |  |  |  |  |  |  | the list of available formats, as an array reference. | 
| 815 |  |  |  |  |  |  |  | 
| 816 |  |  |  |  |  |  | =item C | 
| 817 |  |  |  |  |  |  |  | 
| 818 |  |  |  |  |  |  | the filename extension associated to the default format (see C | 
| 819 |  |  |  |  |  |  | above). | 
| 820 |  |  |  |  |  |  |  | 
| 821 |  |  |  |  |  |  | =item C | 
| 822 |  |  |  |  |  |  |  | 
| 823 |  |  |  |  |  |  | the title of the video | 
| 824 |  |  |  |  |  |  |  | 
| 825 |  |  |  |  |  |  | =item C | 
| 826 |  |  |  |  |  |  |  | 
| 827 |  |  |  |  |  |  | the YouTube user owning the video | 
| 828 |  |  |  |  |  |  |  | 
| 829 |  |  |  |  |  |  | =item C | 
| 830 |  |  |  |  |  |  |  | 
| 831 |  |  |  |  |  |  | the video identifier | 
| 832 |  |  |  |  |  |  |  | 
| 833 |  |  |  |  |  |  | =item C | 
| 834 |  |  |  |  |  |  |  | 
| 835 |  |  |  |  |  |  | the URL of the video associated to the default format (see C | 
| 836 |  |  |  |  |  |  | above). | 
| 837 |  |  |  |  |  |  |  | 
| 838 |  |  |  |  |  |  | =item C | 
| 839 |  |  |  |  |  |  |  | 
| 840 |  |  |  |  |  |  | an hash reference containing details about all available formats. | 
| 841 |  |  |  |  |  |  |  | 
| 842 |  |  |  |  |  |  | =back | 
| 843 |  |  |  |  |  |  |  | 
| 844 |  |  |  |  |  |  | The C has one key/value pair for each available format, | 
| 845 |  |  |  |  |  |  | where the key is the format identifier (can be used as C parameter | 
| 846 |  |  |  |  |  |  | for L, for example) and the value is a hash reference with | 
| 847 |  |  |  |  |  |  | the following data: | 
| 848 |  |  |  |  |  |  |  | 
| 849 |  |  |  |  |  |  | =over | 
| 850 |  |  |  |  |  |  |  | 
| 851 |  |  |  |  |  |  | =item C | 
| 852 |  |  |  |  |  |  |  | 
| 853 |  |  |  |  |  |  | the format specifier, that can be passed to L | 
| 854 |  |  |  |  |  |  |  | 
| 855 |  |  |  |  |  |  | =item C | 
| 856 |  |  |  |  |  |  |  | 
| 857 |  |  |  |  |  |  | the resolution as IxI | 
| 858 |  |  |  |  |  |  |  | 
| 859 |  |  |  |  |  |  | =item C | 
| 860 |  |  |  |  |  |  |  | 
| 861 |  |  |  |  |  |  | the suffix, providing a hint about the video format (e.g. webm, flv, ...) | 
| 862 |  |  |  |  |  |  |  | 
| 863 |  |  |  |  |  |  | =item C | 
| 864 |  |  |  |  |  |  |  | 
| 865 |  |  |  |  |  |  | the URL where the video can be found | 
| 866 |  |  |  |  |  |  |  | 
| 867 |  |  |  |  |  |  | =back | 
| 868 |  |  |  |  |  |  |  | 
| 869 |  |  |  |  |  |  | =item B | 
| 870 |  |  |  |  |  |  |  | 
| 871 |  |  |  |  |  |  | $self->ua->agent(); | 
| 872 |  |  |  |  |  |  | $self->ua($LWP_LIKE_OBJECT); | 
| 873 |  |  |  |  |  |  |  | 
| 874 |  |  |  |  |  |  | Sets and gets LWP::UserAgent object. | 
| 875 |  |  |  |  |  |  |  | 
| 876 |  |  |  |  |  |  | =item B | 
| 877 |  |  |  |  |  |  |  | 
| 878 |  |  |  |  |  |  | Parses given URL and returns video ID. | 
| 879 |  |  |  |  |  |  |  | 
| 880 |  |  |  |  |  |  | =item B | 
| 881 |  |  |  |  |  |  |  | 
| 882 |  |  |  |  |  |  | Parses given URL and returns playlist ID. | 
| 883 |  |  |  |  |  |  |  | 
| 884 |  |  |  |  |  |  | =item B | 
| 885 |  |  |  |  |  |  |  | 
| 886 |  |  |  |  |  |  | Parses given URL and returns YouTube username. | 
| 887 |  |  |  |  |  |  |  | 
| 888 |  |  |  |  |  |  | =item B | 
| 889 |  |  |  |  |  |  |  | 
| 890 |  |  |  |  |  |  | =item B | 
| 891 |  |  |  |  |  |  |  | 
| 892 |  |  |  |  |  |  | =item B | 
| 893 |  |  |  |  |  |  |  | 
| 894 |  |  |  |  |  |  | =item B | 
| 895 |  |  |  |  |  |  |  | 
| 896 |  |  |  |  |  |  | =item B | 
| 897 |  |  |  |  |  |  |  | 
| 898 |  |  |  |  |  |  | =item B | 
| 899 |  |  |  |  |  |  |  | 
| 900 |  |  |  |  |  |  | =item B | 
| 901 |  |  |  |  |  |  |  | 
| 902 |  |  |  |  |  |  | =item B | 
| 903 |  |  |  |  |  |  |  | 
| 904 |  |  |  |  |  |  | Fetches a playlist and returns a ref to an array of hashes, where each hash | 
| 905 |  |  |  |  |  |  | represents a video from the playlist with youtube id, runtime in seconds | 
| 906 |  |  |  |  |  |  | and title. On playlists with many videos, the method iteratively downloads | 
| 907 |  |  |  |  |  |  | pages until the playlist is complete. | 
| 908 |  |  |  |  |  |  |  | 
| 909 |  |  |  |  |  |  | Optionally accepts a second argument, a hashref of options. Currently, you | 
| 910 |  |  |  |  |  |  | can pass a "limit" value to stop downloading of subsequent pages on larger | 
| 911 |  |  |  |  |  |  | playlists after x-amount of fetches (a limit of fetches, not playlist items). | 
| 912 |  |  |  |  |  |  | For example, pass 1 to only download the first page of videos from a playlist | 
| 913 |  |  |  |  |  |  | in order to "skim" the "tip" of new videos in a playlist. YouTube currently | 
| 914 |  |  |  |  |  |  | returns 100 videos at max per page. | 
| 915 |  |  |  |  |  |  |  | 
| 916 |  |  |  |  |  |  | This method is used by the I script. | 
| 917 |  |  |  |  |  |  |  | 
| 918 |  |  |  |  |  |  | =back | 
| 919 |  |  |  |  |  |  |  | 
| 920 |  |  |  |  |  |  | =head1 CONTRIBUTORS | 
| 921 |  |  |  |  |  |  |  | 
| 922 |  |  |  |  |  |  | yusukebe | 
| 923 |  |  |  |  |  |  |  | 
| 924 |  |  |  |  |  |  | =head1 BUG REPORTING | 
| 925 |  |  |  |  |  |  |  | 
| 926 |  |  |  |  |  |  | Please use github issues: L<< https://github.com/xaicron/p5-www-youtube-download/issues >>. | 
| 927 |  |  |  |  |  |  |  | 
| 928 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 929 |  |  |  |  |  |  |  | 
| 930 |  |  |  |  |  |  | L and L. | 
| 931 |  |  |  |  |  |  | L | 
| 932 |  |  |  |  |  |  | L | 
| 933 |  |  |  |  |  |  |  | 
| 934 |  |  |  |  |  |  | =head1 AUTHOR | 
| 935 |  |  |  |  |  |  |  | 
| 936 |  |  |  |  |  |  | xaicron | 
| 937 |  |  |  |  |  |  |  | 
| 938 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 939 |  |  |  |  |  |  |  | 
| 940 |  |  |  |  |  |  | This software is copyright (c) 2013 by Yuji Shimada. | 
| 941 |  |  |  |  |  |  |  | 
| 942 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 943 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 944 |  |  |  |  |  |  |  | 
| 945 |  |  |  |  |  |  | =cut | 
| 946 |  |  |  |  |  |  |  | 
| 947 |  |  |  |  |  |  | __END__ |