blib/lib/WWW/YouTube/Info/Simple.pm

filter: Statementstmt 18.6% 20 / 107 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 35.7% 5 / 14 Podpod 100% 5 / 5 totaltotal 14.5% 30 / 206 SCAR 70.1
CC
39
Cov
11%
get_resolution
72.0
get_url
72.0
_url_encoded_fmt_stream_map
72.0
CRAP 1102.8
↑ Summary
1 package WWW::YouTube::Info::Simple;
2  
3 8 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 require Exporter;
8 8 require WWW::YouTube::Info;
9  
10 8 our @ISA = qw(
11 Exporter
12 WWW::YouTube::Info
13 );
14  
15 8 our @EXPORT = qw(
16 );
17  
18 8 our $VERSION = '0.08';
19  
20 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 NAME
24  
25 WWW::YouTube::Info::Simple - simple interface to WWW::YouTube::Info
26  
27 =head1 SYNOPSIS
28  
29 =head2 Perhaps a little code snippet?
30  
31 #!/usr/bin/perl
32
33 use strict;
34 use warnings;
35
36 use WWW::YouTube::Info::Simple;
37
38 # id taken from YouTube video URL
39 my $id = 'foobar';
40
41 my $yt = WWW::YouTube::Info::Simple->new($id);
42
43 # hash reference holds values gained via http://youtube.com/get_video_info?video_id=foobar
44 my $info = $yt->get_info();
45 # this is basically an inheritance to get_info() at WWW::YouTube::Info
46 # $info->{title} # e.g.: Foo+bar+-+%27Foobar%27
47 # $info->{author} # e.g.: foobar
48 # $info->{keywords} # e.g.: Foo%2Cbar%2CFoobar
49 # $info->{length_seconds} # e.g.: 60
50 # $info->{fmt_map} # e.g.: 22%2F1280x720%2F9%2F0%2F115%2C35%2F854x480%2F9%2F0%2F115%2C34%2F640x360%2F9%2 ..
51 # $info->{fmt_url_map} # e.g.: 22%7Chttp%3A%2F%2Fv14.lscache1.c.youtube.com%2Fvideoplayback%3Fip%3D131.0.0.0 ..
52 # $info->{fmt_stream_map} # e.g.: 22%7Chttp%3A%2F%2Fv14.lscache1.c.youtube.com%2Fvideoplayback%3Fip%3D131.0.0.0 ..
53
54 # array reference holds values keywords
55 my $keys = $yt->get_keywords();
56 # $keys->[0] # e.g.: Foo
57 # $keys->[1] # e.g.: bar
58 # ..
59
60 # hash reference holds values quality -> resolution
61 my $res = $yt->get_resolution();
62 # $res->{35} # e.g.: 854x480
63 # $res->{22} # e.g.: 1280x720
64 # ..
65
66 # URL and masquerading decoded title
67 my $title = $yt->get_title(); # e.g.: Foo bar - 'Foobar'
68
69 # hash reference holds values quality -> url
70 my $url = $yt->get_url();
71 # $url->{35} e.g.: http://v14.lscache1.c.youtube.com/videoplayback?ip=131.0.0.0 ..
72 # $url->{22} e.g.: http://v14.lscache1.c.youtube.com/videoplayback?ip=131.0.0.0 ..
73 # ..
74
75 # URL decoded RTMPE URL
76 my $conn = $yt->get_conn(); # e.g.: rtmpe://cp59009.edgefcs.net/youtube?auth=daEcaboc8dvawbcbxazdobDcZajcDdgcfae ..
77
78 # Remark:
79 # You might want to check $info->{status} before further workout,
80 # as some videos have copyright issues indicated, for instance, by
81 # $info->{status} ne 'ok'.
82  
83 =head1 DESCRIPTION
84  
85 I guess its pretty much self-explanatory ..
86  
87 =head1 METHODS
88  
89 =cut
90  
91 =head2 get_keywords
92  
93 Returns undef if status ne 'ok'.
94 Croaks if not available.
95  
96 =cut
97  
98 sub get_keywords {
99 0 ▶ my ($self) = @_;
Subroutine get_keywords: not called CC=6 SCAR=37.4
Pod documented
100  
101 0 ▶ $self->get_info() unless exists($self->{info});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"info"} 0 0 0
102 0 ▶ return if ( $self->{info}->{status} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"}{"status"} ne "ok" 0 0 0
103  
104 0 my $keywords = $self->{info}->{'keywords'};
105 0 ▶ croak "no keywords found!" unless $keywords;
Branch: 1 branch
BranchTrueFalseTotal
unless $keywords 0 0 0
106  
107 0 my @keywords_parts = split /%2C/, $keywords;
108 0 foreach my $item ( @keywords_parts ) {
109 0 ▶ next unless $item;
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 0 0
110 0 $item = _url_decode($item);
111 0 push @{$self->{keywords}}, $item;
112 }
113  
114 0 return $self->{keywords};
115 }
116  
117 =head2 get_resolution
118  
119 Returns undef if status ne 'ok'.
120 Croaks if not available.
121  
122 =cut
123  
124 sub get_resolution {
125 0 ▶ my ($self) = @_;
Subroutine get_resolution: not called CC=8 SCAR=42.8
Pod documented
126  
127 0 ▶ $self->get_info() unless exists($self->{info});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"info"} 0 0 0
128 0 ▶ return if ( $self->{info}->{status} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"}{"status"} ne "ok" 0 0 0
129  
130 # quality and resolution
131 0 my $fmt_map = $self->{info}->{'fmt_map'};
132 0 ▶ unless ( $fmt_map ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt_map) { } 0 0 0
133 # fallback to fmt_list
134 # as fmt_map doesnt't seem to be supported any more
135 0 ▶ croak "no resolutions found!" unless $self->_fmt_list();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_fmt_list 0 0 0
136 }
137 else {
138 # process fmt_map
139 0 my @fmt_map_parts = split /%2F9%2F0%2F115%2C/, $fmt_map;
140 0 foreach my $item ( @fmt_map_parts ) {
141 0 my ($quality, $resolution) = split /%2F/, $item;
142 0 ▶ next unless $quality and $resolution;
Branch: 1 branch
BranchTrueFalseTotal
unless $quality and $resolution 0 0 0
Condition0%0/3
$quality and $resolution
!ll&&!rl&&r
000
MC/DC0%0/2
$quality and $resolution
$quality $resolution
Truth table0%0/3
A and B
A= $qualityB= $resolution
ABresult
0X0
100
111
143 0 $self->{resolution}->{$quality} = $resolution;
144 }
145 }
146  
147 0 return $self->{resolution};
148 }
149  
150 =head2 get_title
151  
152 Returns undef if status ne 'ok'.
153 Croaks if not available.
154  
155 =cut
156  
157 sub get_title {
158 0 ▶ my ($self) = @_;
Subroutine get_title: not called CC=4 SCAR=30.0
Pod documented
159  
160 0 ▶ $self->get_info() unless exists($self->{info});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"info"} 0 0 0
161 0 ▶ return if ( $self->{info}->{status} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"}{"status"} ne "ok" 0 0 0
162  
163 0 my $title = $self->{info}->{'title'};
164 0 ▶ croak "no title found!" unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 0 0
165  
166 0 $title = _url_decode($title);
167 0 $title =~ s/\+/ /g;
168 0 $self->{title} = $title;
169  
170 0 return $self->{title};
171 }
172  
173 =head2 get_url
174  
175 Returns undef if status ne 'ok'.
176 Croaks if not available.
177  
178 use WWW::YouTube::Info::Simple;
179
180 # id taken from YouTube video URL
181 my $id = 'foobar';
182
183 my $yt = WWW::YouTube::Info::Simple->new($id);
184
185 # hash reference holds values quality -> url
186 my $url = $yt->get_url();
187 # $url->{35} e.g.: http://v14.lscache1.c.youtube.com/videoplayback?ip=131.0.0.0 ..
188 # $url->{22} e.g.: http://v14.lscache1.c.youtube.com/videoplayback?ip=131.0.0.0 ..
189 # ..
190  
191 YouTube videos can be downloaded in given qualities by means of these URLs and the usual suspects (C<wget>, ..).
192  
193 =cut
194  
195 sub get_url {
196 0 ▶ my ($self) = @_;
Subroutine get_url: not called CC=8 SCAR=42.8
Pod documented
197  
198 0 ▶ $self->get_info() unless exists($self->{info});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"info"} 0 0 0
199 0 ▶ return if ( $self->{info}->{status} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"}{"status"} ne "ok" 0 0 0
200  
201 # quality and URL
202 0 my $fmt_url_map = $self->{info}->{'fmt_url_map'};
203 0 ▶ unless ( $fmt_url_map ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt_url_map) { } 0 0 0
204 # fallback to url_encoded_fmt_stream_map
205 # as fmt_url_map doesnt't seem to be supported any more
206 0 ▶ croak "no URLs found!" unless $self->_url_encoded_fmt_stream_map();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_url_encoded_fmt_stream_map 0 0 0
207 }
208 else {
209 # process fmt_url_map
210 0 my @fmt_url_map_parts = split /%2C/, $fmt_url_map;
211 0 foreach my $item ( @fmt_url_map_parts ) {
212 0 my ($quality, $url) = split /%7C/, $item;
213 0 ▶ next unless $quality and $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $quality and $url 0 0 0
Condition0%0/3
$quality and $url
!ll&&!rl&&r
000
MC/DC0%0/2
$quality and $url
$quality $url
Truth table0%0/3
A and B
A= $qualityB= $url
ABresult
0X0
100
111
214 0 $url = _url_decode($url);
215 0 $self->{url}->{$quality} = $url;
216 }
217 }
218  
219 0 return $self->{url};
220 }
221  
222 =head2 get_conn
223  
224 Returns undef if status ne 'ok'.
225 Croaks if not available.
226  
227 use WWW::YouTube::Info::Simple;
228
229 # id taken from YouTube video URL
230 my $id = 'foobar';
231
232 my $yt = WWW::YouTube::Info::Simple->new($id);
233
234 # URL decoded RTMPE URL
235 my $conn = $yt->get_conn(); # e.g.: rtmpe://cp59009.edgefcs.net/youtube?auth=daEcaboc8dvawbcbxazdobDcZajcDdgcfae ..
236  
237 A YouTube RTMPE stream can be accessed via this URL and downloaded by
238 means of the usual suspects (C<rtmpdump>, ..).
239 The URL looses its validity after approx. 30 seconds (experimental value).
240 Gathering a fresh RTMPE URL regarding the same VIDEO_ID and the
241 C<rtmpdump .. --resume> capability might circumvent this inconvenience.
242  
243 =cut
244  
245 sub get_conn {
246 0 ▶ my ($self) = @_;
Subroutine get_conn: not called CC=4 SCAR=30.0
Pod documented
247  
248 0 ▶ $self->get_info() unless exists($self->{info});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"info"} 0 0 0
249 0 ▶ return if ( $self->{info}->{status} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"}{"status"} ne "ok" 0 0 0
250  
251 0 my $conn = $self->{info}->{'conn'};
252 0 ▶ croak "no conn found!" unless $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn 0 0 0
253  
254 0 $self->{conn} = _url_decode($conn);
255  
256 0 return $self->{conn};
257 }
258  
259  
260 sub _url_encoded_fmt_stream_map {
261 0 ▶ my ($self) = @_;
Subroutine _url_encoded_fmt_stream_map: not called CC=8 SCAR=42.8
262  
263 0 ▶ $self->get_info() unless exists($self->{info});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"info"} 0 0 0
264 0 ▶ return if ( $self->{info}->{status} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"}{"status"} ne "ok" 0 0 0
265  
266 # quality and URL
267 0 my $url_encoded_fmt_stream_map = $self->{info}->{'url_encoded_fmt_stream_map'};
268 0 ▶ return unless $url_encoded_fmt_stream_map;
Branch: 1 branch
BranchTrueFalseTotal
unless $url_encoded_fmt_stream_map 0 0 0
269  
270 0 my @url_encoded_fmt_stream_map_parts = split /%2C/, $url_encoded_fmt_stream_map;
271 0 foreach my $item ( @url_encoded_fmt_stream_map_parts ) {
272 0 $item = _url_decode($item);
273 0 (my $url = $item) =~ s/.*url=(.*)&fallback_host=.*/$1/;
274 0 $url = _url_decode($url);
275 0 (my $quality = $url) =~ s/.*&itag=(\d+)&.*/$1/;
276 0 (my $signature = $item) =~ s/.*&sig=([\d\w.]+)&.*/$1/;
277 0 ▶ next unless $quality and $url and $signature;
Branch: 1 branch
BranchTrueFalseTotal
unless $quality and $url and $signature 0 0 0
Condition0%0/6
$quality and $url and $signature
!ll&&!rl&&r
000
$quality and $url
!ll&&!rl&&r
000
MC/DC0%0/3
$quality and $url and $signature
$quality $url $signature
Truth table0%0/4
A and B and C
A= $qualityB= $urlC= $signature
ABCresult
0XX0
10X0
1100
1111
278 0 $self->{url}->{$quality} = $url.'&signature='.$signature;
279 }
280  
281 0 return $self->{url};
282 }
283  
284 sub _fmt_list {
285 0 ▶ my ($self) = @_;
Subroutine _fmt_list: not called CC=7 SCAR=40.3
286  
287 0 ▶ $self->get_info() unless exists($self->{info});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"info"} 0 0 0
288 0 ▶ return if ( $self->{info}->{status} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"info"}{"status"} ne "ok" 0 0 0
289  
290 # quality and resolution
291 0 my $fmt_list = $self->{info}->{'fmt_list'};
292 0 ▶ return unless $fmt_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt_list 0 0 0
293  
294 0 my @fmt_list_parts = split /%2C/, $fmt_list;
295 0 foreach my $item ( @fmt_list_parts ) {
296 0 my ($quality, $resolution, @rest) = split /%2F/, $item;
297 0 ▶ next unless $quality and $resolution;
Branch: 1 branch
BranchTrueFalseTotal
unless $quality and $resolution 0 0 0
Condition0%0/3
$quality and $resolution
!ll&&!rl&&r
000
MC/DC0%0/2
$quality and $resolution
$quality $resolution
Truth table0%0/3
A and B
A= $qualityB= $resolution
ABresult
0X0
100
111
298 0 $self->{resolution}->{$quality} = $resolution;
299 }
300  
301 0 return $self->{resolution};
302 }
303  
304 sub _url_encode {
305 0 ▶ my $string = shift;
Subroutine _url_encode: not called CC=1 SCAR=6.9
306  
307 # URLencode
308 0 $string =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
309  
310 0 return $string;
311 }
312  
313 sub _url_decode {
314 0 ▶ my $string = shift;
Subroutine _url_decode: not called CC=1 SCAR=6.9
315  
316 # URLdecode
317 0 $string =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
318  
319 0 return $string;
320 }
321  
322 8 1;
323  
324 __END__
↑ Summary