Flickr Photo Finder in .NET and C#

Jun
06
2006

Since I'm working to get a bit more familiar with Visual Studio and C# for a couple of work projects, I decided to implement something familiar. One of my most used tools is the Flickr Photo Finder, which made it a prime target. Since LOTS of you use it too (100,000+ last month), it made an even better target and worthy of a quick writeup.

So, I ported the basic concept over into a C# commandline app. It compiled beautifully with Visual Studio Express edition (the free one I'm using until the real one shows up). It takes a tag as input and is hard-coded to use the Attribution license since I rarely choose anything else. That's "6" in the code. If you want to use other licenses, you'll have to dig through the API docs to see the other ones.

At any rate, I just run the "flickrfinder.exe searchterm" and it dumps an .html file named after the term into the directory with both the previews and the HTML snippets for posting. This way, I can go back to frequently used photosets without having to search again.

The meat of the code is below. It uses the Flickr.NET library (though there seem to be something like 4-5 libraries all with the same name), which is just added as a reference in the project.

Create a new commandline project in VS.NET and add the Flickr.NET dll to the project. Add the following 2 lines to the "Using" imports section.


using System.IO;
using FlickrNet;

Then, in the main method, put the following code into the main method, but adjust it for your api key and secret key. See the Flickr API docs for that info. The code itself is pretty straightforward.


String tags = args[0];
Flickr flickr = new Flickr("yourapikey", "secretkey");
PhotoSearchOptions options = new PhotoSearchOptions();
options.SortOrder = PhotoSearchSortOrder.InterestingnessDesc;
options.License = 6;
options.Tags = tags;
Photos photos = flickr.PhotosSearch(options);
String outputString = "<html><head><title>Flickr Photo Finder</title></head><body><table>";
String individualString = "";
foreach (Photo photo in photos.PhotoCollection)
{
String thumbnail = photo.SquareThumbnailUrl;
String mediumPhoto = photo.MediumUrl;
String smallPhoto = photo.SmallUrl;
String photoLink = photo.WebUrl;
String title = photo.Title;
String photographer = photo.OwnerName;
individualString = "";
individualString = "<div class=\"flickr-illustrate\" style=\"border: solid black 2px;padding: 3px;float:right;display: inline;font-family: Arial,Helvetica;font-style: italic;font-size: .75em;text-align: center;padding-left:5px;padding-right:3px;padding-top:5px;margin:8px;\"><a href=\"" + photoLink + "\"><img src=\"" + smallPhoto + "\" style=\"border: solid black 1px;padding: 0px;margin: 0px;\" border=\"0\"></a><a class=\"f-i-attribution\" style=\"text-decoration: none;color:black;display:block;font-weight:bold;text-align:right;\" href=\"" + photoLink + "\">Photo: " + photographer + "</a></div>\r\n";
individualString = "<tr><td valign=\"top\">" + individualString + "</td><td><textarea rows=\"8\" cols=\"50\">" + individualString + "</textarea></td></tr>";
outputString = outputString + individualString;
}
outputString = outputString + "</table></body></html>";
// create a writer and open the file
TextWriter tw = new StreamWriter(tags + ".html");
// write a line of text to the file
tw.WriteLine(outputString);
// close the stream
tw.Close();

 

Comments on this post

Feedback is always welcome. Read some from other folks or leave your own below. Just keep things civil and remember that what you post lives on in public. Forever.

Thanks,
J

One Response to “Flickr Photo Finder in .NET and C#”

  1. J Wynia Says:

    Oh, I forgot to add what my next step with this whole thing is going to be. I'm going to use the Yahoo text analyzer to dig out significant keywords in the post itself, the categories, the Technorati tags, etc. and any linked documents, and dig for images automatically.

    This will then be part of writing up a post. I just hit "find image" and get a list of possibilities. This whole thing will likely end up as a tab in the eventual writing application for hooking into Wordpress that I recently started.

Leave Your Own Comment

By submitting a comment, you agree to license it under the terms of the Creative Commons Attribution license.

People who post comments get the added benefit of visiting the site without advertising.

© 2003-2009 J Wynia. All original content is licensed under the terms of the Creative Commons Attribution license unless otherwise noted. Content from other sources is licensed under its original terms.