Hacking Flickr the JSON way

This was part of my presentation at the Open Hack Day in India and I just got the time to write it up.

Here you’ll learn how to get Flickr photos into your JavaScript solutions without having to resort to using the full API. As this is a hack you will only get the latest 20 photos, if you need more detailed data like restricted to sets or more at once you’ll need to resort to the flickr API.

I mentioned it earlier for some of the Flickr badges shown here and my 24 ways article on how to use Flickr in JavaScript but it is good to have a quick step-by-step to learn how to use Flickr with JavaScript without needing to resort to a full-fledged API call.

Step 1: Locate the photos you want to use on flickr.com

Just surf around the site and find a site that does have an RSS feed. For example, my Flickr stream at http://flickr.com/photos/codepo8 or all the photos of Open Hack Day India at http://flickr.com/photos/tags/hackdayindia/.

Notice that only tags work, not search!

Step2: Click the RSS link or RSS icon (in Firefox) and change the ending of the URL call

This will get you the URL to the RSS feed, for example

http://api.flickr.com/services/feeds/photos_public.gne?tags=hackdayindia&lang=en-us&format=atom

and all you need to do to use this information directly in JavaScript is rename the “atom” at the end to “json”.

Do this and you’ll get a bunch of JavaScript instead of the atom feed:

http://api.flickr.com/services/feeds/photos_public.gne?tags=hackdayindia&lang=en-us&format=json

jsonFlickrFeed({
    "title": "Photos from everyone tagged hackdayindia",
    "link": "http://www.flickr.com/photos/tags/hackdayindia/",
    "description": "",
    "modified": "2007-10-12T14:08:47Z",
    "generator": "http://www.flickr.com/",
    "items": [
     {
      "title": "Open Hack Day coverage in Indian newspaper",
      "link": "http://www.flickr.com/photos/codepo8/1552753330/",
      "media": {"m":"http://farm3.static.flickr.com/2116/1552753330_d4c40d1132_m.jpg"},
      "date_taken": "2007-10-12T15:08:47-08:00",
      "description": "<p><a href="http://www.flickr.com/people/codepo8/">codepo8</a> posted a photo:</p> <p><a href="http://www.flickr.com/photos/codepo8/1552753330/" title="Open Hack Day coverage in Indian newspaper"><img src="http://farm3.static.flickr.com/2116/1552753330_d4c40d1132_m.jpg" width="209" height="240" alt="Open Hack Day coverage in Indian newspaper" /></a></p> <p>The Bangalore newspaper Midday put up this article about the Open Hack Day and apparently I am an ethical Hacker surrounded by Brad and David :)</p>",
      "published": "2007-10-12T14:08:47Z",
      "author": "nobody@flickr.com (codepo8)",
      "author_id": "11414938@N00",
      "tags": "india newspaper bangalore fame midday bradleyhorowitz davidfilo hackday christianheilmann joearnold hackdayindia"
     },
     {
      "title": "Break It",
      "link": "http://www.flickr.com/photos/code_martial/1536271756/",
      "media": {"m":"http://farm3.static.flickr.com/2283/1536271756_c0da3d5f9b_m.jpg"},
      "date_taken": "2007-10-06T23:18:58-08:00",
      "description": "<p><a href="http://www.flickr.com/people/code_martial/">code_martial</a> posted a photo:</p> <p><a href="http://www.flickr.com/photos/code_martial/1536271756/" title="Break It"><img src="http://farm3.static.flickr.com/2283/1536271756_c0da3d5f9b_m.jpg" width="160" height="240" alt="Break It" /></a></p> <p>Joe Arnold does the Break Dance.</p>",
      "published": "2007-10-10T20:43:01Z",
      "author": "nobody@flickr.com (code_martial)",
      "author_id": "61697474@N00",
      "tags": "yahoo dance bangalore taj hackers hacking 50mmf18af hax0rz tajresidency joearnold hackdayindia"
     },

// ... and so on ....
        ]
})

Step 3: Write your display function

As you can see, the Flickr API encloses the dataset inside a function call that executes jsonFlickrFeed, which allows you to define a function with that name:


<script>
function jsonFlickrFeed(o){
  alert(o);
}
</script>
<script src="http://api.flickr.com/services/feeds/photos_public.gne?tags=hackdayindia&lang=en-us&format=json"></script>

The data of the JSON object is a tad askew, especially the encoded output inside the description is pretty much useless in JavaScript. However it is really easy to get the image data and display all the images using the media.m property of each item:


<script>
function jsonFlickrFeed(o){
  var i=0;
  while(o.items[i]){
    document.write('<img src="' + o.items[i].media.m + '" alt="' + o.items[i].title +'">')
    i++;
  }
}
</script>
<script src="http://api.flickr.com/services/feeds/photos_public.gne?tags=hackdayindia&lang=en-us&format=json"></script>

