24 Feb, 2009

Published at 11:36PM

Tagged with api, flickr, javascript, jquery, plugin, programming, and tips

This post has 9 comments

(Some of) the Flickr API in jQuery

And by some of I mean 4 methods (at the time of this writing, at least). However, the use case for this little plugin is for sites like this one, where you may just want to share a few of your recent photos.

I recently had to redo a site that dealt with Flickr (no, not this one). So I was on the hunt for some sort of Flickr library for jQuery. Much to my surprise, there weren’t too many. The one’s that I did find just weren’t going to cut it, so that gave me an excuse to write my own.

You can find it on GitHub. See examples.html for, well, examples. Once you add your API key, you should see something like this:

It’s pretty simple to use, and rather than explain it, I’ll just show you.

1
2
3
4
5
$('#photos').flickr({
  api_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  thumbnail_size: 'sq',
  link_to_size: 'm'
}).photosGetRecent().addClass('thumbnails')

Now, a few things to mention, one being the available sizes and what they mean. Firstly, the thumbnail_size is the size of what you’ll actually see, and the link_to_size is the size for what the image links to (useful for lightbox stuff). You can set the link_to_size to ‘flickr’ if you want the thumbnails to link directly back to the image on Flickr’s site. Now, on to the available sizes:

'sq' // => square
't'  // => thumbnail
's'  // => small
'm'  // => medium (default for @link_to_size@)

Now onto the cooler stuff.

The jQuery object that first receives the flickr() call is what will get filled with the resultant images. The flickr() method accepts some configuration (api_key is required), then returns an object housing all of the available API methods, allowing you to chain the API call. The cool thing is, each of the implemented API methods return the original jQuery object, so you can continue processing it as you normally would. In the above example, $('#photos') is returned after the photosGetRecent() call. Maybe this will help show you why this is important:

1
2
3
$('#photos').flickr({
  api_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}).photosGetRecent().find('a').facebox()

I originally thought it would be good just go ahead and return the inserted collection of images, but there are cases where a user may want to modify the list items or the links or the images, separately, and that’s just not my place to decide :-)

Oh, and just a heads up. You can set the defaults for the flickr() method in the javascript itself (somewhere near the end). Then your calls become:

1
$('#photos').flickr().photosGetRecent().find('a').facebox()

A little bit nicer. And in case it’s not obvious, options that you pass to the flickr() method will override those defaults in the javascript.

Each of the available API methods support all of the options located in the Flickr API documentation. And here’s a quick list of the supported methods:

  • photosGetRecent()
  • photosGetContactsPublicPhotos()
  • photosSearch() (by far the most powerful)
  • photosetsGetPhotos()

I’ll soon be migrating my current Flickr importing to use javascript, and specifically, this plugin. Remember, the source is on GitHub if you’re interested.

Thoughts are welcome. Enjoy!

Comments

Chris Wednesday, 25 Feb, 2009 Posted at 10:15AM

Are you worried about having the API key in the source like that?

Ryan Wednesday, 25 Feb, 2009 Posted at 11:00AM

No, not really. Anything destructive requires authentication, anyway, so the worst that can happen is someone either cranks up the usage of the key or does something that violates Flickr terms.

The original need for this was directed by someone else, I just happen to know javascript a little better than they do, so I wrote it for them :-) I advised them to create a second key for use with this script, just in case.

I’ll admit, a better solution would be to use the feeds, but that’s not what they wanted. I’ll have to do a one-to-one mapping to see how the feeds map up to the methods they need, and maybe I can just use the feeds under the covers, eliminating the key requirement.

Ryan Wednesday, 25 Feb, 2009 Posted at 12:45PM

For those interested, I have a basic implementation working with public feeds in a separate branch on GitHub.

If no API key is supplied, the methods will default to use the public feeds. Note: obviously, the feeds are extremely limited to what you can do, and the options are completely different. If you go this route, you can find the available options here.

Just a heads up, I doubt this will ever get merged into master. The mix of the two is too messy (especially since the parameters are different). However, it may be worth writing a separate library that uses feeds only. We’ll see.

baa Saturday, 20 Jun, 2009 Posted at 09:54AM

web development company Wednesday, 08 Jul, 2009 Posted at 09:00AM

jQuery was launched by “John Resig” at BarCamp, New York City in the start of 2006. jQuery is an open source (free to use) Java Script library and plays a role of “bridge”, centering HTML and JavaScript. It means it works as an essential tool at multiple places while designing or developing a web page. jQuery is one of the most emerging tools which are used in modern web design and development trends. More ever the time to master the knowledge of using jQuery is very least.

tucson website design Thursday, 08 Oct, 2009 Posted at 02:28PM

Flickr is great. Makes me wonder why Google choose to buy Picassa over Flickr. Google usually identifies the best tech company and buys them. There is no question in my mind that Flickr is better than Picassa. Looks like Yahoo! actually beat Google in this instance.

Ian Friday, 11 Dec, 2009 Posted at 03:58AM

I’m trying to get this to work with fancybox. I applied my class directly before the href in the js file but it doesn’t seem to be working. Been at this thing for awhile trying to figure it out. Any help would be tremendous. Thanks in advance.

Ryan Friday, 11 Dec, 2009 Posted at 09:57AM

Hmm… Well, you should just be able to do something like this:

1
$('#photos').flickr({ ... }).photosGetRecent().find('a').fancybox()

However, that might not work just because the javascript (ie find('a')) may execute before the images/links are present. So, another option might be to look into the jQuery#live() method, which might look something like this:

1
2
3
4
5
$(function() {
  $('#photos a').live('click', function() {
    $(this).fancybox()
  });
});

The live() function works for existing elements on the page, as well as any future elements that were added to the DOM later (ie your Flickr photos). I may add some hooks into this plugin that could be bound/triggered from javascript outside of the plugin.

If the first suggestion didn’t work, I’d try using live(). See the documentation for details. Hope that helps!

Wayne Tuesday, 26 Jan, 2010 Posted at 02:22AM

This is great, but there is one more size you can take advantage of:

Line 34 add:

case ‘l’ : return ‘_b’ // large

:)

Do you have something to say about this post?
Retype the image to the right Spam Hint: Are You Human? Textile Formatting Tips

or

Ryan Heath | Site Management A Ruby on Rails production.

This site is a Formed Function. Formed Function LLC | @formedfunction | Get in Touch