Close and Go BackBack to Viget

Pulling Your Flickr Feed with jQuery

Keith Muth
Keith Muth, ON THE TOPIC OF Development and Favorites and Javascript
May12 25

Feeds are the easiest way to view updated content, whether it's through a feed reader or outputted onto a web site. There are many different types of feeds, such as RSS or Atom, and many different ways display them on your site, such as using MagpieRSS to parse an RSS feed in PHP. However, you can also display feeds on your site using JavaScript, so in this post I'm going to be talking about a feed format called JSON and how you can use JavaScript to parse it out and display it.

JSON (JavaScript Object Notation) is a data format that is easy to read and language-independent, meaning you can parse it using any programming language. Both Yahoo! and Google have been offering data from their sites in JSON format for the past couple years. A good example of this is Flickr. Anyone with a Flickr account can access a JSON feed of their photos.

Finding Your Feed

If we go to the Viget Inspire collection on Flickr, we can click on the feed (orange button, bottom of the page) and bring up a RSS 2.0 feed of all the images in our pool. Flickr's API has many other feed formats, so I suggest going to their site to read up on it because there are a lot of things you can do. If you want the JSON version of the feed, change "format=rss_200" at the end of the query string to "format=json" so that your URL looks like this:

http://api.flickr.com/services/feeds/groups_pool.gne? id=675729@N22&lang=en-us&format=json

Bringing It Into jQuery

So now that you have your JSON feed, lets put it to good use. My co-workers and I are big fans of the JavaScript framework jQuery. With version 1.2, jQuery added support for transferring JSON data across multiple domains (this is referred to as JSONP), so in this example we'll be using it to do all the JavaScript work for us. First off, make sure you've downloaded the latest version of jQuery and added it to your page. Next, add "jsoncallback=?" to the end of your query string (this is the callback name) and put the code inside <script> tags to get things running:

$.getJSON("http://api.flickr.com/services/feeds/groups_pool .gne?id=675729@N22&lang=en-us&format=json&jsoncallback=?", function(data){
  $.each(data.items, function(i,item){
    $("<img/>").attr("src", item.media.m).appendTo("#images")
      .wrap("<a href='" + item.link + "'></a>");
  });
});

See how that function has appendTo("#images")? The JavaScript will be looking for a div with an id of "images" to pull in all the images coming in through the feed, and then wrapping them in a link to the images on Flickr. If you try it out you'll notice it just displays all the images in a row. Obviously this doesn't look very good, so you can use CSS and jQuery to display things nicely. I decided to use the jQuery cycle plugin which has numerous cool effects. Download the plugin and make sure you include a link to it in your page. So at this point your HTML should look something like this:

<div id="images"></div>  
<div class="flickrNav">
    <a id="prev" href="#">Prev</a><a id="next" href="#">Next</a>
</div>

and the CSS could be something like this:

#images { height: 185px; width: 240px; padding:0; margin:0; overflow: hidden;}
#images img { border:none;}

Then we'll add in the cycle plugin code inside our initial function that creates the images out of the JSON feed:

$.getJSON("http://api.flickr.com/services/feeds/groups_pool .gne?id=675729@N22&lang=en-us&format=json&jsoncallback=?", function(data){
  $.each(data.items, function(i,item){
    $("<img/>").attr("src", item.media.m).appendTo("#images")
      .wrap("<a href='" + item.link + "'></a>");
  });
  $('#images').cycle({
    fx:     'fade',
    speed:    'fast',
    timeout:  0,
    next:   '#next',
    prev:   '#prev'
  });
});

Notice that I put some options in the cycle method for previous and next id's. As you can probably guess, these links will allow the user to navigate through the list of images.

Getting More Than Just Pretty Pictures

At this point images should be showing up, but that JSON feed has a lot of other information in it you can pull in. For any attribute you want to display you just need to write it in the format of $("div name you are targeting").html(name of JSON object you want to display). In our example, if we wanted to show the title of the photo pool, the description of the pool, and a link to the pool on Flickr we would write the following into our jQuery function:

