I was introduced to NASA's Image Of The Day RSS feed from Doug Jenkins' post named Dynamic Maps and finally got around to doing some feed parsing.
I put together a short piece of VBA code that caches the RSS feed and returns all of the Image Of The Day URLs. What you choose to do with this is up to you — putting the images into a WebBrowser Control, adding them to a listbox, or downloading them and displaying them elsewhere are all possibilities. For this exercise I only grabbed the URLs, but you may also want the caption, text description, or latitude/longitude.
Function to download Images Of The Day
The RSS feed returns the last ten items, so this function returns ten URLs as a 1D array.
Function Get_NASA_IOTD(Optional forceRequery As Boolean) As String() Dim tempDir As String Dim tempFilename As String Dim xmlDoc As Object ' MSXML2.DOMDocument60 Dim rss As Object ' MSXML2.IXMLDOMNode Dim channel As Object ' MSXML2.IXMLDOMNode Dim item As Object ' MSXML2.IXMLDOMNode Dim tempString() As String Dim i As Long Const FEED_URL As String = _ "http://earthobservatory.nasa.gov/Feeds/rss/eo_iotd.rss" ' build filepath from feed filename tempDir = Environ("temp") tempFilename = tempDir & Application.PathSeparator & _ Split(FEED_URL, "/")(UBound(Split(FEED_URL, "/"))) ' create a XML DOM Document Set xmlDoc = GetDomDoc If xmlDoc Is Nothing Then Exit Function If Len(Dir(tempFilename)) = 0 Or forceRequery Then ' grab IOTD feed xmlDoc.Load FEED_URL Else ' feed is cached, load from local temp folder xmlDoc.Load tempFilename End If If LoadError(xmlDoc) Then Exit Function ' cache the feed, but only if there were no loading errors xmlDoc.Save tempFilename ' start parsing RSS feed Set rss = GetRootNode(xmlDoc) Set channel = GetNode(rss, 1) ' start at node 11, first ten nodes can be ignored For i = 11 To channel.childNodes.Length Set item = GetNode(channel, i) ' increase array size ReDim Preserve tempString(1 To (i - 10)) tempString(i - 10) = _ item.childNodes(2).Attributes.getNamedItem("url").nodeTypedValue Next i Get_NASA_IOTD = tempString End Function
Just like the Fly On Time API, we do not need the XMLHTTP object because the IOTD website provides a .rss file (which is XML formatted) that we can load directly into a XML DOM Document.
GetDomDoc, LoadError, GetRootNode and GetNode may be found in the MSXML Class Library.
The following example downloads the NASA IOTD RSS feed and caches it, then returns the image URLs for each of the images in the feed.
Sub TestIOTD() Dim results() As String Dim i As Long results = Get_NASA_IOTD For i = LBound(results) To UBound(results) Debug.Print results(i) Next i End Sub
Here are the last few images (as of this writing):
Update the Cache
There is no time control built into the function, so if a whole day passes the function will still used the cached copy (if available) to grab the URLs. You would need to grab the pubDate node (a few nodes down from the URL node) and associate it with the URL. A separate function would take the publication date of the first URL (which is the most recent one), parse it and compare it with the current date to determine if the feed needs to be requeried. Or, you could simply schedule a .vbs that runs every day to clear your temp folder of .rss files