Yesterday I blogged a simple example of using ColdFusion's RSS parsing feature with the cffeed tag. One of the readers who commented on the story mentioned that he was interested in using cffeed to parse an RSS feed from the National Geographic. What made this feed a bit different is that it was specifically for pictures. He wanted to take those pictures and download them. What follows is a simple template that does just that. (I'll warn folks now though - I did not check what NG's requiresment were for copyright notices. You will want to do that if you make use of this code.)

<cfset rssUrl = "http://feeds.nationalgeographic.com/ng/photography/photo-of-the-day/">

<cffeed action="read" source="#rssUrl#" query="entries">

<cfset dir = expandPath("./ngg")> <cfif not directoryExists(dir)> <cfdirectory action="create" directory="#dir#"> </cfif>

<cfloop query="entries"> <cfset localfile = listLast(linkhref,"/")> <cfoutput>checking #localfile#... </cfoutput> <cfif not fileExists(dir & "/" & localfile)> <cfhttp method="get" getAsBinary="yes" url="#linkhref#" path="#dir#" file="#localfile#"> downloading!<br/> <cfelse> skipped<br/> </cfif> </cfloop>

Done.

The first two lines of code are pretty similar to what I used yesterday. I got rid of the properties attribute since I don't need the metadata from the feed. I'm going to store the images in a subdirectory called ngg, so you can see the simple check I use there to ensure it actually exists. Now for the fun part.

As I loop through the RSS feed, I make use of the linkhref column for the image url. This is where NG stores the path to their image. I get just the filename from that and see if I have a copy already. If not, I just use cfhttp to fetch it down. And that's it. Really. Here's a quick screen shot from my local directory after running this once.