root/lang/perl/WebService-YouTube/trunk/lib/WebService/YouTube/Videos.pm @ 28693

Revision 28693, 6.9 kB (checked in by holidays-l, 4 years ago)

バージョン更新

Line 
1#
2# $Id: Videos.pm 11 2007-04-09 04:34:01Z hironori.yoshida $
3#
4package WebService::YouTube::Videos;
5use strict;
6use warnings;
7use version; our $VERSION = qv('1.0.3');
8
9use Carp;
10use LWP::UserAgent;
11use WebService::YouTube::Util;
12use WebService::YouTube::Video;
13use XML::Simple;
14
15use base qw(Class::Accessor::Fast);
16
17__PACKAGE__->mk_accessors(qw(dev_id ua));
18
19sub 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
32sub 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
46See L<http://youtube.com/dev_error_codes> and each B<API Function Reference>
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
84sub 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
106sub 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
125sub 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
139sub 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
1521;
153
154__END__
155
156=head1 NAME
157
158WebService::YouTube::Videos - Perl interfece to youtube.videos.*
159
160=head1 VERSION
161
162This 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
188This is a Perl interface to YouTube REST API.
189
190See 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
196Creates 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
201Parses 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
206Returns a L<WebService::YouTube::Video> object.
207$video_id is an ID of the video which you want to get details.
208
209See L<http://youtube.com/dev_api_ref?m=youtube.videos.get_details> for details.
210
211=head2 list_by_tag( $tag, \%fields )
212
213Returns 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
2211 <= page
222
223=item per_page
224
225per_page <= 100 (default 20)
226
227=back
228
229See L<http://youtube.com/dev_api_ref?m=youtube.videos.list_by_tag> for details.
230
231=head2 list_by_user( $user )
232
233Returns 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
237See L<http://youtube.com/dev_api_ref?m=youtube.videos.list_by_user> for details.
238
239=head2 list_featured( )
240
241Returns an array of L<WebService::YouTube::Video> object.
242
243See L<http://youtube.com/dev_api_ref?m=youtube.videos.list_featured> for details.
244
245=head2 ACCESSORS
246
247=head3 dev_id
248
249Developer ID
250
251=head3 ua
252
253L<LWP::UserAgent> object
254
255=head1 DIAGNOSTICS
256
257=over
258
259=item dev_id is required
260
261Developer ID is required when you call API of YouTube.
262
263=item invalid XML
264
265The XML is not a YouTube's XML.
266
267=item unknown response
268
269The ut_response is neither <video_list> nor <video_details>.
270
271=back
272
273=head1 CONFIGURATION AND ENVIRONMENT
274
275WebService::YouTube::Videos requires no configuration files or environment variables.
276
277=head1 DEPENDENCIES
278
279L<Class::Accessor::Fast>, L<LWP::UserAgent>, L<XML::Simple>, L<WebService::YouTube::Util>, L<WebService::YouTube::Video>
280
281=head1 INCOMPATIBILITIES
282
283None reported.
284
285=head1 BUGS AND LIMITATIONS
286
287No bugs have been reported.
288
289Please report any bugs or feature requests to
290C<bug-webservice-youtube@rt.cpan.org>, or through the web interface at
291L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-YouTube>.
292I will be notified, and then you'll automatically be notified of progress on
293your bug as I make changes.
294
295=head1 AUTHOR
296
297Hironori Yoshida <yoshida@cpan.org>
298
299=head1 LICENSE AND COPYRIGHT
300
301This module is free software; you can redistribute it and/or
302modify it under the same terms as Perl itself. See L<perlartistic>.
303
304=cut
Note: See TracBrowser for help on using the browser.