Calling the Flickr API getRecent method with c# and Linq to XML
UPDATE: I have updated this article to now use the flickr.interestingness.getList method instead of getRecent. I love getRecent, but out of every 3 to 400 pictures there’s almost always one or two pictures that most audiences would consider offensive. All of the photos returned by flickr.interestingness.getList are artistic and very few of them would be considered offensive by most people.
One of my favorite features of Flickr is browsing through photos that have just been uploaded. You can get a glimpse of this on Flickr’s home page, but I decided I’d write some code so I could look at a bunch of them all at once. I also like making calls against other web API’s (like from Twitter, FriendFeed or delicious).
Here’s a sample of the kind of code you might write to call Flickr:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
namespace CurrentFlickrPicsConsoleExample
{
class Program
{
static void Main(string[] args)
{
string currentPicsXML = GetCurrentFlickrPicsXML();
Console.WriteLine(currentPicsXML);
Console.WriteLine();
Console.WriteLine("Press ENTER to exit this app...");
Console.ReadLine();
}
private static string GetCurrentFlickrPicsXML()
{
string result = new WebClient().DownloadString("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=YOURFLICKRAPIKEY");
return result;
}
}
}
If you want to execute this code, copy it and paste it into a new Console application and be sure to replace ‘YOURFLICKRAPIKEY’ with your key. Instructions to get a Flickr API key are here.
A subset of the XML returned from Flickr looks like this:
<photos page="2" pages="89" perpage="10" total="881"> <photo id="2636" owner="47058503995@N01" secret="a123456" server="2" title="test_04" ispublic="1" isfriend="0" isfamily="0" /> <photo id="2635" owner="47058503995@N01" secret="b123456" server="2" title="test_03" ispublic="0" isfriend="1" isfamily="1" /> <photo id="2633" owner="47058503995@N01" secret="c123456" server="2" title="test_01" ispublic="1" isfriend="0" isfamily="0" /> <photo id="2610" owner="12037949754@N01" secret="d123456" server="2" title="00_tall" ispublic="1" isfriend="0" isfamily="0" /> </photos>
In order to get the Linq to XML magic, you need to create a System.Linq.Xml.XElement object. The XElement can parse the XML string. I created this extension to System.Net.WebClient to make the process super simple:
public static XElement DownloadXElement(this System.Net.WebClient webClient, string address)
{
string result = webClient.DownloadString(address);
XElement x = XElement.Parse(result);
return x;
}
Assuming I have a class created called Photo, I can write this super simple Linq to XML code to create an IEnumerable collection of photos (assuming x is of type XElement):
XElement x = (new WebClient()).DownloadXElement("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&amp;api_key=YOURFLICKRAPIKEY");
var query = from b in x.Elements("photos").Elements("photo")
select new Photo
{
Farm = b.Attribute("farm").Value,
Id = b.Attribute("id").Value,
Secret = b.Attribute("secret").Value,
Server = b.Attribute("server").Value,
Owner = b.Attribute("owner").Value,
Title = b.Attribute("title").Value
};
foreach (Photo photo in query)
{
photos.Add(photo);
}
This query is very simple - we just loop through all of the photo elements which are children of the photo element. At each photo, we create a new Photo object, reading its values from the attributes of the xml photo. Those Photo objects then go into the result of the query, query, which we know implements IEnumerable. Its actual type is irrelevant to use since we’re only interested in its IEnumerable implementation.
For the purposes of this article, we can put all these pieces together into one console app, call the Flickr API, receive the XML, parse it into a collection of Photo objects and then spit out the photo urls for each of those objects.
I have created a web application using this code. I call it “Tad’s Flickr Toy - Interestingness.” You can view this application here.
This article is sort of a prelude to a series of articles using a WCF component built around this code. In the series I will be hosting and using the component in all sorts of scenarios, showing how to use WCF in many different ways.
Add New Comment
Viewing 1 Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks
(Trackback URL)