blib/lib/WWW/YouTube/Download.pm

filter: Statementstmt 47.2% 118 / 250 Branchbran 20.7% 29 / 140 Conditioncond 2.3% 1 / 43 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 57.5% 23 / 40 Podpod 100% 8 / 8 totaltotal 35.3% 179 / 506 CC 90 SCAR 40.7
CC
90
Cov
34%
_sigdecode
272.0
download
156.0
playback_url
42.0
CRAP 58.7
↑ Summary
1 package WWW::YouTube::Download;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '0.63';
8  
9 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use JSON::MaybeXS 'JSON';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use HTML::Entities qw/decode_entities/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use MIME::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 $Carp::Internal{ (__PACKAGE__) }++;
18  
19 7 ▶use constant DEFAULT_FMT => 18;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 7 my $base_url = 'http://www.youtube.com/watch?v=';
22  
23 sub new {
24 32 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
25 32 my %args = @_;
26 $args{ua} = LWP::UserAgent->new(
27 agent => __PACKAGE__.'/'.$VERSION,
28 parse_head => 0,
29 32 ▶ ) unless exists $args{ua};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"ua"} 32 0 32
30 32 bless \%args, $class;
31 }
32  
33 7 for my $name (qw[video_id video_url title user fmt fmt_list suffix]) {
34 ## no critic (TestingAndDebugging::ProhibitNoStrict)
35 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 49 *{"get_$name"} = sub {
37 7 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 0 ▶ my ($self, $video_id) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
39 0 ▶ croak "Usage: $self->get_$name(\$video_id|\$watch_url)" unless $video_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $video_id 0 0 0
40 0 my $data = $self->prepare_download($video_id);
41 0 return $data->{$name};
42 49 };
43 }
44  
45 sub playback_url {
46 0 ▶ my ($self, $video_id, $args) = @_;
Subroutine playback_url: not called CC=6 SCAR=37.4
Pod documented
47 0 ▶ croak "Usage: $self->playback_url('[video_id|video_url]')" unless $video_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $video_id 0 0 0
48 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
49  
50 0 my $data = $self->prepare_download($video_id);
51 0 ▶ my $fmt = $args->{fmt} || $data->{fmt} || DEFAULT_FMT;
Condition0%0/5
$args->{'fmt'} || $data->{'fmt'} || 18
l!l
00
$args->{"fmt"} or $data->{"fmt"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'fmt'} || $data->{'fmt'} || 18
$args->{"fmt"} $data->{"fmt"}
Truth table0%0/3
A or B
A= $args->{"fmt"}B= $data->{"fmt"}
ABresult
1X1
011
000
52 0 ▶ my $video_url = $data->{video_url_map}{$fmt}{url} || croak "this video does not offer format (fmt) $fmt";
Condition0%0/3
$data->{'video_url_map'}{$fmt}{'url'} || croak("this video does not offer format (fmt) $fmt")
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{'video_url_map'}{$fmt}{'url'} || croak("this video does not offer format (fmt) $fmt")
$data->{'video_url_map'}{$fmt}{'url'} croak("this video does not offer format (fmt) $fmt")
Truth table0%0/3
A || B
A= $data->{'video_url_map'}{$fmt}{'url'}B= croak("this video does not offer format (fmt) $fmt")
ABresult
1X1
011
000
53  
54 0 return $video_url;
55 }
56  
57 sub download {
58 0 ▶ my ($self, $video_id, $args) = @_;
Subroutine download: not called CC=12 SCAR=50.5
Pod documented
59 0 ▶ croak "Usage: $self->download('[video_id|video_url]')" unless $video_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $video_id 0 0 0
60 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
61  
62 0 my $data = $self->prepare_download($video_id);
63  
64 0 ▶ my $fmt = $args->{fmt} || $data->{fmt} || DEFAULT_FMT;
Condition0%0/5
$args->{'fmt'} || $data->{'fmt'} || 18
l!l
00
$args->{"fmt"} or $data->{"fmt"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'fmt'} || $data->{'fmt'} || 18
$args->{"fmt"} $data->{"fmt"}
Truth table0%0/3
A or B
A= $args->{"fmt"}B= $data->{"fmt"}
ABresult
1X1
011
000
65  
66 0 ▶ my $video_url = $data->{video_url_map}{$fmt}{url} || croak "this video has not supported fmt: $fmt";
Condition0%0/3
$data->{'video_url_map'}{$fmt}{'url'} || croak("this video has not supported fmt: $fmt")
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{'video_url_map'}{$fmt}{'url'} || croak("this video has not supported fmt: $fmt")
$data->{'video_url_map'}{$fmt}{'url'} croak("this video has not supported fmt: $fmt")
Truth table0%0/3
A || B
A= $data->{'video_url_map'}{$fmt}{'url'}B= croak("this video has not supported fmt: $fmt")
ABresult
1X1
011
000
67 0 ▶ $args->{filename} ||= $args->{file_name};
Condition0%0/3
$args->{"filename"} ||= $args->{"file_name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"filename"} ||= $args->{"file_name"}
$args->{"filename"} $args->{"file_name"}
Truth table0%0/3
A ||= B
A= $args->{"filename"}B= $args->{"file_name"}
ABresult
1X1
011
000
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 ▶ resolution => $data->{video_url_map}{$fmt}{resolution} || '0x0',
Condition0%0/5
$data->{'video_url_map'}{$fmt}{'suffix'} || _suffix($fmt)
l!l&&r!l&&!r
000
$data->{'video_url_map'}{$fmt}{'resolution'} || '0x0'
l!l
00
MC/DC0%0/2
$data->{'video_url_map'}{$fmt}{'suffix'} || _suffix($fmt)
$data->{'video_url_map'}{$fmt}{'suffix'} _suffix($fmt)
MC/DC0%0/1
$data->{'video_url_map'}{$fmt}{'resolution'} || '0x0'
$data->{'video_url_map'}{$fmt}{'resolution'}
Truth table0%0/3
A || B
A= $data->{'video_url_map'}{$fmt}{'suffix'}B= _suffix($fmt)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $data->{'video_url_map'}{$fmt}{'resolution'}
Aresult
11
00
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 ▶ }) unless ref $args->{cb} eq 'CODE';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $args->{"cb"} eq "CODE" 0 0 0
defined $args->{'overwrite'} ? : 0 0 0
83  
84 0 my $res = $self->ua->get($video_url, ':content_cb' => $args->{cb});
85 0 ▶ croak "!! $video_id download failed: ", $res->status_line if $res->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_error 0 0 0
86 }
87  
88 sub _format_filename {
89 0 ▶ my ($self, $filename, $data) = @_;
Subroutine _format_filename: not called CC=3 SCAR=24.8
90 0 ▶ return "$data->{video_id}.$data->{suffix}" unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 0 0
91 0 ▶ $filename =~ s#{([^}]+)}#$data->{$1} || "{$1}"#eg;
Branch: 1 branch
BranchTrueFalseTotal
$data->{$1} or "{$1}" 0 0 0
92 0 return $filename;
93 }
94  
95 sub _is_supported_fmt {
96 0 ▶ my ($self, $video_id, $fmt) = @_;
Subroutine _is_supported_fmt: not called CC=2 SCAR=17.9
97 0 my $data = $self->prepare_download($video_id);
98 0 ▶ defined($data->{video_url_map}{$fmt}{url}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'video_url_map'}{$fmt}{'url'} ? : 0 0 0
99 }
100  
101 sub _progress {
102 0 ▶ my ($self, $args, $total) = @_;
Subroutine _progress: not called CC=4 SCAR=30.0
103  
104 0 ▶ if (not defined $args->{_progress}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"_progress"}) 0 0 0
105 0 ▶ eval "require Term::ProgressBar" or return; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
eval "require Term::ProgressBar" or return 0 0 0
106 0 $args->{_progress} = Term::ProgressBar->new( { count => $total, ETA => 'linear', remove => 0, fh => \*STDOUT } );
107 0 ▶ $args->{_progress}->minor( $total > 50_000_000 ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$total > 50000000 ? : 0 0 0
108 0 $args->{_progress}->max_update_rate(1);
109  
110 0 $args->{_progress}->message("Total $total");
111 }
112  
113 0 return $args->{_progress};
114 }
115  
116 sub _default_cb {
117 0 ▶ my ($self, $args) = @_;
Subroutine _default_cb: not called CC=5 SCAR=34.0
118 0 my ($file, $verbose, $overwrite, $progress) = @$args{qw/filename verbose overwrite progress/};
119  
120 0 ▶ croak "file exists! $file" if -f $file and !$overwrite;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file and not $overwrite 0 0 0
Condition0%0/3
-f $file and not $overwrite
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file and not $overwrite
-f $file not $overwrite
Truth table0%0/3
A and not(B)
A= -f $fileB= not $overwrite
ABresult
0X0
100
111
121 0 ▶ open my $wfh, '>', $file or croak $file, " $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $wfh, ">", $file or croak($file, " $!") 0 0 0
122 0 binmode $wfh;
123  
124 0 ▶ print "Downloading `$file`\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
125 return sub {
126 0 ▶ my ($chunk, $res, $proto) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
127 0 print $wfh $chunk; # write file
128  
129 0 ▶ if ($verbose || $self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose or $self->{"verbose"}) 0 0 0
Condition0%0/3
$verbose or $self->{"verbose"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$verbose or $self->{"verbose"}
$verbose $self->{"verbose"}
Truth table0%0/3
A or B
A= $verboseB= $self->{"verbose"}
ABresult
1X1
011
000
130 0 my $size = tell $wfh;
131 0 my $total = $res->header('Content-Length');
132  
133 0 ▶ if ($progress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($progress) 0 0 0
134 0 ▶ if (my $p = $self->_progress($args, $total)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->_progress($args, $total)) { } 0 0 0
135 0 $p->update($size);
136 0 return;
137 }
138 else{
139 0 print "(You need Term::ProgressBar module to show progress bar with -P switch)\n";
140 0 $progress = 0;
141 }
142 }
143  
144 0 printf "%d/%d (%.2f%%)\r", $size, $total, $size / $total * 100;
145 0 ▶ print "\n" if $total == $size;
Branch: 1 branch
BranchTrueFalseTotal
if $total == $size 0 0 0
146 }
147 0 };
148 }
149  
150 sub prepare_download {
151 1 ▶ my ($self, $video_id) = @_;
Subroutine prepare_download: called CC=4 SCAR=14.1
Pod documented
152 1 ▶ croak "Usage: $self->prepare_download('[video_id|watch_url]')" unless $video_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $video_id 0 1 1
153 1 $video_id = $self->video_id($video_id);
154  
155 1 ▶ return $self->{cache}{$video_id} if ref $self->{cache}{$video_id} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"cache"}{$video_id} eq "HASH" 0 1 1
156  
157 1 my ($title, $user, $video_url_map);
158 1 my $content = $self->_get_content($video_id);
159 1 ▶ if ($self->_is_new($content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_new($content)) { } 1 0 1
160 1 my $args = $self->_get_args($content);
161 1 my $player_resp = JSON()->new->decode($args->{player_response});
162 1 $video_url_map = $self->_decode_player_response($player_resp);
163 1 $title = decode_entities $player_resp->{videoDetails}{title};
164 1 $user = decode_entities $player_resp->{videoDetails}{author};
165 } else {
166 0 $title = $self->_fetch_title($content);
167 0 $user = $self->_fetch_user($content);
168 0 $video_url_map = $self->_fetch_video_url_map($content);
169 }
170  
171 1 my $fmt_list = [];
172 my $sorted = [
173 map {
174 2 push @$fmt_list, $_->[0]->{fmt};
175 2 $_->[0]
176 } sort {
177 1 $b->[1] <=> $a->[1]
178 } map {
179 1 my $resolution = $_->{resolution};
180 2 $resolution =~ s/(\d+)x(\d+)/$1 * $2/e;
181 2 [ $_, $resolution ]
182 } values %$video_url_map,
183 ];
184  
185 1 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 };
198 }
199  
200 sub _mime_to_suffix {
201 2 ▶ (my $mime = shift) =~ s{^([^;]+);.*}{$1};
Subroutine _mime_to_suffix: called CC=4 SCAR=16.9
202 2 my $stype = MIME::Type->new(type => $mime)->subType;
203 2 ▶ return $stype eq 'webm' ? 'webm'
Branch: 3 branches
BranchTrueFalseTotal
$stype eq 'webm' ? : 0 2 2
$stype eq 'mp4' ? : 2 0 2
$stype eq '3gp' ? : 0 0 0
204 : $stype eq 'mp4' ? 'mp4'
205 : $stype eq '3gp' ? '3gp'
206 : 'flv'
207 ;
208 }
209  
210 sub _decode_player_response {
211 1 ▶ my ($self, $player_data) = @_;
Subroutine _decode_player_response: called CC=1 SCAR=0.0
212 # need to decode $player_data->{streamingData}{formats};
213 1 my $fmt_map = { map { $_->{mimeType} => join 'x', $_->{width}, $_->{height} } @{$player_data->{streamingData}{formats}} };
214 1 my $fmt_url_map = { map { $_->{mimeType} => $_->{url} } @{$player_data->{streamingData}{formats}} };
215 # more formats in $player_data->{streamingData}{adaptiveFormats};
216 return +{
217 map {
218 2 $_->{fmt} => $_,
219 } map +{
220 fmt => $_,
221 resolution => $fmt_map->{$_},
222 1 url => $fmt_url_map->{$_},
223 suffix => _mime_to_suffix($_),
224 }, keys %$fmt_map
225 };
226 }
227  
228 sub _fetch_title {
229 0 ▶ my ($self, $content) = @_;
Subroutine _fetch_title: not called CC=2 SCAR=17.9
230  
231 0 ▶ my ($title) = $content =~ /<meta name="title" content="(.+?)">/ or return;
Branch: 1 branch
BranchTrueFalseTotal
my($title) = $content =~ /<meta name="title" content="(.+?)">/ or return 0 0 0
232 0 return decode_entities($title);
233 }
234  
235 sub _fetch_user {
236 1 ▶ my ($self, $content) = @_;
Subroutine _fetch_user: called CC=3 SCAR=15.1
237  
238 1 ▶ if( $content =~ /<span class="yt-user-name [^>]+>([^<]+)<\/span>/ ){
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ m[<span class="yt-user-name [^>]+>([^<]+)</span>]) { } 1 0 1
elsif ($content =~ /","author":"([^"]+)","/) { } 0 0 0
239 1 return decode_entities($1);
240 }elsif( $content =~ /","author":"([^"]+)","/ ){
241 0 return decode_entities($1);
242 }else{
243 0 return;
244 }
245 }
246  
247 sub _fetch_video_url_map {
248 0 ▶ my ($self, $content) = @_;
Subroutine _fetch_video_url_map: not called CC=3 SCAR=24.8
249  
250 0 my $args = $self->_get_args($content);
251 0 ▶ unless ($args->{fmt_list} and $args->{url_encoded_fmt_stream_map}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"fmt_list"} and $args->{"url_encoded_fmt_stream_map"}) 0 0 0
Condition0%0/3
$args->{"fmt_list"} and $args->{"url_encoded_fmt_stream_map"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"fmt_list"} and $args->{"url_encoded_fmt_stream_map"}
$args->{"fmt_list"} $args->{"url_encoded_fmt_stream_map"}
Truth table0%0/3
A and B
A= $args->{"fmt_list"}B= $args->{"url_encoded_fmt_stream_map"}
ABresult
0X0
100
111
252 0 croak 'failed to find video urls';
253 }
254  
255 0 my $fmt_map = _parse_fmt_map($args->{fmt_list});
256 0 my $fmt_url_map = _parse_stream_map($args->{url_encoded_fmt_stream_map});
257  
258 my $video_url_map = +{
259 map {
260 0 $_->{fmt} => $_,
261 } map +{
262 fmt => $_,
263 resolution => $fmt_map->{$_},
264 0 url => $fmt_url_map->{$_},
265 suffix => _suffix($_),
266 }, keys %$fmt_map
267 };
268  
269 0 return $video_url_map;
270 }
271  
272 sub _get_content {
273 0 ▶ my ($self, $video_id) = @_;
Subroutine _get_content: not called CC=2 SCAR=17.9
274  
275 0 my $url = "$base_url$video_id";
276  
277 0 my $req = HTTP::Request->new;
278 0 $req->method('GET');
279 0 $req->uri($url);
280 0 $req->header('Accept-Language' => 'en-US');
281  
282 0 my $res = $self->ua->request($req);
283 0 ▶ croak "GET $url failed. status: ", $res->status_line if $res->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_error 0 0 0
284  
285 0 return $res->content;
286 }
287  
288 sub _get_args {
289 2 ▶ my ($self, $content) = @_;
Subroutine _get_args: called CC=6 SCAR=19.1
290  
291 2 my $data;
292 2 for my $line (split "\n", $content) {
293 2 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 2 2
294 2 ▶ if ($line =~ /the uploader has not made this video available in your country/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /the uploader has not made this video available in your country/i) { } 0 2 2
elsif ($line =~ /^.+ytplayer\.config\s*=\s*(\{.*})/) { } 2 0 2
295 0 croak 'Video not available in your country';
296 }
297 elsif ($line =~ /^.+ytplayer\.config\s*=\s*(\{.*})/) {
298 2 ($data, undef) = JSON->new->utf8(1)->decode_prefix($1);
299 2 last;
300 }
301 }
302  
303 2 ▶ croak 'failed to extract JSON data' unless $data->{args};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"args"} 0 2 2
304  
305 2 return $data->{args};
306 }
307  
308 sub _is_new {
309 1 ▶ my ($self, $content) = @_;
Subroutine _is_new: called CC=3 SCAR=13.3
310 1 my $args = $self->_get_args($content);
311 1 ▶ return 1 unless ($args->{fmt_list} and $args->{url_encoded_fmt_stream_map});
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"fmt_list"} and $args->{"url_encoded_fmt_stream_map"} 1 0 1
Condition33%1/3
$args->{"fmt_list"} and $args->{"url_encoded_fmt_stream_map"}
!ll&&!rl&&r
100
MC/DC0%0/2
$args->{"fmt_list"} and $args->{"url_encoded_fmt_stream_map"}
$args->{"fmt_list"} $args->{"url_encoded_fmt_stream_map"}
Truth table33%1/3
A and B
A= $args->{"fmt_list"}B= $args->{"url_encoded_fmt_stream_map"}
ABresult
0X0
100
111
312 0 return 0;
313 }
314  
315 sub _parse_fmt_map {
316 0 ▶ my $param = shift;
Subroutine _parse_fmt_map: not called CC=2 SCAR=17.9
317 0 my $fmt_map = {};
318 0 for my $stuff (split ',', $param) {
319 0 my ($fmt, $resolution) = split '/', $stuff;
320 0 $fmt_map->{$fmt} = $resolution;
321 }
322  
323 0 return $fmt_map;
324 }
325  
326 sub _sigdecode {
327 0 ▶ my @s = @_;
Subroutine _sigdecode: not called CC=16 SCAR=56.1
328  
329 # based on youtube_dl/extractor/youtube.py from yt-dl.org
330 0 ▶ if (@s == 93) {
Branch: 15 branches
BranchTrueFalseTotal
if (@s == 93) { } 0 0 0
elsif (@s == 92) { } 0 0 0
elsif (@s == 91) { } 0 0 0
elsif (@s == 90) { } 0 0 0
elsif (@s == 89) { } 0 0 0
elsif (@s == 88) { } 0 0 0
elsif (@s == 87) { } 0 0 0
elsif (@s == 86) { } 0 0 0
elsif (@s == 85) { } 0 0 0
elsif (@s == 84) { } 0 0 0
elsif (@s == 83) { } 0 0 0
elsif (@s == 82) { } 0 0 0
elsif (@s == 81) { } 0 0 0
elsif (@s == 80) { } 0 0 0
elsif (@s == 79) { } 0 0 0
331 0 return (reverse(@s[30..86]), $s[88], reverse(@s[6..28]));
332 } elsif (@s == 92) {
333 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 return (reverse(@s[28..84]), $s[86], reverse(@s[6..26]));
336 } elsif (@s == 90) {
337 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 return (reverse(@s[79..84]), $s[87], reverse(@s[61..77]), $s[0], reverse(@s[4..59]));
340 } elsif (@s == 88) {
341 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 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 return (@s[4..30], $s[3], @s[32..84]);
346 } elsif (@s == 85) {
347 0 return (@s[3..10], $s[0], @s[12..54], $s[84], @s[56..83]);
348 } elsif (@s == 84) {
349 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 return (reverse(@s[64..80]), $s[0], reverse(@s[1..62]), $s[63]);
352 } elsif (@s == 82) {
353 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 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 return (@s[1..18], $s[0], @s[20..67], $s[19], @s[69..79]);
358 } elsif (@s == 79) {
359 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 return (); # fail
363 }
364  
365 sub _getsig {
366 0 ▶ my $sig = shift;
Subroutine _getsig: not called CC=3 SCAR=24.8
367 0 ▶ croak 'Unable to find signature' unless $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig 0 0 0
368 0 my @sig = _sigdecode(split(//, $sig));
369 0 ▶ croak "Unable to decode signature $sig of length " . length($sig) unless @sig;
Branch: 1 branch
BranchTrueFalseTotal
unless @sig 0 0 0
370 0 return join('', @sig);
371 }
372  
373 sub _parse_stream_map {
374 0 ▶ my $param = shift;
Subroutine _parse_stream_map: not called CC=5 SCAR=34.0
375 0 my $fmt_url_map = {};
376 0 for my $stuff (split ',', $param) {
377 0 my $uri = URI->new;
378 0 $uri->query($stuff);
379 0 my $query = +{ $uri->query_form };
380 0 ▶ my $sig = $query->{sig} || ($query->{s} ? _getsig($query->{s}) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$query->{'s'} ? : 0 0 0
Condition0%0/3
$query->{'sig'} || ($query->{'s'} ? _getsig($query->{'s'}) : undef)
l!l&&r!l&&!r
000
MC/DC0%0/2
$query->{'sig'} || ($query->{'s'} ? _getsig($query->{'s'}) : undef)
$query->{'sig'} ($query->{'s'} ? _getsig($query->{'s'}) : undef)
Truth table0%0/3
A || B
A= $query->{'sig'}B= ($query->{'s'} ? _getsig($query->{'s'}) : undef)
ABresult
1X1
011
000
381 0 my $url = $query->{url};
382 0 ▶ $fmt_url_map->{$query->{itag}} = $url . ($sig ? '&signature='.$sig : '');
Branch: 1 branch
BranchTrueFalseTotal
$sig ? : 0 0 0
383 }
384  
385 0 return $fmt_url_map;
386 }
387  
388 sub ua {
389 0 ▶ my ($self, $ua) = @_;
Subroutine ua: not called CC=3 SCAR=24.8
Pod documented
390 0 ▶ return $self->{ua} unless $ua;
Branch: 1 branch
BranchTrueFalseTotal
unless $ua 0 0 0
391 0 ▶ croak "Usage: $self->ua(\$LWP_LIKE_OBJECT)" unless eval { $ua->isa('LWP::UserAgent') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $ua->isa("LWP::UserAgent") } } 0 0 0
392 0 $self->{ua} = $ua;
393 }
394  
395 sub _suffix {
396 0 ▶ my $fmt = shift;
Subroutine _suffix: not called CC=4 SCAR=30.0
397 0 ▶ return $fmt =~ /43|44|45|46/ ? 'webm'
Branch: 3 branches
BranchTrueFalseTotal
$fmt =~ /43|44|45|46/ ? : 0 0 0
$fmt =~ /18|22|37|38/ ? : 0 0 0
$fmt =~ /13|17/ ? : 0 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 ▶ my ($self, $stuff) = @_;
Subroutine video_id: called CC=6 SCAR=17.9
Pod documented
406 17 ▶ return unless $stuff;
Branch: 1 branch
BranchTrueFalseTotal
unless $stuff 0 17 17
407 17 ▶ if ($stuff =~ m{/.*?[?&;!](?:v|video_id)=([^&#?=/;]+)}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($stuff =~ m[/.*?[?&;!](?:v|video_id)=([^&#?=/;]+)]) { } 8 9 17
elsif ($stuff =~ m[/(?:e|v|embed)/([^&#?=/;]+)]) { } 4 5 9
elsif ($stuff =~ m[#p/(?:u|search)/\d+/([^&?/]+)]) { } 1 4 5
elsif ($stuff =~ m[youtu.be/([^&#?=/;]+)]) { } 1 3 4
408 8 return $1;
409 }
410 elsif ($stuff =~ m{/(?:e|v|embed)/([^&#?=/;]+)}) {
411 4 return $1;
412 }
413 elsif ($stuff =~ m{#p/(?:u|search)/\d+/([^&?/]+)}) {
414 1 return $1;
415 }
416 elsif ($stuff =~ m{youtu.be/([^&#?=/;]+)}) {
417 1 return $1;
418 }
419 else {
420 3 return $stuff;
421 }
422 }
423  
424 sub playlist_id {
425 10 ▶ my ($self, $stuff) = @_;
Subroutine playlist_id: called CC=4 SCAR=13.9
Pod documented
426 10 ▶ return unless $stuff;
Branch: 1 branch
BranchTrueFalseTotal
unless $stuff 0 10 10
427 10 ▶ if ($stuff =~ m{/.*?[?&;!]list=([^&#?=/;]+)}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stuff =~ m[/.*?[?&;!]list=([^&#?=/;]+)]) { } 4 6 10
elsif ($stuff =~ /^\s*([FP]L[\w\-]+)\s*$/) { } 3 3 6
428 4 return $1;
429 }
430 elsif ($stuff =~ m{^\s*([FP]L[\w\-]+)\s*$}) {
431 3 return $1;
432 }
433 3 return $stuff;
434 }
435  
436 sub user_id {
437 4 ▶ my ($self, $stuff) = @_;
Subroutine user_id: called CC=3 SCAR=11.0
Pod documented
438 4 ▶ return unless $stuff;
Branch: 1 branch
BranchTrueFalseTotal
unless $stuff 0 4 4
439 4 ▶ if ($stuff =~ m{/user/([^&#?=/;]+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stuff =~ m[/user/([^&#?=/;]+)]) 3 1 4
440 3 return $1;
441 }
442 1 return $stuff;
443 }
444  
445 7 1;
446  
447 =pod
448  
449 =encoding UTF-8
450  
451 =head1 NAME
452  
453 WWW::YouTube::Download - WWW::YouTube::Download - Very simple YouTube video download interface
454  
455 =head1 VERSION
456  
457 version 0.63
458  
459 =head1 SYNOPSIS
460  
461 use WWW::YouTube::Download;
462  
463 my $client = WWW::YouTube::Download->new;
464 $client->download($video_id);
465  
466 my $video_url = $client->get_video_url($video_id);
467 my $title = $client->get_title($video_id); # maybe encoded utf8 string.
468 my $fmt = $client->get_fmt($video_id); # maybe highest quality.
469 my $suffix = $client->get_suffix($video_id); # maybe highest quality file suffix
470  
471 =head1 DESCRIPTION
472  
473 WWW::YouTube::Download is a library to download videos from YouTube. It relies entirely on
474 scraping a video's webpage and does not use YT's /get_video_info URL space.
475  
476 =head1 METHODS
477  
478 =over
479  
480 =item B<new()>
481  
482 $client = WWW::YouTube::Download->new;
483  
484 Creates a WWW::YouTube::Download instance.
485  
486 =item B<download($video_id [, \%args])>
487  
488 $client->download($video_id);
489 $client->download($video_id, {
490 fmt => 37,
491 filename => 'sample.mp4', # save file name
492 });
493 $client->download($video_id, {
494 filename => '{title}.{suffix}', # maybe `video_title.mp4`
495 });
496 $client->download($video_id, {
497 cb => \&callback,
498 });
499  
500 Download the video file.
501 The first parameter is passed to YouTube video url.
502  
503 Allowed arguments:
504  
505 =over
506  
507 =item C<cb>
508  
509 Set a callback subroutine, SEE L<LWP::UserAgent> ':content_cb'
510 for details.
511  
512 =item C<filename>
513  
514 Set the filename, possibly using placeholders to be filled with
515 information gathered about the video.
516  
517 C<< filename >> supported format placeholders:
518  
519 {video_id}
520 {title}
521 {user}
522 {fmt}
523 {suffix}
524 {resolution}
525  
526 Output filename is set to C<{video_id}.{suffix}> by default.
527  
528 =item C<file_name>
529  
530 B<< DEPRECATED >> alternative for C<filename>.
531  
532 =item C<fmt>
533  
534 set the format to download. Defaults to the best video quality
535 (inferred by the available resolutions).
536  
537 =back
538  
539 =item B<playback_url($video_id, [, \%args])>
540  
541 $client->playback_url($video_id);
542 $client->playback_url($video_id, { fmt => 37 });
543  
544 Return playback URL of the video. This is direct link to the movie file.
545 Function supports only "fmt" option.
546  
547 =item B<prepare_download($video_id)>
548  
549 Gather data about the video. A hash reference is returned, with the following
550 keys:
551  
552 =over
553  
554 =item C<fmt>
555  
556 the default, suggested format. It is inferred by selecting the
557 alternative with the highest resolution.
558  
559 =item C<fmt_list>
560  
561 the list of available formats, as an array reference.
562  
563 =item C<suffix>
564  
565 the filename extension associated to the default format (see C<fmt>
566 above).
567  
568 =item C<title>
569  
570 the title of the video
571  
572 =item C<user>
573  
574 the YouTube user owning the video
575  
576 =item C<video_id>
577  
578 the video identifier
579  
580 =item C<video_url>
581  
582 the URL of the video associated to the default format (see C<fmt>
583 above).
584  
585 =item C<video_url_map>
586  
587 an hash reference containing details about all available formats.
588  
589 =back
590  
591 The C<video_url_map> has one key/value pair for each available format,
592 where the key is the format identifier (can be used as C<fmt> parameter
593 for L</download>, for example) and the value is a hash reference with
594 the following data:
595  
596 =over
597  
598 =item C<fmt>
599  
600 the format specifier, that can be passed to L</download>
601  
602 =item C<resolution>
603  
604 the resolution as I<width>xI<height>
605  
606 =item C<suffix>
607  
608 the suffix, providing a hint about the video format (e.g. webm, flv, ...)
609  
610 =item C<url>
611  
612 the URL where the video can be found
613  
614 =back
615  
616 =item B<ua([$ua])>
617  
618 $self->ua->agent();
619 $self->ua($LWP_LIKE_OBJECT);
620  
621 Sets and gets LWP::UserAgent object.
622  
623 =item B<video_id($url)>
624  
625 Parses given URL and returns video ID.
626  
627 =item B<playlist_id($url)>
628  
629 Parses given URL and returns playlist ID.
630  
631 =item B<user_id($url)>
632  
633 Parses given URL and returns YouTube username.
634  
635 =item B<get_video_id($video_id)>
636  
637 =item B<get_video_url($video_id)>
638  
639 =item B<get_title($video_id)>
640  
641 =item B<get_user($video_id)>
642  
643 =item B<get_fmt($video_id)>
644  
645 =item B<get_fmt_list($video_id)>
646  
647 =item B<get_suffix($video_id)>
648  
649 =back
650  
651 =head1 CONTRIBUTORS
652  
653 yusukebe
654  
655 =head1 BUG REPORTING
656  
657 Please use github issues: L<< https://github.com/xaicron/p5-www-youtube-download/issues >>.
658  
659 =head1 SEE ALSO
660  
661 L<WWW::YouTube::Info> and L<WWW::YouTube::Info::Simple>.
662 L<WWW::NicoVideo::Download>
663 L<http://rg3.github.io/youtube-dl/>
664  
665 =head1 AUTHOR
666  
667 xaicron <xaicron {@} cpan.org>
668  
669 =head1 COPYRIGHT AND LICENSE
670  
671 This software is copyright (c) 2013 by Yuji Shimada.
672  
673 This is free software; you can redistribute it and/or modify it under
674 the same terms as the Perl 5 programming language system itself.
675  
676 =cut
677  
678 __END__
↑ Summary