$.getJSON("http://api.flickr.com/services/feeds/groups_pool .gne?id=675729@N22&lang=en-us&format=json&jsoncallback=?", function(data){
  $.each(data.items, function(i,item){
    $("<img/>").attr("src", item.media.m).appendTo("#images")
      .wrap("<a href='" + item.link + "'></a>");
  });

  $("#title").html(data.title);
  $("#description").html(data.description);
  $("#link").html("<a href='"+data.link+"' target=\"_blank\">Visit the Viget Inspiration Pool!</a>");
    //Notice that the object here is "data" because that information sits outside of "items" in the JSON feed

  $('#images').cycle({
    fx:     'fade',
    speed:    'normal',
    timeout:  0,
    next:   '#next',
    prev:   '#prev'
  });
});

Notice that we have to call items based on where they are located in the JSON feed. For example, "item.link" will link to the image but "data.link" will link to the overall photo pool. You can look at the actual JSON feed and see how its organized. Next add the HTML so jQuery has something to update:

<h1 id="title"></h1> //Title of the Flickr pool
<p id="description"></p> //Description of the images pool
<p id="link"></p> //Link to the images pool
<div id="images"></div> //Container for the images
<div id="flickrNav"> //Navigation for the images
    <a id="prev" href="#">Prev</a><a id="next" href="#">Next</a>
</div>

Now you've got yourself a nice feed of Flickr images and can all the CSS you want to make it look more stylish:

 

 

 

 

Everything in the box above is being pulled directly from Flickr; pretty cool right? Sure, you could easily make a Flickr badge to display your photos, but this method gives you more control over how it display your data, especially when using a framework like jQuery. There are many other ways to display the content of a feed on a web site, so please share any other methods you might use because I'm really interested to learn more about this stuff.

Why Does Flickr Do That?

On a side note, has anyone else noticed that Flickr puts in extra markup for the description of individual photos in a feed? Each photo description has "(username) posted a photo:" with HTML tags that don't have any class or id names (which makes styling with CSS more difficult). Sure you can use JavaScript to style it or regular expressions to take it out, but I don't really think a feed should be forcing the user to deal with markup like that. I would understand if it was in the feed for the free version of Flickr since it could be promotion for the site, but it exists for paying members too. As far as I know, this is in all feeds that Flickr offers through their API, but I could be wrong. If anyone has any insight into this, please let me know.

Gabe said on 05/13 at 09:16 AM

Great walk through, I’m excited to see what other services I can use this technique on.  I hadn’t heard of the jQuery cycle plug in either!

Mindy said on 05/13 at 10:36 AM

Nice - there are plenty of little scripts available, but being able to fully control and style it with jQuery will come in handy. Thanks for the walk-through.

Philip Morton said on 05/13 at 11:33 AM

Excellent tutorial, thanks!

Frank said on 05/13 at 01:21 PM

I can’t seem to get this to work!

Keith Muth said on 05/13 at 01:57 PM

