Over the next day or two I will be extending one or two Flickr API libraries with the goal of adding support for Collections in a user's photostream.
I can't find any information as to when Flickr is going to support collections in their API themselves. If anyone wants to save me a few hours of work, let me know. I'd be happy to not have to do the work myself. Sofar I'm expecting to have to pull out my regex reference book to give you a clue as to how it'll happen.
CURLing for Collections
I have a client who requested I build a custom front end to their Flickr account. She relies heavily on the collections feature of Flickr to organize her many many photosets. As we were trying to figure out how she can organize her account so I can quicklly parse the data returned by the Flickr API, I came up with a few proposed solutions. This is one of them, and though now what we went with, I decided to share.
CODE:
-
<?php
-
function getLinks($url){
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, $url);
-
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
-
$data = curl_exec ($ch);
-
curl_close ($ch);
-
$doc = new DOMDocument;
-
// We don't want to bother with white spaces
-
$doc->preserveWhiteSpace = false;
-
$doc->loadHTML($data);
-
$items = $doc->getElementsByTagName('a');
-
return $items;
-
}
-
-
-
function listLinks($recursive,$id = ""){
-
$url = "http://www.flickr.com/photos/".$_SESSION['username']."/collections/".$id;
-
-
$items = getLinks($url);
-
echo "<ul>";
-
for ($i = 0; $i <$items->length; $i++) {
-
if ($items->item($i)->getAttribute('class') == "Colla"){
-
$thisCollectionID = split("/",$items->item($i)->getAttribute("href"));
-
echo "<li>" . $items->item($i)->nodeValue . ': ' . $thisCollectionID[4];
-
if ($recursive == true){
-
listLinks($recursive,$thisCollectionID[4]);
-
}
-
echo "</li>";
-
} elseif ($items->item($i)->getAttribute('class') == "Seta"){
-
$thisSetID = split("/",$items->item($i)->getAttribute("href"));
-
echo "<li>" . $items->item($i)->nodeValue . ': ' . $thisSetID[4];
-
echo "</li>";
-
}
-
-
}
-
echo "</ul>";
-
}
-
?>
Listing all of the photosets recursively
To loop through all collections and collections of collections recursively we simply call listLinks(false);. The execution time on the server was about ~3 seconds, which is unacceptably slow for realtime requests.
In order to just list the photosets that are inside of the top level collections, which takes about half the time (about 1-1.5 seconds), but is still unacceptable, you just set the parameter to false like so listLinks(false);. Simple as that.
You may have noticed that these are global functions. The Flickr API php library that I used for this project was actually not written in PHP5, and since this was just a simple proof of concept to talk the client out of using Collections, and instead a photoset naming convention, to organize the photos that were going to be made available on her site.
Using collections in the real world
Though 3 seconds of processing time would be horrible if your code was making requests all the time, using CURL to pull this data in to your server once in awhile and storing the results in a database doesn't seem too bad. An acceptable approach to working around the lack of support in the API is better than nothing at all. Right?