File Coverage

blib/lib/WebService/YouTube/Util.pm
Criterion Covered Total %
statement 37 77 48.0
branch 4 28 14.2
condition 2 15 13.3
subroutine 9 11 81.8
pod 4 4 100.0
total 56 135 41.4


line stmt bran cond sub pod time code
1             #
2             # $Id: Util.pm 11 2007-04-09 04:34:01Z hironori.yoshida $
3             #
4             package WebService::YouTube::Util;
5 5     5   755 use strict;
  5         13  
  5         178  
6 5     5   28 use warnings;
  5         10  
  5         149  
7 5     5   28 use version; our $VERSION = qv('1.0.3');
  5         11  
  5         56  
8              
9 5     5   428 use Carp;
  5         9  
  5         397  
10 5     5   1051 use LWP::UserAgent;
  5         47152  
  5         171  
11 5     5   32 use URI::Escape qw(uri_escape uri_escape_utf8);
  5         11  
  5         328  
12 5     5   5851 use Encode ();
  5         59538  
  5         3491  
13              
14             sub rss_uri {
15 1     1 1 8 my ( $class, $type, $arg ) = @_;
16              
17 1 0 33     10 if ( $type ne 'global' && $type ne 'tag' && $type ne 'user' ) {
      33        
18 0         0 croak "type of $type is not supported";
19             }
20              
21 1 50       12 if ( Encode::is_utf8($arg) ) {
22 0         0 $arg = uri_escape_utf8($arg);
23             }
24             else {
25 1         7 $arg = uri_escape($arg);
26             }
27              
28 1 50       38 if ( $type eq 'user' ) {
29 0         0 $arg = lc $arg . '/videos';
30             }
31 1         11 return "http://www.youtube.com/rss/$type/$arg.rss";
32             }
33              
34             sub rest_uri {
35 1     1 1 3 my ( $class, $dev_id, $method, $fields ) = @_;
36              
37 1         3 my $query = q{};
38 1 50       3 if ($fields) {
39 1         3 foreach my $key ( keys %{$fields} ) {
  1         5  
40 1         2 my $value = $fields->{$key};
41 1 50       5 if ( Encode::is_utf8($value) ) {
42 0         0 $value = uri_escape_utf8($value);
43             }
44             else {
45 1         4 $value = uri_escape($value);
46             }
47 1         15 $query .= sprintf '&%s=%s', $key, $value;
48             }
49             }
50             return
51 1         5 "http://www.youtube.com/api2_rest?dev_id=$dev_id&method=$method$query";
52             }
53              
54             sub get_video_uri {
55 0     0 1   my ( $class, $video, $args ) = @_;
56              
57 0 0         if ( !$video ) {
58 0           return;
59             }
60              
61 0   0       $args->{ua} ||= LWP::UserAgent->new;
62              
63 0           my ( $video_id, $video_uri );
64 0 0         if ( ref $video ) {
65 0           $video_id = $video->id;
66 0           $video_uri = $video->url;
67             }
68             else {
69 0           $video_id = $video;
70             }
71 0   0       $video_uri ||= "http://youtube.com/?v=$video_id";
72              
73 0           my $res = $args->{ua}->get($video_uri);
74 0 0         if ( !$res->is_success ) {
75 0           carp $res->status_line;
76 0           return;
77             }
78              
79 0           my $content = $res->content;
80 0 0         if ( $content =~ m{"/player2\.swf\?([^"]+)",\s*"movie_player"}msx ) {
81 0           return "http://youtube.com/get_video.php?$1";
82             }
83 0 0         if ( $content =~ m{\bt\b[^:]*:\s*(['"])(.+?)\1}msx ) {
84 0           return "http://youtube.com/get_video.php?video_id=$video_id&t=$2";
85             }
86 0 0         if ( $content =~ m{class="errorBox"[^>]*>\s*([^<]+?)\s*<}msx ) {
87 0           carp "$video_id: $1";
88 0           return;
89             }
90 0           carp "$video_id: got a page but it is invalid page\n$content";
91 0           return;
92             }
93              
94             sub get_video {
95 0     0 1   my ( $class, $video, $args ) = @_;
96              
97 0 0         if ( !$video ) {
98 0           return;
99             }
100              
101 0   0       $args->{ua} ||= LWP::UserAgent->new;
102              
103 0           my $video_uri = $class->get_video_uri( $video, $args );
104 0 0         if ( !$video_uri ) {
105 0           return;
106             }
107 0           my $res = $args->{ua}->get($video_uri);
108 0 0         if ( !$res->is_success ) {
109 0           carp $res->status_line;
110 0           return;
111             }
112 0           return $res->content;
113             }
114              
115             1;
116              
117             __END__