Searching the Flickr API and iterators

Posted on August 7, 2005

I’ve been slacking off on the Flickr-Tools project, mainly because it serves all my purposes as it is right now.

But then someone pinged me regarding the photos method in the Flickr::Person class (the one that will return all the photos for the user) and that was the incentive I needed to finally get to writing that method.

So now I remember why I left it half-way through…

The thing is that this method must use the flickr.photos.search method to get it’s info and it could return a lot of info.

So flickr.photos.search has a mechanism for managing the potentially huge volume of information –it forces you to break up your request into “pages” of results– which, of course, is the right way to do it.

But how do I map that onto my Flickr::Person class in a nice way? I surely don’t want to make the user have to control things like results per page and current page and so on, that would defeat the purpose of the whole thing, but I sure as hell can’t just go and fetch everything from Flickr because for a user with many photos I would blow up the computer’s memory fast!

So after thinking about it for something like 10 seconds I knew that the right way to do it would be to use an iterator. Instead of returning an array of Flickr::Photo objects I should return a Flickr::Photogroup object (or somesuch) and it should have a few simple methods like next, previous and total. You know, a classic iterator. When it ran out of results it should then invoque the Flickr::API::Photos::search method to get the next batch of photos.

OK, that is a bit more work than I bargained for but it is the right way to do it.

But then all of the other methods which I’ve already written and which return groups of things (mostly photos) should also return this kind of object. And each instance should know how to get more results when it needs it, so it should be rather smart about what it is representing.

This means two things:

  • I have to rewrite the interface for methods which are already out there (major turn-off)
  • I have a lot more work ahead of me in order to do it
  • So I will make this re-write and, unfortunately, I probably won’t be able to have it done by tomorrow as I told the poor soul who is expecting a new version of Flickr-Tools with the new method and, probably, no interface changes.

    Should have though of that before, now shouldn’t I? Duh!

    Technorati Tags: ,