In this section, I’ll return to an idea I first wrote about in Chapter 3—demonstrating how you can create a mashup using the del.icio.us API to enhance the functionality of Flickr. There are many ways to organize the photos that you own in Flickr: you can tag them, put them in sets and collections, and send them to specific groups. With photos that belong to others, you have a lot fewer options. Generally, you’re limited to making a photo a favorite. (If a photo has been placed into a group pool, you as a member of that group are able to tag the photo.) Moreover, you can’t make any groupings of photos within Flickr that contain both your own photos and those of others. You can’t “favorite” your own photos, and you can’t place other users’ photos in your sets. (You can say that group pools are an exception, but the owner of the photo has to send the photo to the pool.)
You can use del.icio.us to increase your ability to annotate Flickr photos and to intermix your own photos with those of others. A nice supporting feature of del.icio.us is that Flickr photos that are bookmarked in del.icio.us are shown with a thumbnail of the image—making deli.cio.us a simple photo display mechanism.
To use del.icio.us to track and annotate Flickr photos, you have to bookmark the photo. For bookmarking individual photos, you can simply use the same del.icio.us bookmarklets you would use to bookmark any other URL. However, if you have a large number of Flickr photos to manage with del.icio.us, it’s more convenient to programmatically bookmark the photos.
Here I demonstrate how to send your Flickr favorites into del.icio.us so that you set your own tags and descriptions for the photos. The following code uses phpFlickr (see Chapter 6) and PHPDelicious:
<?php # This PHP script pushes a Flickr user's favorites into a del.icio.us account # a function for appending two input strings separated by a comma. function aconcat ($v, $w) { return $v . "," . $w; } # read in passwords for Flickr, the MySQL cache for phpFlickr, and del.icio.us include("flickr_key.php"); include("mysql_cred.php"); include("delicious.cred.php"); # use phpFlickr with caching require_once("phpFlickr/phpFlickr.php"); $api = new phpFlickr(API_KEY, API_SECRET); $db_string = "mysql://" . DB_USER . ":" . DB_PASSWORD. "@" . DB_SERVER . "/". DB_NAME; $api->enableCache( "db", $db_string, 10 ); # instantiate a del.icio.us object via the phpDelicious library require_once('php-delicious/php-delicious.inc.php'); $del_obj = new PhpDelicious(DELICIOUS_USER, DELICIOUS_PASSWORD); $username = 'Raymond Yee'; if ($user_id = $api->people_findByUsername($username)) { $user_id = $user_id['id']; } else { print 'error on looking up $username'; exit(); } #print $user_id; # get a list of the user's favorites (public ones first) # http://www.flickr.com/services/api/flickr.favorites.getPublicList.html # allow a maximum number of photos to be copied over -- useful for testing. $maxcount = 2; $count = 0; # set the page size and the page number to start with $per_page = 500; $page = 1; # loop over the pages of photos and the photos within each page do { if (!$photos = $api->favorites_getPublicList($user_id,"owner_name,last_update,tags", $per_page, $page)) { echo "Problem in favorites_getPublicList call: ", $api->getErrorCode(), " ", $api->getErrorMsg(); exit(); } $max_page = $photos['pages']; foreach ($photos['photo'] as $photo) { $count += 1; if ($count > $maxcount) { break; } echo $photo['id'], "\n"; # Map Flickr metadata to del.icio.us fields # use the URL of the context page as the del.icio.us URL $sUrl = "http://www.flickr.com/photos/$photo[owner]/$photo[id]/"; # copy the photo title as the del.icio.us description $sDescription = $photo['title']. " (on Flickr)"; # set del.icio.us note to empty $sNotes = ''; # use the default date of now. $sDate = ''; # replace previous del.icio.us posts with this URL $bReplace = true; # copy over the tags and add FlickrFavorite $aTags = split(' ', $photo['tags']); $aTags[] = 'FlickrFavorite'; echo $sUrl, $sDescription, " ", $sNotes, " ", array_reduce($aTags, "aconcat") , " ", $sDate, " ", $bReplace; print "\n"; if ($del_obj->AddPost($sUrl, $sDescription, $sNotes, $aTags, $sDate, $bReplace)) { print "added $sUrl successfully\n"; } else { print "problem in adding $sUrl\n"; } } // foreach $page += 1; } while (($page <= $max_page) && ($count <= $maxcount)) // do ?>
When you run this script, you will see something akin to Figure 14-1.
Figure 14.1. Figure 14-1. Mashup of Flickr favorites and deli.cio.us. (Reproduced with permission of Yahoo! Inc. ® 2007 by Yahoo! Inc. YAHOO! and the YAHOO! logo are trademarks of Yahoo! Inc.)
Once you have your Flickr favorites in del.icio.us, you can use all the del.ico.us functionality to manipulate them; for example, you can edit the tags (which have been copied over from Flickr), title, and description of a photo—something you couldn’t do directly to the Flickr photos you don’t own. In addition, you can use tags to group pictures—whether they are your own or someone else’s.
These are some ways in which you can expand the functionality of the script presented here:
You can use the del.icio.us API to help you gather your groups of Flickr photos by their del.icio.us tags and present them as slide shows.
You can change the script to push all of a user’s favorites instead of just the public favorites.
In addition to favorites, you can add any individual photo or group of your own photos or any arbitrary Flickr aggregation of photos to del.icio.us.
You can keep favorites synchronized between Flickr and del.icio.us.