| 1 |
# |
| 2 |
# $Id: Videos.pm 11 2007-04-09 04:34:01Z hironori.yoshida $ |
| 3 |
# |
| 4 |
package WebService::YouTube::Videosscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/; |
| 5 |
use strictscip-perl cpan WebService-YouTube 1.0.3 strict/; |
| 6 |
use warningsscip-perl cpan WebService-YouTube 1.0.3 warnings/; |
| 7 |
use versionscip-perl cpan WebService-YouTube 1.0.3 version/; our $VERSIONscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/$VERSION. = qvscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/qv().('1.0.3'); |
| 8 |
|
| 9 |
use Carpscip-perl cpan WebService-YouTube 1.0.3 Carp/; |
| 10 |
use LWP::UserAgentscip-perl cpan WebService-YouTube 1.0.3 LWP/UserAgent/; |
| 11 |
use WebService::YouTube::Utilscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Util/; |
| 12 |
use WebService::YouTube::Videoscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Video/; |
| 13 |
use XML::Simplescip-perl cpan WebService-YouTube 1.0.3 XML/Simple/; |
| 14 |
|
| 15 |
use basescip-perl cpan WebService-YouTube 1.0.3 base/ qw(Class::Accessor::Fast); |
| 16 |
|
| 17 |
__PACKAGE__->mk_accessorsscip-perl cpan WebService-YouTube 1.0.3 __PACKAGE__/mk_accessors().(qw(dev_id ua)); |
| 18 |
|
| 19 |
sub newscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/new(). { |
| 20 |
my ( $class, @args ) = @_scip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/`@_`.; |
| 21 |
|
| 22 |
my $self = $class->SUPER::newscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/`SUPER::new`().(@args); |
| 23 |
if ( !$self->dev_idscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/dev_id(). ) { |
| 24 |
croakscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/croak(). 'dev_id is required'; |
| 25 |
} |
| 26 |
if ( !$self->uascip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/ua(). ) { |
| 27 |
$self->uascip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/ua().( LWP::UserAgent->newscip-perl cpan WebService-YouTube 1.0.3 LWP/UserAgent/new(). ); |
| 28 |
} |
| 29 |
return $self; |
| 30 |
} |
| 31 |
|
| 32 |
sub parse_xmlscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/parse_xml(). { |
| 33 |
my ( $self, $xml ) = @_scip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/`@_`.; |
| 34 |
|
| 35 |
my $ut_response = XMLinscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/XMLin().( $xml, ForceArray => [qw(comment channel video)] ); |
| 36 |
|
| 37 |
if ( !$ut_response ) { |
| 38 |
carpscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/carp(). 'invalid XML'; |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if ( $ut_response->{status} ne 'ok' ) { |
| 43 |
|
| 44 |
=begin comment |
| 45 |
|
| 46 |
See L<http://youtube.com/dev_error_codes> and each B<API Function Reference> |
| 47 |
|
| 48 |
=end comment |
| 49 |
|
| 50 |
=cut |
| 51 |
|
| 52 |
carpscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/carp().( |
| 53 |
sprintfscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/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 ( existsscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/exists(). $ut_response->{video_list} ) { |
| 62 |
my $video_list = $ut_response->{video_list}->{video}; |
| 63 |
my @videos; |
| 64 |
foreach my $video_id ( keysscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/keys(). %{$video_list} ) { |
| 65 |
my $video = |
| 66 |
WebService::YouTube::Video->newscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Video/new().( $video_list->{$video_id} ); |
| 67 |
$vidscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Video/id().eo->id($video_id); |
| 68 |
pushscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/push(). @videos, $video; |
| 69 |
} |
| 70 |
return @videos; |
| 71 |
} |
| 72 |
|
| 73 |
if ( existsscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/exists(). $ut_response->{video_details} ) { |
| 74 |
my $video = |
| 75 |
WebService::YouTube::Video->newscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Video/new().( $ut_response->{video_details} ); |
| 76 |
return $video; |
| 77 |
} |
| 78 |
|
| 79 |
carpscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/carp().( sprintfscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/sprintf(). '%s: unknown response at %s', |
| 80 |
[ keysscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/keys(). %{$ut_response} ]->[0], $ut_response ); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
sub get_detailsscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/get_details(). { |
| 85 |
my ( $self, $video_id ) = @_scip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/`@_`.; |
| 86 |
|
| 87 |
if ( refscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/ref(). $video_id ) { |
| 88 |
$video_id = $vidscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/id().eo_id->id; |
| 89 |
} |
| 90 |
my $uri = |
| 91 |
WebService::YouTube::Util->rest_uriscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Util/rest_uri().( $self->dev_idscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/dev_id()., |
| 92 |
'youtube.videos.get_details', { video_id => $video_id } ); |
| 93 |
my $res = $self->uascip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/ua().->getscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/get().($uri); |
| 94 |
if ( !$res->is_successscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/is_success(). ) { |
| 95 |
carpscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/carp(). $res->status_linescip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/status_line().; |
| 96 |
return; |
| 97 |
} |
| 98 |
my $video = $self->parse_xmlscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/parse_xml().( $res->contentscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/content(). ); |
| 99 |
if ( !$video ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
$vidscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/id().eo->id($video_id); |
| 103 |
return $video; |
| 104 |
} |
| 105 |
|
| 106 |
sub list_by_tagscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/list_by_tag(). { |
| 107 |
my ( $self, $tag, $fields ) = @_scip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/`@_`.; |
| 108 |
|
| 109 |
my $uri = WebService::YouTube::Util->rest_uriscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Util/rest_uri().( |
| 110 |
$self->dev_idscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/dev_id()., |
| 111 |
'youtube.videos.list_by_tag', |
| 112 |
{ |
| 113 |
tag => $tag, |
| 114 |
%{ $fields || {} } |
| 115 |
} |
| 116 |
); |
| 117 |
my $res = $self->uascip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/ua().->getscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/get().($uri); |
| 118 |
if ( !$res->is_successscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/is_success(). ) { |
| 119 |
carpscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/carp(). $res->status_linescip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/status_line().; |
| 120 |
return; |
| 121 |
} |
| 122 |
return $self->parse_xmlscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/parse_xml().( $res->contentscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/content(). ); |
| 123 |
} |
| 124 |
|
| 125 |
sub list_by_userscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/list_by_user(). { |
| 126 |
my ( $self, $user ) = @_scip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/`@_`.; |
| 127 |
|
| 128 |
my $uri = |
| 129 |
WebService::YouTube::Util->rest_uriscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Util/rest_uri().( $self->dev_idscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/dev_id()., |
| 130 |
'youtube.videos.list_by_user', { user => $user } ); |
| 131 |
my $res = $self->uascip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/ua().->getscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/get().($uri); |
| 132 |
if ( !$res->is_successscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/is_success(). ) { |
| 133 |
carpscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/carp(). $res->status_linescip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/status_line().; |
| 134 |
return; |
| 135 |
} |
| 136 |
return $self->parse_xmlscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/parse_xml().( $res->contentscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/content(). ); |
| 137 |
} |
| 138 |
|
| 139 |
sub list_featuredscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/list_featured(). { |
| 140 |
my $self = shiftscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/shift().; |
| 141 |
|
| 142 |
my $uri = WebService::YouTube::Util->rest_uriscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Util/rest_uri().( $self->dev_idscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/dev_id()., |
| 143 |
'youtube.videos.list_featured' ); |
| 144 |
my $res = $self->uascip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/ua().->getscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/get().($uri); |
| 145 |
if ( !$res->is_successscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/is_success(). ) { |
| 146 |
carpscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/carp(). $res->status_linescip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/status_line().; |
| 147 |
return; |
| 148 |
} |
| 149 |
return $self->parse_xmlscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/parse_xml().( $res->contentscip-perl cpan WebService-YouTube 1.0.3 WebService/YouTube/Videos/content(). ); |
| 150 |
} |
| 151 |
|
| 152 |
1; |
| 153 |
|
| 154 |
__END__ |
| 155 |
|
| 156 |
=head1 NAME |
| 157 |
|
| •158 |
WebService::YouTube::Videos - Perl interface to youtube.videos.* |
| 159 |
|
| 160 |
=head1 VERSION |
| 161 |
|
| 162 |
This document describes WebService::YouTube::Videos version 1.0.3 |
| 163 |
|
| 164 |
=head1 SYNOPSIS |
| 165 |
|
| 166 |
use WebService::YouTube::Videos; |
| 167 |
|
| 168 |
my $api = WebService::YouTube::Videos->new( { dev_id => YOUR_DEV_ID } ); |
| 169 |
|
| 170 |
# Call API youtube.videos.list_featured |
| 171 |
my @videos = $api->list_featured; |
| 172 |
foreach my $video (@videos) { |
| 173 |
# $video->isa('WebService::YouTube::Video'); |
| 174 |
} |
| 175 |
|
| 176 |
# Call other APIs |
| 177 |
my @videos = $api->list_by_user($user); |
| 178 |
my @videos = $api->list_by_tag($tag); |
| 179 |
|
| 180 |
my $video = $api->get_details($video_id); |
| 181 |
|
| 182 |
# Parse XML |
| 183 |
my @video = $api->parse_xml($xml); # when $xml contains <video_list> |
| 184 |
my $video = $api->parse_xml($xml); # when $xml contains <video_details> |
| 185 |
|
| 186 |
=head1 DESCRIPTION |
| 187 |
|
| 188 |
This is a Perl interface to YouTube REST API. |
| 189 |
|
| 190 |
See B<Developer APIs> L<http://youtube.com/dev> and B<Developer API -- REST Interface> L<http://youtube.com/dev_rest> for details. |
| 191 |
|
| 192 |
=head1 SUBROUTINES/METHODS |
| 193 |
|
| 194 |
=head2 new(\%fields) |
| 195 |
|
| 196 |
Creates and returns a new WebService::YouTube::Videos object. |
| 197 |
%fields can contain parameters enumerated in L</ACCESSORS> section. |
| 198 |
|
| 199 |
=head2 parse_xml($xml) |
| 200 |
|
| 201 |
Parses XML and returns the result. |
| 202 |
$xml should be an object that L<XML::Simple> can understand. |
| 203 |
|
| 204 |
=head2 get_details( $video_id ) |
| 205 |
|
| 206 |
Returns a L<WebService::YouTube::Video> object. |
| 207 |
$video_id is an ID of the video which you want to get details. |
| 208 |
|
| 209 |
See L<http://youtube.com/dev_api_ref?m=youtube.videos.get_details> for details. |
| 210 |
|
| 211 |
=head2 list_by_tag( $tag, \%fields ) |
| 212 |
|
| 213 |
Returns an array of L<WebService::YouTube::Video> object. |
| 214 |
$tag is a keyword string separated by a space. |
| 215 |
%fields can contain the optional parameters. |
| 216 |
|
| 217 |
=over |
| 218 |
|
| 219 |
=item page |
| 220 |
|
| 221 |
1 <= page |
| 222 |
|
| 223 |
=item per_page |
| 224 |
|
| 225 |
per_page <= 100 (default 20) |
| 226 |
|
| 227 |
=back |
| 228 |
|
| 229 |
See L<http://youtube.com/dev_api_ref?m=youtube.videos.list_by_tag> for details. |
| 230 |
|
| 231 |
=head2 list_by_user( $user ) |
| 232 |
|
| 233 |
Returns an array of L<WebService::YouTube::Video> object. |
| 234 |
$tag is a keyword string separated by a space. |
| 235 |
%fields can contain optional parameters. |
| 236 |
|
| 237 |
See L<http://youtube.com/dev_api_ref?m=youtube.videos.list_by_user> for details. |
| 238 |
|
| 239 |
=head2 list_featured( ) |
| 240 |
|
| 241 |
Returns an array of L<WebService::YouTube::Video> object. |
| 242 |
|
| 243 |
See L<http://youtube.com/dev_api_ref?m=youtube.videos.list_featured> for details. |
| 244 |
|
| 245 |
=head2 ACCESSORS |
| 246 |
|
| 247 |
=head3 dev_id |
| 248 |
|
| 249 |
Developer ID |
| 250 |
|
| 251 |
=head3 ua |
| 252 |
|
| 253 |
L<LWP::UserAgent> object |
| 254 |
|
| 255 |
=head1 DIAGNOSTICS |
| 256 |
|
| 257 |
=over |
| 258 |
|
| 259 |
=item dev_id is required |
| 260 |
|
| 261 |
Developer ID is required when you call API of YouTube. |
| 262 |
|
| 263 |
=item invalid XML |
| 264 |
|
| 265 |
The XML is not a YouTube's XML. |
| 266 |
|
| 267 |
=item unknown response |
| 268 |
|
| 269 |
The ut_response is neither <video_list> nor <video_details>. |
| 270 |
|
| 271 |
=back |
| 272 |
|
| 273 |
=head1 CONFIGURATION AND ENVIRONMENT |
| 274 |
|
| 275 |
WebService::YouTube::Videos requires no configuration files or environment variables. |
| 276 |
|
| 277 |
=head1 DEPENDENCIES |
| 278 |
|
| 279 |
L<Class::Accessor::Fast>, L<LWP::UserAgent>, L<XML::Simple>, L<WebService::YouTube::Util>, L<WebService::YouTube::Video> |
| 280 |
|
| 281 |
=head1 INCOMPATIBILITIES |
| 282 |
|
| 283 |
None reported. |
| 284 |
|
| 285 |
=head1 BUGS AND LIMITATIONS |
| 286 |
|
| 287 |
No bugs have been reported. |
| 288 |
|
| 289 |
Please report any bugs or feature requests to |
| 290 |
C<[email protected]>, or through the web interface at |
| 291 |
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-YouTube>. |
| 292 |
I will be notified, and then you'll automatically be notified of progress on |
| 293 |
your bug as I make changes. |
| 294 |
|
| 295 |
=head1 AUTHOR |
| 296 |
|
| 297 |
Hironori Yoshida <[email protected]> |
| 298 |
|
| 299 |
=head1 LICENSE AND COPYRIGHT |
| 300 |
|
| 301 |
This module is free software; you can redistribute it and/or |
| 302 |
modify it under the same terms as Perl itself. See L<perlartistic>. |
| 303 |
|
| 304 |
=cut |