I written this bit of code for 2 reasons, first was to read whatever was posted onto a Tumblr blog and integrate it into a clients website. The other reason was if Tumblr was to go down, the blog page would still work due to the caching on the Tumblr data.
I always choose Tumblr when setting up a blog for a client where no one really looks direct at the blog, its more a easy way of letting the client create blog post and it magically appears on their website.
Anyway, enough rambling here is code. Please remember there is probably a million ways to do this and mine might not be the best but it works.
Header – This tries to load the XML from tumblr and store it in a file on the webserver. It then checks to see if a cached file/xml is present, if not it makes a cached file/xml out of the file stored on the webserver. The way the cache is created using a dependency, so if the file changes, the cache will change. It then loads the cached file/xml in a var called Posts.
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Linq" %>
Dim Posts As XElement
Sub Page_Load(sender As Object, e As EventArgs)
Try
'Read Tumblr Feed and save it locally
Dim xmlMasterUpdateDoc As New XmlDocument()
xmlMasterUpdateDoc.Load("http://*nameofyourtumblrblog*.tumblr.com/api/read")
xmlMasterUpdateDoc.Save("*somewhereonyourhostingspace*\blog.xml")
Catch xmlEx As XmlException
'Catch any XML error and display it.
Response.Write("XML Exception Thrown -> " + xmlEx.Message)
Catch eX As Exception
'Catch the error and display it.
Response.Write("Exception Thrown -> " + eX.Message)
Finally
End Try
If Cache("tumblrXE") is Nothing then
'There is no tumblrXE item in the Cache, so load it from the local file
Dim tumblrResponse As XElement = XElement.Load("*somewhereonyourhostingspace*\blog.xml")
'Add it to the Cache and make it dependant on the local file (so it if changes the cache will)
Cache.Insert("tumblrXE", tumblrResponse, New CacheDependency("*somewhereonyourhostingspace*\blog.xml"))
End If
Posts = Cache("tumblrXE").Element("posts")
End Sub
This next bit of code, goes through all the posts and depending on the type displays them. I have only done regular, link and image post types.
<%
'if this value is 0, there are no posts.
If Posts.Attribute("total").Value <> "0" Then
'loop around all the posts
For Each itemElement As XElement In Posts.Elements("post")
If itemElement.Attribute("type") = "link" Then
Response.Write(itemElement.Attribute("date").Value)
Response.Write(itemElement.Elements("link-text").Value)
Response.Write(itemElement.Elements("link-url").Value)
End If
If itemElement.Attribute("type") = "photo" Then
Response.Write(itemElement.Elements("photo-caption").Value)
Response.Write(itemElement.Attribute("date").Value)
Response.Write(itemElement.Elements("photo-link-url").Value)
Response.Write(itemElement.Elements("photo-url").Value)
Response.Write(itemElement.Elements("photo-caption").Value)
End If
If itemElement.Attribute("type") = "regular" Then
Response.Write(itemElement.Elements("regular-title").Value)
Response.Write(itemElement.Attribute("date").Value)
Response.Write(itemElement.Elements("regular-body").Value)
End If
Next
End If
As I say, not sure if it’s the best way but it works for me so hopefully has given you a helping hand to implement your own solution.