Feeds from Flickr

You can find feeds in Flickr in several ways. First, you can look throughout the Flickr UI for the orange feed icon and the text Subscribe To, as shown in Figure 4-1. You can then find out the feed’s URL from the Feed link. Once you have that URL, you can subscribe to the feed and read the data it contains. For example, if you go to the following address:

http://flickr.com/groups/central/

you will find a the feed icon and link pointing here:

http://api.flickr.com/services/feeds/groups_discuss.gne?id=34427469792@N01&lang=en-us&format=rss_200

Figure 4.1. Figure 4-1.Icon for subscribing to a feed in Flickr. (Reproduced with permission of Yahoo! Inc. ® 2007 by Yahoo! Inc. YAHOO! and the YAHOO! logo are trademarks of Yahoo! Inc.)

Figure 4-1.Icon for subscribing to a feed in Flickr. (Reproduced with permission of Yahoo! Inc. ® 2007 by Yahoo! Inc. YAHOO! and the YAHOO! logo are trademarks of Yahoo! Inc.)

A second way of finding feeds is to consult Flickr’s documentation of its feeds:

http://www.flickr.com/services/feeds/

In a moment, I’ll list the Flickr feeds that are available following the same convention of using URI templates as I did in Chapter 2. All the feeds share two common optional parameters: format and lang.

Flickr Feed Parameters

Let’s look first at format, which can be one of the values listed in Table 4-1.

Table 4.1. Values for the format Parameter in Flickr Feeds
Format Definition
rss_200 or rss2 RSS 2.0
atom_1 or atom Atom 1.0
rss_091 RSS 0.91
rss_092 or rss RSS 0.92

If format is not specified, Atom 1.0 is assumed. Note that RSS 2.0, RSS 1.0, and Atom 1.0 (along with RSS 0.92 and RSS 0.91) are included among the formats. I’ll present some samples of various formats later in this chapter, after I cover the rest of the URL language for Flickr feeds.

The second pervasive and optional parameter is lang, which represents the language you can use to query Flickr. Table 4-2 lists the values. The default language is en-us (English).

Table 4.2. Values for the lang Parameter in Flickr Feeds
Format Definition
de-de German
en-us English
es-us Spanish
fr-fr French
it-it Italian
ko-kr Korean
pt-br Portuguese (Brazilian)
zh-hk Traditional Chinese (Hong Kong)

I’ll now list the feeds available. Remember that all the feed URLs can include the optional format and lang parameters.

Examining the Flickr Feeds

Feeds for public photos in Flickr are available here:

http://api.flickr.com/services/feeds/photos_public.gne

with the following optional parameters:

  • id={user_nsid}

  • ids={comma_delimited_user_nsids}

  • tags={comma_delimited_tags}

  • tagmode={mode} where mode is all (the default value) or any

For example, the following:

http://api.flickr.com/services/feeds/photos_public.gne?tags=flower%2CBerkeley&format=rss2&lang=fr-fr

returns a RSS 2.0 feed, annotated in French, of recent public photos tagged with both flower and Berkeley tags.

You can get a feed of recent photos of a user’s friends here:

http://api.flickr.com/services/feeds/photos_friends.gne?user_id={user-nsid}

where user-nsid is the NSID of the user whose friends’ photos you want to access. There are also optional parameters:

  • display_all can be 1 to show multiple photos per friend, instead of the default value of one photo per friend.

  • friends can be set to 1 to limit photos to only the family and friends for the requested user.

For example, the following:

http://api.flickr.com/services/feeds/photos_friends.gne?user_id=48600101146@N01&friends=0&display_all=0&lang=en-us&format=atom_1

is an Atom 1.0 feed of up to one photo each from my Flickr friends, family, and contacts.

You can get feeds of a group discussion here:

http://api.flickr.com/services/feeds/groups_discuss.gne?id={group-nsid}

Feeds for the group photo pools are accessible here:

http://api.flickr.com/services/feeds/groups_pool.gne?{group-nsid}

