We have our various Developer Blogs on typepad.com, including the one this is posted on :), and it is interesting to gather some statistics about our activities. So I had a look at what the typepad API offers.
Since it is RESTful like many other services on the web, it is quite easy to use it from almost any programming environment. I thought I'd try something lightweight and stumbled upon jQuery which I used in conjunction with JavaScript inside an html page. I just wanted to get information about our posts and then place the results in a simple html table.
When checking if jQuery had any support in Visual Studio I ran into this sample, which did not actually retrieve results (I think the service it is trying to talk to is down or I was supposed to create the service as well), but at least it's given me an idea about what a basic application would look like and also to simply start with an ASP.NET Web Application project. I just had to add an HTML Page item to the project and paste the sample in it.
After reading this article I just replaced the <script> tag's src attribute in the code to point at the jquery-1.4.1.js file which was part of the project by default and the intellisense for jQuery started working.
When I hit Debug (F5) then the html page automatically started on the localhost and I could step through the code to see what's going on. If you want to take advantage of console.log() then you have to make sure that before trying to use that, the Developer Tools window is open in IE - click Tools > Developer Tools (F12). To see the logs just select Script tab on the left side of the Developer Tools window and Console on the right side. To avoid error popping up when you forget to start Developer Tools, best thing is to create you own log function and use that - maybe something like this:
function log(text) {
try {
console.log(text);
}
catch (e) { }
} // function log()
I wanted to get a list of who posted on which blog and when. The only way I've found to achieve that was using the /blogs/<id>/post-assets endpoint, which means that I'm retrieving complete articles/posts not just some basic information about them - maybe there is a less traffic intensive way? I haven't found one.
In order to use the above, I needed to find out the id of the blogs I wanted to gather information about. There may be a programmatic way, but since it won't change anyway and there are not so many of them I just used the id finder of typepad - you just need to type in the URL of one of the articles posted on the blog you want to get the id of.
Now I just had to run an $.ajax() query on the above mentioned endpoints to get the blog posts from them. As it turned out the maximum number of results was 50, so if I want to check more, I need to keep querying starting with a different index. You need to use the start-index query string argument for that.
I wrote the functionality so that I can specify how many days back I want to check. Based on the returned results I could see that the post at the first index was the newest article and the others were coming in chronological order going back to the oldest one. So if I reached an article which was older than the ones I was interested in then I could just stop querying further. Also as soon as I reach such an article I could stop the loop. When using $.each() to iterate through items you can just return false from the function you pass in to achieve just that.
One nice thing about using jQuery is that the received JSON data can be accessed as properties. E.g. in case of the JSON structure /blogs/<id>/post-assets retrieves I can just say data.entries[0].container.displayName to get the name of the blog that the article was posted on :)
Sometimes when debugging the html page it did not contain the latest code. Simply clicking refresh in IE once it popped up seemed to solve this issue.
I also ran into some other nice little issues like in IE you cannot set the <table> content through innerHTML, if you turn off cache for the $.ajax() call it will add some extra query string that typepad does not like, etc, but I got there in the end. :)
Attached is the code I came up with. You can easily modify it to retrieve information about your own typepad blog or other ones: Download DevBlogStats_2012-08-15
Comments