| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Net::Google::PicasaWeb::Media; | ||
| 5 | use Moose; | ||
| 6 | |||
| 7 | extends 'Net::Google::PicasaWeb::Base'; | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Net::Google::PicasaWeb::Media - hold information about a photo or video | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | my @photos = $album->list_media_entries; | ||
| 18 | for my $photo (@photos) { | ||
| 19 | my $media_info = $photo->photo; | ||
| 20 | |||
| 21 | print "Image Title: ", $media_info->title, "\n"; | ||
| 22 | print "Image Description: ", $media_info->description, "\n\n"; | ||
| 23 | |||
| 24 | my $main_photo = $media_info->content; | ||
| 25 | print "Image URL: ", $main_photo->url, "\n"; | ||
| 26 | print "Image MIME Type: ", $main_photo->mime_type, "\n"; | ||
| 27 | print "Image Medium: ", $main_photo->medium, "\n"; | ||
| 28 | |||
| 29 | print "Thumbnails:\n\n"; | ||
| 30 | |||
| 31 | for my $thumbnail ($media_info->thumbnails) { | ||
| 32 | print " Thumbnail URL: ", $thumbnail->url, "\n"; | ||
| 33 | print " Thumbnail Dimensions: ", | ||
| 34 | $thumbnail->width, "x", $thumbnail->height, "\n\n"; | ||
| 35 | |||
| 36 | my $photo_data = $thumbnail->fetch; | ||
| 37 | $thumbnail->fetch( file => 'thumbnail.jpg' ); | ||
| 38 | } | ||
| 39 | |||
| 40 | my $photo_data = $main_photo->fetch; | ||
| 41 | $main_photo->fetch( file => 'photo.jpg' ); | ||
| 42 | } | ||
| 43 | |||
| 44 | =head1 DESCRIPTION | ||
| 45 | |||
| 46 | This is where you will find information about the photos, videos, and thumbnails themselves. You can get information about them with this object, such as the URL that can be used to download the media file. This object (and its children) also provide some features to fetching this information. | ||
| 47 | |||
| 48 | =head1 ATTRIBUTE | ||
| 49 | |||
| 50 | =head2 title | ||
| 51 | |||
| 52 | This is the title of the photo or video. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | has title => ( | ||
| 57 | is => 'rw', | ||
| 58 | isa => 'Str', | ||
| 59 | ); | ||
| 60 | |||
| 61 | =head2 description | ||
| 62 | |||
| 63 | This is a description for the photo or video. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | has description => ( | ||
| 68 | is => 'rw', | ||
| 69 | isa => 'Str', | ||
| 70 | ); | ||
| 71 | |||
| 72 | =head2 content | ||
| 73 | |||
| 74 | This is the main photo or video item attached to the media information entry. See L</MEDIA CONTENT> below for information about the object returned. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | has content => ( | ||
| 79 | is => 'rw', | ||
| 80 | isa => 'Net::Google::PicasaWeb::Media::Content', | ||
| 81 | ); | ||
| 82 | |||
| 83 | =head2 thumbnails | ||
| 84 | |||
| 85 | This is an array of object containing information about the thumbnails that were attached when the photo was retrieved from the server. See L</THUMBNAILS> below for information about these objects. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | has thumbnails => ( | ||
| 90 | is => 'rw', | ||
| 91 | isa => 'ArrayRef[Net::Google::PicasaWeb::Media::Thumbnail]', | ||
| 92 | auto_deref => 1, | ||
| 93 | ); | ||
| 94 | |||
| 95 | =head1 METHODS | ||
| 96 | |||
| 97 | =head2 from_feed | ||
| 98 | |||
| 99 | Builds a media class from a service object and reference to a C<< <media:group> >> object in L<XML::Twig::Elt>. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub from_feed { | ||
| 104 | my ($class, $service, $media_group) = @_; | ||
| 105 | |||
| 106 | my $content = $media_group->first_child('media:content'); | ||
| 107 | |||
| 108 | my %params = ( | ||
| 109 | service => $service, | ||
| 110 | twig => $media_group, | ||
| 111 | title => $media_group->field('media:title'), | ||
| 112 | description => $media_group->field('media:description'), | ||
| 113 | ); | ||
| 114 | |||
| 115 | my $self = $class->new(\%params); | ||
| 116 | |||
| 117 | $self->content( | ||
| 118 | Net::Google::PicasaWeb::Media::Content->new( | ||
| 119 | media => $self, | ||
| 120 | url => $content->att('url'), | ||
| 121 | mime_type => $content->att('type'), | ||
| 122 | medium => $content->att('medium'), | ||
| 123 | ) | ||
| 124 | ); | ||
| 125 | $self->thumbnails( | ||
| 126 | [ | ||
| 127 | map { | ||
| 128 | Net::Google::PicasaWeb::Media::Thumbnail->new( | ||
| 129 | media => $self, | ||
| 130 | url => $_->att('url'), | ||
| 131 | width => $_->att('width'), | ||
| 132 | height => $_->att('height'), | ||
| 133 | ) | ||
| 134 | } $media_group->children('media:thumbnail') | ||
| 135 | ] | ||
| 136 | ); | ||
| 137 | |||
| 138 | return $self; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _fetch { | ||
| 142 | my ($self, $content, %params) = @_; | ||
| 143 | my $url = $content->url; | ||
| 144 | |||
| 145 | my %header; | ||
| 146 | $header{':content_file'} = $params{file} if $params{file}; | ||
| 147 | |||
| 148 | my $response = $self->service->user_agent->get($url, %header); | ||
| 149 | |||
| 150 | if ($response->is_success) { | ||
| 151 | return $response->content; | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | croak $response->status_line; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | package Net::Google::PicasaWeb::Media::Content; | ||
| 159 | use Moose; | ||
| 160 | |||
| 161 | =head1 MEDIA CONTENT | ||
| 162 | |||
| 163 | The object returned from the L</content> accessor is an object with the following accessors and methods. | ||
| 164 | |||
| 165 | =head2 ATTRIBUTES | ||
| 166 | |||
| 167 | =head3 media | ||
| 168 | |||
| 169 | This is the parent L<Net::Google::PicasaWeb::Media> object. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | has media => ( | ||
| 174 | is => 'rw', | ||
| 175 | isa => 'Net::Google::PicasaWeb::Media', | ||
| 176 | required => 1, | ||
| 177 | weak_ref => 1, | ||
| 178 | ); | ||
| 179 | |||
| 180 | =head3 url | ||
| 181 | |||
| 182 | This is the URL where the photo or video may be downloaded from. | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | has url => ( | ||
| 187 | is => 'rw', | ||
| 188 | isa => 'Str', | ||
| 189 | ); | ||
| 190 | |||
| 191 | =head3 mime_type | ||
| 192 | |||
| 193 | This is the MIME-Type of the photo or video. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | has mime_type => ( | ||
| 198 | is => 'rw', | ||
| 199 | isa => 'Str', | ||
| 200 | ); | ||
| 201 | |||
| 202 | =head3 medium | ||
| 203 | |||
| 204 | This should be one of the following scalar values describing the media entry: | ||
| 205 | |||
| 206 | =over | ||
| 207 | |||
| 208 | =item * | ||
| 209 | |||
| 210 | image | ||
| 211 | |||
| 212 | =item * | ||
| 213 | |||
| 214 | video | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | has medium => ( | ||
| 221 | is => 'rw', | ||
| 222 | isa => 'Str', | ||
| 223 | ); | ||
| 224 | |||
| 225 | =head1 METHODS | ||
| 226 | |||
| 227 | =head2 fetch | ||
| 228 | |||
| 229 | my $data = $content->fetch(%params); | ||
| 230 | |||
| 231 | Fetches the image or video from Picasa Web. By default, this method returns the file data as a scalar. | ||
| 232 | |||
| 233 | This method accepts the following parameters, which modify this behavior: | ||
| 234 | |||
| 235 | =over | ||
| 236 | |||
| 237 | =item file | ||
| 238 | |||
| 239 | If given, the data will not be returned, but saved to the named file instead. | ||
| 240 | |||
| 241 | =back | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub fetch { | ||
| 246 | my $self = shift; | ||
| 247 | return $self->media->_fetch($self, @_); | ||
| 248 | } | ||
| 249 | |||
| 250 | package Net::Google::PicasaWeb::Media::Thumbnail; | ||
| 251 | use Moose; | ||
| 252 | |||
| 253 | =head1 THUMBNAILS | ||
| 254 | |||
| 255 | Each thumbnail returned represents an individual image resource used as a thumbnail for the main item. Each one has the following attributes and methods. | ||
| 256 | |||
| 257 | =head2 ATTRIBUTES | ||
| 258 | |||
| 259 | =head3 media | ||
| 260 | |||
| 261 | This is the parent L<Net::Google::PicasaWeb::Media> object. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | has media => ( | ||
| 266 | is => 'rw', | ||
| 267 | isa => 'Net::Google::PicasaWeb::Media', | ||
| 268 | required => 1, | ||
| 269 | weak_ref => 1, | ||
| 270 | ); | ||
| 271 | |||
| 272 | =head3 url | ||
| 273 | |||
| 274 | This is the URL where the thumbnail image can be pulled down from. | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | has url => ( | ||
| 279 | is => 'rw', | ||
| 280 | isa => 'Str', | ||
| 281 | ); | ||
| 282 | |||
| 283 | =head3 width | ||
| 284 | |||
| 285 | This is the pixel width of the image. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | has width => ( | ||
| 290 | is => 'rw', | ||
| 291 | isa => 'Int', | ||
| 292 | ); | ||
| 293 | |||
| 294 | =head3 height | ||
| 295 | |||
| 296 | This is the pixel height of the image. | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | has height => ( | ||
| 301 | is => 'rw', | ||
| 302 | isa => 'Int', | ||
| 303 | ); | ||
| 304 | |||
| 305 | =head1 METHODS | ||
| 306 | |||
| 307 | =head2 fetch | ||
| 308 | |||
| 309 | my $data = $thumbnail->fetch(%params); | ||
| 310 | |||
| 311 | Fetches the thumbnail image from Picasa Web. By default, this method returns the image data as a scalar. | ||
| 312 | |||
| 313 | This method accepts the following parameters, which modify this behavior: | ||
| 314 | |||
| 315 | =over | ||
| 316 | |||
| 317 | =item file | ||
| 318 | |||
| 319 | If given, the data will not be returned, but saved to the named file instead. | ||
| 320 | |||
| 321 | =back | ||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | sub fetch { | ||
| 326 | my $self = shift; | ||
| 327 | return $self->media->_fetch($self, @_); | ||
| 328 | } | ||
| 329 | |||
| 330 | =head1 AUTHOR | ||
| 331 | |||
| 332 | Andrew Sterling Hanenkamp, C<< <hanenkamp at cpan.org> >> | ||
| 333 | |||
| 334 | =head1 COPYRIGHT & LICENSE | ||
| 335 | |||
| 336 | Copyright 2008 Andrew Sterling Hanenkamp | ||
| 337 | |||
| 338 | This program is free software; you can redistribute it and/or modify it | ||
| 339 | under the same terms as Perl itself. | ||
| 340 | |||
| 341 | =cut | ||
| 342 | |||
| 343 | 1; |