Frank, if you copy and paste the code I put inside the post, there is probably a space at the end of the JSON URL from the Flick API (after “groups_pool") so that it breaks to the next line inside the box of code. A space in the URL wil keep it from working. The best idea would to look at the source code in my demo and go from there. Let me know if you have any more problems with it.

Dave said on 05/13 at 02:14 PM

Great post! I look forward to trying it out.

Benjamin Sterling said on 05/13 at 11:14 PM

Great post, here is a plugin that will make it easier to pull in your flickr account.

Benjamin Sterling said on 05/13 at 11:17 PM

Darn… hit submit too quickly… Also wanted to add that you have come a long way Keith, glad to see you stuck with jQuery.

ps. can you fix my previous post to shorten the url.

Drupal Museum said on 05/14 at 03:17 AM

Thanks for the tutorial!

haiming webhosting reviews said on 05/16 at 09:44 PM

Yes!Thinks! like jquery and Flickr

Web Designer Glasgow said on 05/18 at 06:34 AM

Keith, what do you think the relative merits of jQuery are in comparison to mootools?

austin web design said on 05/20 at 10:20 AM

Is it possible to have the link go to a thickbox overlay of the large version of the photo instead of to the flickr page?  That would definitely make this idea even better.

Keith Muth said on 05/20 at 12:26 PM

It is possible to pull the larger version of the image, but you would just have to change your link. The JSON feed only pulls in a medium sized images but if you remove the “_m” at the end of the image (with regular expressions) it should pull a larger version and you could add a thickbox/lightbox technique.

Ben Duncan said on 05/27 at 11:43 AM

Nice, I had never heard of the cycle plugin, and you show a damn clean way of parsing JSON with jQuery.

Anna said on 06/04 at 09:49 PM

This is an awesome tutorial, thanks. I also want to use larger images, and have found where the “_m” is located, but having only used jQuery once - how do you actually remove that so the larger version is pulled? Thanks again.

Keith Muth said on 06/05 at 09:31 AM

Anna, good question, and something I wanted to talk about in my original post. Obviously you can only work with Flickr gives you in their feed. As you pointed out, the feed has “_m” for a medium size image. You could use a regular expression to change the “_m” to “_t” (for a thumbnail) or “_b” (for a larger size). If you don’t feel like messing with regular expressions, you can use Flickr’s JSON response through their API, which means you need an API Key (free from Flickr...if you ask nicely). They have a write up on their site about it:
http://www.flickr.com/services/api/response.json.html

You can use Flickr’s jsonFlickrApi() function to run through your feed. You’ll also need to put in a JavaScript link to your feed right below that function so it knows where to get the images. I was going to put up an example here in the comments section, but it didn’t look very good so I decided to take it out. There are examples out there if you want to see it in action, or maybe I should just put it up as an addition to my post…

Anyway, let me know if you make something cool, I love seeing new ways to use Flickr feeds.

Doug C. said on 06/13 at 03:17 PM

Wow, would I love to be able to do that (lol). I’ve been searching high and low for a way to “tweak” my Flickr group’s code so I could get the photos to appear in two columns inside a sidebar content box that I created myself. However, I am a complete n00b when it comes to coding. It took me a couple weeks just to figure out how to make the content boxes.

By the way, I love your header artwork on the page. Beautiful stuff.

tim said on 06/28 at 05:35 AM

How can i display more than 20 results. The Rss only ever seems to contain that many. What is i wanted a whole set of say 60 images.

christopher said on 06/30 at 05:09 AM

This is a really great, thank you very much :-) !

Rey Bango said on 07/14 at 04:28 PM

Hi Keith, I posted your tutorial to the jQuery mailing list. We’d love to see more posted there so if you could join us on the mailing list, that would be awesome. Keep up the great work.

Rey - jQuery Project

Keith Muth said on 07/14 at 05:03 PM

hey rey, that’s awesome! thanks for sharing my article. i saw it on jquery’s twitter feed and it made my day. i’ve been on the jquery mailing list for a while, but i’ll admit i haven’t been as active as i should be. that being said, anytime i have been on it everyone has been super helpful. whenever someone asks me why i like jquery i always say one of the biggest reasons is the amount of support from the jquery community. so thanks for the feedback and i’ll see you on the mailing list.

pab said on 07/22 at 02:32 AM

I’ve been using this for a while now, great tutorial, well explained.

I have one question, is there a way to control the amount of pictures that gets called?

maybe a script?

Blu said on 07/31 at 08:24 PM

Same here Great stuff but nobody seems to know how to display less images...any ideas??

“pab said on 07/22 at 02:32 AM
I’ve been using this for a while now, great tutorial, well explained.

I have one question, is there a way to control the amount of pictures that gets called?

maybe a script?”

Keith Muth said on 08/01 at 08:40 AM

Blue and pab, you can only work with the number of images that exist in the feed, so there’s no way to get more images...unless there is some cool Flickr API magic you could do, which I would love to know about.

As far as getting LESS images, you could just put an IF statement at the end of “$.each(data.items, function(i,item){” that tells the function to stop if a certain number is reached. The variable “i” acts as a counter, so if you wanted to limit the number of images to 4, you could put in “if ( i == 3 ) return false;” (remember that that counter actually starts at 0, so 3 actually gives you 4 images). You could also probably use a FOR loop but I think this is the easiest way.

blu said on 08/05 at 06:17 PM

Thank you SO much the if statement worked great!

Blu

Name:

Email:

URL:

Not a robot? Prove it by entering the word below.


Remember my personal information

Notify me of follow-up comments?