# Author: Dean Stringer # Original Release Date: 27/Apr/2006 # ----------------------------------------------------------------------------------------------- # Description: # Takes a search 'tag' and uses that to lookup photos at flickr that are # tagged with that word, randomly chooses one of the resulting photos and # wraps in some HTML output that includes a link of a thumnail copy of the # photo to the full flickr page display of it # ----------------------------------------------------------------------------------------------- # attributes and sample values returned from a photo.search # id = 111258336 # owner = 12345678@ABC # secret = 63bfdeb7b9 # server = 46 # title = Honda Fireblade # ispublic = 1 # isfriend = 0 # id = 111258336 # # for full API details and to apply for a key see: http://www.flickr.com/services/ # ----------------------------------------------------------------------------------------------- use strict; use CGI; use Flickr::API; use Flickr::API::Request; my $q = new CGI; my $tag = $q->param('tag'); print $q->header; my $api = new Flickr::API({'key' => 'YOUR-FLICKR-KEY-GOES-HERE'}); my $request = new Flickr::API::Request({ 'method' => 'flickr.photos.search', 'args' => { #'user_id' => '12345678@ABC', # e.g. from a particular users collection only 'tags' => "$tag", 'per_page' => '20' }, }); our %photoData; my $response = $api->execute_request($request); if ($response->{success}) { my $responseParser = new XML::Parser::Lite; $responseParser->setHandlers( Start => \&startTag, End => sub { shift; print "\n" }, ); $responseParser->parse($response->{_content}); my @photoIds = (keys %photoData); if (scalar @photoIds) { # may not have been any matching pics my $randRef = int(rand(scalar @photoIds)) + 1; my %photo = %{$photoData{$photoIds[$randRef]}}; my $photoURL = "http://static.flickr.com/" . $photo{server} . "/" . $photo{id} . "_" . $photo{secret} . "_m.jpg"; my $photoOwnerURL = "http://flickr.com/photos/" . $photo{owner} . "/" . $photo{id} . "/"; print " " . ""; } } sub startTag { my $p = shift; my $tagName = shift; my @args = @_; # the tag's attributes if ($tagName == 'photo') { my %thisPhoto; my $argCount = (scalar @args) / 2; for (my $i = 1; $i < $argCount; $i++) { my $attName = $args[($i*2) - 2]; my $attValue = $args[($i*2) - 1]; $thisPhoto{$attName} = $attValue; } if (defined $thisPhoto{'id'}) { $photoData{$thisPhoto{'id'}} = \%thisPhoto; } } }