| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # | 
| 2 |  |  |  |  |  |  | # $Id: Videos.pm 11 2007-04-09 04:34:01Z hironori.yoshida $ | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | package WebService::YouTube::Videos; | 
| 5 | 2 |  |  | 2 |  | 767 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 71 |  | 
| 6 | 2 |  |  | 2 |  | 9 | use warnings; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 53 |  | 
| 7 | 2 |  |  | 2 |  | 11 | use version; our $VERSION = qv('1.0.3'); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 2 |  |  | 2 |  | 182 | use Carp; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 150 |  | 
| 10 | 2 |  |  | 2 |  | 1349 | use LWP::UserAgent; | 
|  | 2 |  |  |  |  | 50798 |  | 
|  | 2 |  |  |  |  | 55 |  | 
| 11 | 2 |  |  | 2 |  | 644 | use WebService::YouTube::Util; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 58 |  | 
| 12 | 2 |  |  | 2 |  | 442 | use WebService::YouTube::Video; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 13 |  | 
| 13 | 2 |  |  | 2 |  | 984 | use XML::Simple; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | use base qw(Class::Accessor::Fast); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw(dev_id ua)); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub new { | 
| 20 |  |  |  |  |  |  | my ( $class, @args ) = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | my $self = $class->SUPER::new(@args); | 
| 23 |  |  |  |  |  |  | if ( !$self->dev_id ) { | 
| 24 |  |  |  |  |  |  | croak 'dev_id is required'; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  | if ( !$self->ua ) { | 
| 27 |  |  |  |  |  |  | $self->ua( LWP::UserAgent->new ); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | return $self; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub parse_xml { | 
| 33 |  |  |  |  |  |  | my ( $self, $xml ) = @_; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | my $ut_response = XMLin( $xml, ForceArray => [qw(comment channel video)] ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | if ( !$ut_response ) { | 
| 38 |  |  |  |  |  |  | carp 'invalid XML'; | 
| 39 |  |  |  |  |  |  | return; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | if ( $ut_response->{status} ne 'ok' ) { | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =begin comment | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | See L and each B | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =end comment | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =cut | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | carp( | 
| 53 |  |  |  |  |  |  | sprintf "status: %s\ncode: %d\ndescription: %s", | 
| 54 |  |  |  |  |  |  | $ut_response->{status}, | 
| 55 |  |  |  |  |  |  | $ut_response->{error}->{code}, | 
| 56 |  |  |  |  |  |  | $ut_response->{error}->{description} | 
| 57 |  |  |  |  |  |  | ); | 
| 58 |  |  |  |  |  |  | return; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | if ( exists $ut_response->{video_list} ) { | 
| 62 |  |  |  |  |  |  | my $video_list = $ut_response->{video_list}->{video}; | 
| 63 |  |  |  |  |  |  | my @videos; | 
| 64 |  |  |  |  |  |  | foreach my $video_id ( keys %{$video_list} ) { | 
| 65 |  |  |  |  |  |  | my $video = | 
| 66 |  |  |  |  |  |  | WebService::YouTube::Video->new( $video_list->{$video_id} ); | 
| 67 |  |  |  |  |  |  | $video->id($video_id); | 
| 68 |  |  |  |  |  |  | push @videos, $video; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | return @videos; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | if ( exists $ut_response->{video_details} ) { | 
| 74 |  |  |  |  |  |  | my $video = | 
| 75 |  |  |  |  |  |  | WebService::YouTube::Video->new( $ut_response->{video_details} ); | 
| 76 |  |  |  |  |  |  | return $video; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | carp( sprintf '%s: unknown response at %s', | 
| 80 |  |  |  |  |  |  | [ keys %{$ut_response} ]->[0], $ut_response ); | 
| 81 |  |  |  |  |  |  | return; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub get_details { | 
| 85 |  |  |  |  |  |  | my ( $self, $video_id ) = @_; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | if ( ref $video_id ) { | 
| 88 |  |  |  |  |  |  | $video_id = $video_id->id; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  | my $uri = | 
| 91 |  |  |  |  |  |  | WebService::YouTube::Util->rest_uri( $self->dev_id, | 
| 92 |  |  |  |  |  |  | 'youtube.videos.get_details', { video_id => $video_id } ); | 
| 93 |  |  |  |  |  |  | my $res = $self->ua->get($uri); | 
| 94 |  |  |  |  |  |  | if ( !$res->is_success ) { | 
| 95 |  |  |  |  |  |  | carp $res->status_line; | 
| 96 |  |  |  |  |  |  | return; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | my $video = $self->parse_xml( $res->content ); | 
| 99 |  |  |  |  |  |  | if ( !$video ) { | 
| 100 |  |  |  |  |  |  | return; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  | $video->id($video_id); | 
| 103 |  |  |  |  |  |  | return $video; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | sub list_by_tag { | 
| 107 |  |  |  |  |  |  | my ( $self, $tag, $fields ) = @_; | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | my $uri = WebService::YouTube::Util->rest_uri( | 
| 110 |  |  |  |  |  |  | $self->dev_id, | 
| 111 |  |  |  |  |  |  | 'youtube.videos.list_by_tag', | 
| 112 |  |  |  |  |  |  | { | 
| 113 |  |  |  |  |  |  | tag => $tag, | 
| 114 |  |  |  |  |  |  | %{ $fields || {} } | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  | ); | 
| 117 |  |  |  |  |  |  | my $res = $self->ua->get($uri); | 
| 118 |  |  |  |  |  |  | if ( !$res->is_success ) { | 
| 119 |  |  |  |  |  |  | carp $res->status_line; | 
| 120 |  |  |  |  |  |  | return; | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  | return $self->parse_xml( $res->content ); | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub list_by_user { | 
| 126 |  |  |  |  |  |  | my ( $self, $user ) = @_; | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | my $uri = | 
| 129 |  |  |  |  |  |  | WebService::YouTube::Util->rest_uri( $self->dev_id, | 
| 130 |  |  |  |  |  |  | 'youtube.videos.list_by_user', { user => $user } ); | 
| 131 |  |  |  |  |  |  | my $res = $self->ua->get($uri); | 
| 132 |  |  |  |  |  |  | if ( !$res->is_success ) { | 
| 133 |  |  |  |  |  |  | carp $res->status_line; | 
| 134 |  |  |  |  |  |  | return; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  | return $self->parse_xml( $res->content ); | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | sub list_featured { | 
| 140 |  |  |  |  |  |  | my $self = shift; | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | my $uri = WebService::YouTube::Util->rest_uri( $self->dev_id, | 
| 143 |  |  |  |  |  |  | 'youtube.videos.list_featured' ); | 
| 144 |  |  |  |  |  |  | my $res = $self->ua->get($uri); | 
| 145 |  |  |  |  |  |  | if ( !$res->is_success ) { | 
| 146 |  |  |  |  |  |  | carp $res->status_line; | 
| 147 |  |  |  |  |  |  | return; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  | return $self->parse_xml( $res->content ); | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | 1; | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | __END__ |