I trust you know however that using document.write is the main reason for hair loss and other problems in this world, so use some nice DOM scripting instead. This was just for the sake of this example.

Step4: Choose the size of the photos with different file endings.

The only remaining annoyance is the size of the photos. Once again, hacking the URLs of flickr helps you there. By default, flickr will choose _m which is the medium size of the photos. Simply rename the _something to the size you need:

  • _b is the big size (1024)
  • _t is the thumbnail
  • _s is the 75×75 pixels square
  • _m is the medium size
  • removing the _? extenstion means you choose the small size.

You can also get the sizes by clicking around the image detail pages, for example http://www.flickr.com/photo_zoom.gne?id=1536271606&size=s and checking the image properties.

Happy Hacking!

20 Responses to “Hacking Flickr the JSON way”

  1. University Update - Firefox - Hacking Flickr the JSON way Says:

    [...] Hacking Flickr the JSON way »

    This Summary is [...]

  2. Fireball » Blog Archive » Hack dey India Says:

    [...] lore | This one was a long time pending post, but reading the blogs of few other lazy people encouraged me to continue to write,after three weeks. After the Int [...]

  3. icon » Hacking Flickr the JSON way Says:

    [...] .gne?tags=hackdayindia&lang=en-us&format= … Read the rest of this great post here Permalink Leave a [...]

  4. Javascript News » Blog Archive » A little Flickr hacking (in a good way) Says:

    [...] Christian Heilmann is at it again posting an entry about a neat method of grabbing Flickr photos without having to using the Flickr API: Here you’ll learn how to get Flickr p [...]

  5. Flickr Hack The JSON Way get flickr photo without flickr API - chazzuka Says:

    [...] Filed under: Web 2.0, Javascript

    Christian Heilmann has written an article about how to get Flickr photos into JavaScript solutions without having to resort to using the full API. Thi [...]

  6. napyfab:blog» Blog Archive » links for 2007-10-22 Says:

    [...] l) Hacking Flickr the JSON way – Wait till I come! (tags: javascript json flickr api image picture tutori [...]

  7. Flickr hacking » D’ Technology Weblog: Technology News & Reviews Says:

    [...] Christian Heilmann is at it again posting an entry about a neat method of grabbing Flickr photos without having to using the Flickr API: Here you’ll learn how to get Flickr [...]

  8. Ajax Girl » Blog Archive » A little Flickr hacking (in a good way) Says:

    [...] our own site. Christian Heilmann is at it again posting an entry about a neat method of grabbing Flickr photos without having to using the Flickr API: Here you’ll learn how to get Flickr p [...]

  9. Shourya Says:

    Thanks, this was long-pending :)
    You’ve been linked off my blog

    shouryalive.com/blog/barcamp-bangalore-planning-session-at-brewhaha-and-salad-bar/

  10. bluesmoon Says:

    Just an FYI. Putting _o as the suffix of your image url will not always give you the original. It works only if your original was a .jpg. If you used a different file type, the extension needs to change as well.

    PS: Please add a convenient tabindex for the checkbox below this textarea.

  11. Markandey singh Says:

    Thank You….

  12. Jaichowdary Says:

    Great tutorial… have just entered the Flickr API world with this…. Thanks a lot!!!

  13. Floyd Pepper Says:

    Nice article Chris. There is a fairly good/hackish javascript class on the heavyweightgeek blog that does this sort of thing.

  14. NIT Trichy - Planet » Blog Archive » Hack dey India Says:

    [...] one was a long time pending post, but reading the blogs of few other lazy people encouraged me to continue to write,after three weeks. After the Internal Hackday, [...]

  15. Joshua Wait Says:

    Thanks for this very clear and easy to follow example. Much easier to follow than Flickr’s own documentation.

  16. Ozzy Says:

    Thanks for the post… I got playing with it and ended up with a couple of toys: http://myggadget.com/blog/2008/09/25/flickr-fun/

  17. Ed Boal Says:

    Please help me!!!!

    I am accessing JSON using jquery and have created an expression to change the medium image (_m) default to original as the image I want to display is 750px wide and therefore doesn’t fit in any of the other size brackets. It won’t work and I have no idea why?!!!! Do I need to buy a pro account to do this???

  18. Chris Says:

    @Ed Boal, do you have a URL I can check? I’ve never encountered that problem, unless the photo is only available as a small size.

  19. pat ricio Says:

    hi:

    is there some way to limit the amount of pictures (less than 20)???

    thanks

  20. jQuery Still Amazes Me After 3 Years - Rey Bango Says:

    [...] with for some time is the Flickr API, mainly because I upload a boatload of pics to the service. Chris Heilmann posted a really great tutorial on pulling back pics using JSON and plain ole JavaScr…. I have to admit that it was insanely easy and Chris, as always, did a great job of breaking it [...]

Leave a Reply

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).