Discussion feeds from the Help forum (http://www.flickr.com/help/forum/en-us/) are here:

http://api.flickr.com/services/feeds/forums.gne

You can track recent activity on a user’s photos through feeds here:

http://api.flickr.com/services/feeds/activity.gne?user-id={user-nsid}

Feeds of recent comments made by a user are here:

http://api.flickr.com/services/feeds/photos_comments.gne?user-id={user-nsid}

You can track Flickr news bulletins here:

http://api.flickr.com/services/feeds/news.gne

Now that you know how to access the various feeds available in Flickr, you’ll look at some examples of feeds to understand how various formats are implemented, including the role of extensions. Consider the following excerpt of a sample RSS 2.0 feed of recent public photos with the tag tree:

            http://api.flickr.com/services/feeds/photos_public.gne?tags=tree&format=rss2
            <?xml version="1.0" encoding="utf-8"?>
            <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"
                 xmlns:dc="http://purl.org/dc/elements/1.1/">
              <channel>
                <title>tree - Everyone's Tagged Photos</title>
                <link>http://www.flickr.com/photos/tags/tree/</link>
                <description>A feed of tree - Everyone's Tagged Photos</description>
                <pubDate>Mon, 29 Jan 2007 06:40:42 -0800</pubDate>
                <lastBuildDate>Mon, 29 Jan 2007 06:40:42 -0800</lastBuildDate>
                <generator>http://www.flickr.com/</generator>
                <image>
                  <url>http://www.flickr.com/images/buddyicon.jpg</url>
                  <title>tree - Everyone's Tagged Photos</title>
                  <link>http://www.flickr.com/photos/tags/tree/</link>
                </image>
                <item>
                  <title>Odd Tree</title>
                  <link>http://www.flickr.com/photos/davidleong/373343287/</link>
                  <description>[....]</description>
                  <pubDate>Mon, 29 Jan 2007 06:40:42 -0800</pubDate>
                  <dc:date.Taken>2007-01-28T11:31:31-08:00</dc:date.Taken>
                  <author>nobody@flickr.com (mountainhiker)</author>
                  <guid isPermaLink="false">tag:flickr.com,2004:/photo/373343287</guid>
                  <media:content
                    url="http://farm1.static.flickr.com/127/373343287_df43da61f7_m.jpg"
                    type="image/jpeg" height="160" width="240"/>
                  <media:title>Odd Tree</media:title>
                  <media:text type="html">[....]</media:text>
                  <media:thumbnail
                    url="http://farm1.static.flickr.com/127/373343287_df43da61f7_s.jpg"
                    height="75" width="75"/>
                  <media:credit role="photographer">mountainhiker</media:credit>
                  <media:category scheme="urn:flickr:tags">snow tree vancouver northvancouver
                                  grousemountain</media:category>
                </item>
                <item>
                [....]
                </item>
              </channel>
            </rss>
         

Note the following about this XML fragment:

  • Within an <item> element are some standard elements that you will find in RSS 2.0 feeds, namely, <title>, <link>, <description>, <pubDate>, and <guid>, as well as elements that are extensions of RSS 2.0, including <dc.date.Taken> and the tags in the media namespace such as <media:thumbnail>.

  • Remember that RSS is basically a flat structure. That is, a <channel> consists of <item> elements—but an <item> doesn’t consist of other <item> elements. This contrasts with other XML formats that you will learn about in later chapters of the book (such as the OpenDocument format).

Let’s take a closer look at the extensions used in the RSS 2.0 Flickr feed:

  • The dc prefix (for example, in <dc:date.Taken>), corresponding to the http://purl.org/?dc/?elements/1.1/ namespace, denotes the Dublin Core (DC) metadata standard. DC is a vocabulary for a core set of metadata designed to be applicable to a wide range of digital content.

  • Media RSS (http://search.yahoo.com/mrss), which supplements the <enclosure> element in RSS 2.0, is used by Flickr to store metadata of the photos, such as the following:

    • <media:content>, with the attributes url, type, height, and width

    • <media:title> to hold the photo’s title

    • <media:text> with HTML to embed that picture into a web page

    • <media:thumbnail> to hold a URL to the thumbnail in the url attribute

    • <media:credit> to indicate the owner of the photo

    • <media:category> to holds tags

It’s interesting to look at how all the Flickr metadata is expressed in the other feed formats. The following:

http://api.flickr.com/services/feeds/photos_public.gne?tags=tree&format=atom

shows that Atom 1.0 is natively able to encode much of the information for which Media RSS is being used in RSS 2.0. The Atom feed does, however, use a Dublin Core extension to mark up a date.

Exchange Formats Other Than RSS and Atom

Let’s now return to the feed formats supported by Flickr other than RSS 2.0, RSS 1.0, and Atom 1.0. To get a start at understanding the various formats, load a Flickr feed, which will by default be in Atom 1.0, and replace the format parameter with the alternatives. For instance, if you are curious about CDF, change the following:

http://api.flickr.com/services/feeds/photos_public.gne?tags=tree

to the following:

http://api.flickr.com/services/feeds/photos_public.gne?tags=tree&format=cdf

In the following sections, I’ll mention some but not all of formats supported in Flickr feeds.

RSS 0.91 and RSS 0.92

Although RSS 0.91 and RSS 0.92 have largely been superseded by RSS 2.0, you can still look at examples such as this:

http://api.flickr.com/services/feeds/photos_public.gne?tags=tree&format=rss_091

and this:

http://api.flickr.com/services/feeds/photos_public.gne?tags=tree&format=rss_092

to see how those older versions do not contain any of the Media RSS or Dublin Core extensions you find in the RSS 2.0 feeds.

JSON

The JSON format facilitates the consumption of Flickr feeds in JavaScript. I discuss JSON at greater length in Chapter 8. However, if you look at the following:

http://api.flickr.com/services/feeds/photos_public.gne?tags=tree&format=json

you can see that Flickr returns JavaScript code, specifically a call to a jsonFlickrFeed() function with one parameter. This parameter is a JavaScript object that holds the feed data. As a JavaScript programmer, you can use this JSON-­formatted feed by supplying a jsonFlickrFeed() function to do something with the feed, such as displaying it on a web page.

Here’s an example of some code that reads the Flickr JSON feed and renders the feed as HTML in the browser:

               <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
               <html xmlns="http://www.w3.org/1999/xhtml">
                 <head>
                   <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
                   <title>Flickr JSON feed</title>
                   <script type="text/javascript">
                     //<![CDATA[
               
                     function jsonFlickrFeed (feed) {
                       var feed_div = document.getElementById("feed");
                       var feed_html = '<p>' + '<a href="' + feed.link+ '">' +
                         feed.title + '</a>'+ '</p>';
                       for (x=0; x<feed.items.length; x++) {
                         feed_html += '<a href="' + feed.items[x].link + '">' +
                           '<img ' + 'src="' + feed.items[x].media["m"] + '"' + ' alt="' +
                           feed.items[x].title + '"' + '/>' + '</a>'+ '<br/>';
                       }
                       feed_div.innerHTML = feed_html;
                     }
               
                     function load() {
                       var head_element = document.getElementsByTagName("head")[0];
                       var newScript = document.createElement('script');
                       newScript.type = 'text/javascript';
                       newScript.src = 'http://api.flickr.com/services/feeds/photos_public.gne?
               tags=tree&format=json';
                       head_element.appendChild(newScript);
                     }
               
                     //]]>
                   </script>
                 </head>
                 <body onload="load()">
                   <div id="feed" />
                 </body>
               </html>
            

In Chapter 8, I’ll return to how to use JSON in mashups.

Other Feed Formats

The other available formats are less commonly used to represent feeds in web sites other than Flickr but come in handy depending on your specific needs. For example:

  • You can use the php or php_serial format to generate a convenient representation of the feed for PHP programming. (These formats have roughly the relationship to PHP that the json format has to JavaScript.)

  • You can use the sql format to quickly generate SQL code to get the Flickr feed into your SQL database.

  • The csv format comes in handy for importing your feed into a spreadsheet.

  • The rss_200_enc format is used to insert a reference to the original photo in an RSS 2.0 <enclosure> tag.