File Coverage

blib/lib/WWW/YouTube/VideoURI.pm
Criterion Covered Total %
statement 21 35 60.0
branch 0 4 0.0
condition n/a
subroutine 7 9 77.7
pod 0 2 0.0
total 28 50 56.0


line stmt bran cond sub pod time code
1             # $Id: VideoURI.pm,v 1.1 2007/08/29 17:35:11 gavin Exp $
2             # Copyright (c) Gavin Brown. All rights reserved. This program is free
3             # software; you can redistribute it and/or modify it under the same
4             # terms as Perl itself.
5             package WWW::YouTube::VideoURI;
6 1     1   5931 use Carp;
  1         3  
  1         64  
7 1     1   2207 use HTTP::Request::Common;
  1         55423  
  1         215  
8 1     1   3214 use LWP;
  1         50013  
  1         37  
9 1     1   19 use URI;
  1         1  
  1         25  
10 1     1   5 use URI::Escape;
  1         1  
  1         91  
11 1     1   5 use vars qw($VERSION);
  1         2  
  1         52  
12 1     1   5 use strict;
  1         4  
  1         364  
13              
14             our $VERSION = '0.1';
15              
16             sub new {
17 0     0 0   my $self = bless({}, shift);
18 0           $self->{ua} = LWP::UserAgent->new(
19             agent => sprintf(
20             '%s/%s, %s',
21             ref($self),
22             $VERSION,
23             ucfirst($^O)
24             ),
25             max_redirect => 0,
26             requests_redirectable => [],
27             );
28 0           return $self;
29             }
30              
31             sub get_video_uri {
32 0     0 0   my ($self, $uri_s) = @_;
33 0           my $uri = URI->new($uri_s);
34 0           my %params = $uri->query_form;
35 0 0         croak("Malformed URL or missing parameter") if ($params{v} eq '');
36              
37 0           my $new_uri = sprintf(
38             'http://www.youtube.com/v/%s',
39             uri_escape($params{v})
40             );
41 0           my $req = GET($new_uri);
42 0           my $res = $self->{ua}->request($req);
43              
44 0 0         croak($res->status_line) if ($res->is_error);
45              
46 0           my $target = URI->new_abs($res->header('Location'), $new_uri);
47 0           my %target_params = $target->query_form;
48              
49 0           return sprintf(
50             'http://www.youtube.com/get_video.php?video_id=%s&t=%s',
51             uri_escape($params{v}),
52             uri_escape($target_params{t})
53             );
54             }
55              
56             1;
57              
58             __END__