SAM MORRELL


ASTROPHYSICIST

Messing with the Tumblr API

June 11, 2012   |   Reading time: ~6 mins

Last night I had a near overwhelming urge to do something web design related, which really isn’t good when you’re in a battle to the death Angry Birds tournament. None the less, I dropped that (I lost catastrophically by the way) and retreated to my favourite text editor and started writing HTML. It was very shortly after that I realised that I’d need a purpose for this rather than just doing a free for all so I wondered what I could set my mind upon for a little challenge. I finally set my mind on doing a Tumblr theme and started designing it. It was once again shortly after this that I realised there’d be no way to test out my theme short of uploading it… and that there’s a Tumblr JSON API. There is of course awesome theme development tools such as Thimble which I contemplated using, however I like to learn some new stuff when I do something for fun so I finally settled on writing my own.

Getting Started

I’m currently in the ‘I have the idea and a rough thought about how it’ll work but it needs a bit of thought before I retreat into the ‘coding mode’ stage of hacking with code. Also, I’m currently unsure what kind of thing I should be aiming for. My two ideas are to use the Tumblr API to either make a primitive Tumblr client or to write somewhat of a Tumblr simulator that will read in, parse a theme and spit out the HTML so that it can be easily tested; most likely using the API to fetch dummy data from the designers Tumblr blog to fill it in.

Next question is what should I write it in. I’m going to opt for either PHP, C# or C++. Basically I know PHP very, very well so it’d be relatively easy to pull off with PHP but there wouldn’t be as much challenge in it. I could write it in C# but every time I have attempted to do anything relatively useful with it before the complete ickyness of the .NET framework has thwarted me before anything decent got done and then there’s C++ which is probably going to be the most difficult to do because I’d probably have to write my own memory allocator, parser for JSON and abstraction layer for Berkeley Sockets / WinSock. So, C++ would be the most work and the hardest, but it’d also return the best rewards as it’d be the one I’d learn from the most.

An Aside on Tumblr Themes

So, as it turns out theming Tumblr is actually pretty nice. You just write straight HTML and a templating engine on Tumblr’s side puts in all the data. All you do is fill in tags inside curly braces, such as {title}. There are two types of tags when you’re making a tumblr theme Variables and Blocks. Both of these are rather important. Variable tags are used to place data into the document like URLs for stuff and content. Blocks are used to define chunks of code to give context to the variables. For example, one of the most commonly used tags within the templating engine is {Post:[post type]} where you replace [post type] with the type of a Tumblr post like ‘text’, ‘photo’ or ‘photoset’. With this, the templating engine is able to take that chunk of code and for each post that the user has posted find the chunk of code that corresponds to the post type, ram it into the page output and slot in the data where the variables are. It’s actually quite an elegant system really. Really, I need to be able to parse this, which fundamentally isn’t that difficult. Using PHP or Javascript, constructing the lexical analyser would be fairly easy and parsing it would pose a little more of a challenge but still pretty managable. Though, since I’m aiming to write this into C++ I’m going to have to use some kind if finite state machine to parse the theme byte by byte. This isn’t too hard to do but using only C++ primitives it’s going to be a chore.

Sorry if that descended into nerdiness at the end there, I do like to over-complicate things for no other reason than it’s a challenge. Seriously, don’t let me put you off making a Tumblr theme, it’s super fun and pretty easy if you have a decent grasp of HTML and CSS. It’s well worth checking out the Tumblr Developer pages, more specifically the page about making a theme, as they are easy to understand and invaluable.

Another Aside on the Tumblr API

Two asides in one blog post? With all this zig-zagging I might get ill. Anyway, back to business! As well as being able to write your own themes you can do all kinds of cool stuff off of Tunblr using your blog’s data. You can fetch all this using a Tumblr API. I will apologise in advance here, because there’s no real way to not lapse into geek speak. The Tumblr API is a cool part of Tumblr’s site where you can use simple HTTP requests to fetch data from blogs in the form of JSON responses. The good thing is that the API can only give access to most of your data if it has a valid API key / OAuth key. That brings me along to my next ponderance. I have to either acquire or write an OAuth framework. OAuth is an authentication method using HTTP requests to verify someone’s identity. My choices are this. Either hunt down a pre-written library for OAuth, which there are quite a few of, or to use sockets (or maybe cURL) to write my own. I am aware from watching my friend carry out this task that it’s not mean feat, but that’s the point of this whole exercise.

Here Goes Nothing

You’ve pretty much heard the sort of things that go on in my head when I approach a new project such as this. What language is best to write it in, what other libraries should I use or should I write my own. Whatever happens, I’m sure I’ll have a little fun with the Tumblr API, even if I just default back to PHP. I have tried to disperse Wikipedia articles through this post as most of the stuff I talked about is nasty if you haven’t encountered  it before. If you have been, thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Sorry, there are currently no comments for this artcle. Why not start a conversation?