Reading Tumbr Data

Tumblr is a site that has blogs, friends, followers, and all of the other things you expect in a social networking site. It also has a REST API that you can query.  Here’s an example that pulls blog information:

using System;
using System.Xml.Linq;

namespace Tumblr
{
    class Program
    {
        static void Main()
        {
            XElement tumblrResponse =
                XElement.Load("http://joemayo.tumblr.com/api/read");
            XElement posts = tumblrResponse.Element("posts");

            if (posts.Attribute("total").Value != "0")
            {
                foreach (var post in posts.Elements("post"))
                {
                    Console.WriteLine(
                        "nTitle: {0}nBody: {1}",
                        post.Element("regular-title").Value,
                        post.Element("regular-body").Value);
                }
            }

            Console.ReadKey();
        }
    }
}

You can see that I’m using LINQ to XML, pointing the XElement.Load at the URL on tumblr for reading. With LINQ to XML, I’m using the Element, Elements, and Attribute selectors to get at the information I need and display it.  The query processing is based on the following XML document returned from tumblr:

<tumblr version="1.0">
  <tumblelog name="joemayo" timezone="US/Eastern" title="Untitled"></tumblelog>
  <posts start="0" total="1">
    <post id="303295702"
             url=http://joemayo.tumblr.com/post/303295702
             url-with-slug=http://joemayo.tumblr.com/post/303295702/checking-out-tumblr
             type="regular" date-gmt="2009-12-27 22:27:57 GMT" date="Sun, 27 Dec 2009 17:27:57"
             unix-timestamp="1261952877" format="html" reblog-key="k5TElAb8" slug="checking-out-tumblr">
      <regular-title>Checking Out Tumblr</regular-title>
      <regular-body>&lt;p&gt;This is a test post to see how the Tumblr API works.&lt;/p&gt;</regular-body>
    </post>
  </posts>
</tumblr>

Joe

Published by Joe Mayo

Author and independent software consultant. Specializing in Microsoft .NET technology. #ai #botframework #chatbots

Leave a comment