Permalink
Browse files

Support retrieving photos by ID, and more image sizes.

  • Loading branch information...
1 parent cb794a0 commit 8214d5e88f9b84de3c462b223832ce12776e449a @TJC committed Oct 13, 2011
Showing with 101 additions and 6 deletions.
  1. +5 −0 lib/Flickr/API2.pm
  2. +63 −5 lib/Flickr/API2/Photo.pm
  3. +23 −1 lib/Flickr/API2/Photos.pm
  4. +10 −0 t/flickr-api.t
View
@@ -83,6 +83,11 @@ Flickr::API2 - Perl interface to the Flickr API
say "Title is " . $photo->title;
}
+ Individual photos can be retrieved by id like so:
+
+ my $p = $api->photos->by_id(1122334455);
+ say "Medium JPEG is " . $p->url_m;
+
To access the raw flickr API, use methods like:
my $response = $api->execute_method('flickr.test.echo', {
@@ -38,18 +38,45 @@ has 'date_upload' => ( is => 'rw' );
has 'date_taken' => ( is => 'rw' );
has 'owner_id' => ( is => 'rw' );
has 'owner_name' => ( is => 'rw' );
-has 'url_s' => ( is => 'rw' );
-has 'url_m' => ( is => 'rw' );
-has 'url_l' => ( is => 'rw' );
-has 'url_o' => ( is => 'rw' );
+has 'url_sq' => (
+ is => 'rw',
+ lazy => 1,
+ default => sub { $_[0]->populate_size_urls; $_[0]->url_sq; },
+);
+has 'url_t' => (
+ is => 'rw',
+ lazy => 1,
+ default => sub { $_[0]->populate_size_urls; $_[0]->url_t; },
+);
+has 'url_s' => (
+ is => 'rw',
+ lazy => 1,
+ default => sub { $_[0]->populate_size_urls; $_[0]->url_s; },
+);
+has 'url_m' => (
+ is => 'rw',
+ lazy => 1,
+ default => sub { $_[0]->populate_size_urls; $_[0]->url_m; },
+);
+has 'url_l' => (
+ is => 'rw',
+ lazy => 1,
+ default => sub { $_[0]->populate_size_urls; $_[0]->url_l; },
+);
+has 'url_o' => (
+ is => 'rw',
+ lazy => 1,
+ default => sub { $_[0]->populate_size_urls; $_[0]->url_o; },
+);
has 'description' => ( is => 'rw' );
has 'path_alias' => ( is => 'rw' );
=head1 METHODS
=head2 info
-Returns getInfo results for this photo; not really tested much yet.
+Returns getInfo results for this photo; contains lots of info that isn't
+currently available by simple accessors.
=cut
@@ -68,6 +95,10 @@ sub info {
Returns getSizes results for this photo.
+Note that you probably don't need to call this directly - instead, see the
+various url accessors. If they're blank, try calling populate_size_urls()
+first.
+
=cut
sub sizes {
@@ -81,6 +112,33 @@ sub sizes {
return $response->{sizes};
}
+=head2 populate_size_urls
+
+Populates all the various URLs to the differently-sized images by querying
+the API. This *should* have been done automatically for you, the first time
+you queried one of the url accessors.
+
+=cut
+
+our %label_to_accessor = (
+ Square => 'url_sq',
+ Thumbnail => 'url_t',
+ Small => 'url_s',
+ Medium => 'url_m',
+ Large => 'url_l',
+ Original => 'url_o',
+);
+
+sub populate_size_urls {
+ my $self = shift;
+ my $sizes = $self->sizes->{size};
+ for my $s (@$sizes) {
+ if (my $acc = $label_to_accessor{$s->{label}}) {
+ $self->$acc($s->{source});
+ }
+ }
+}
+
=head2 page_url
Returns the URL for this photo's page on Flickr.
@@ -1,5 +1,6 @@
package Flickr::API2::Photos;
use Mouse;
+use Flickr::API2::Photo;
extends 'Flickr::API2::Base';
=head1 NAME
@@ -8,10 +9,31 @@ Flickr::API2::Photos
=head1 SYNOPSIS
-See search() below.
+See search() and by_id() methods below.
=head1 METHODS
+=head2 by_id
+
+Finds one photo by its id number.
+
+eg. say $api->photos->by_id(3386874895)->title;
+
+=cut
+
+sub by_id {
+ my ($self, $id) = @_;
+ my $p = Flickr::API2::Photo->new( api => $self->api, id => $id );
+ my $info = $p->info->{photo};
+ $p->title($info->{title}{_content});
+ $p->description($info->{description}{_content});
+ $p->owner_name($info->{owner}{realname});
+ $p->owner_id($info->{owner}{nsid});
+ $p->path_alias($info->{owner}{username});
+
+ return $p;
+}
+
=head2 search
Search for photos, for eg:
View
@@ -83,4 +83,14 @@ throws_ok {
};
}
+# Check photo-by-id methods
+{
+ my $photo = $api->photos->by_id(3386874895);
+
+ ok($photo->id, "photo has an id");
+ ok($photo->page_url, "photo has a generated page url");
+ ok($photo->title, "photo has a title");
+ ok($photo->url_m, "photo has a medium sized url");
+}
+
done_testing();

0 comments on commit 8214d5e

Please sign in to comment.