Since the first time I wrote an “Hello World” script I thought that the most exciting thing of programming is the fact that, if you know what you’re doing, you can make your computer do whatever you want and, actually even better, you can control almost everything that runs on a computer. So the reason why I’m writing the second consecutive post about an API service is not because I hate you, rather because I love to control stuff and make them do things that they couldn’t normally do. This time is the turn of Tumblr.
Now of course you know what is Tumblr and probably you also know what kind of contents you can find there (almost everything I would say) and if you don’t have a Tumblr blog don’t worry, this post is useful also for people who just want to download the entire content of any blog with just one click.
Now when we talk about API of websites like Tumblr or Facebook, is also usually necessary to talk about authentication keys and how to get them. Luckily I didn’t have to request any key to do I wanted to do, infact I could easily use the one showed here just because I didn’t need to perform relevant operations like deleting or posting posts (most of the times, requesting an authentication key for an API service is a very twisted process thanks to giant companies that do nothing to make the process easier).
Everything started yesterday when I was searching a way to save every text post of my tumblr blog, and I found two websites: revert.io which is the very first result if you write “tumblr backup” on Google, and this blog post that combines the use of curl, a famous command line tool that does a lot of wonderful things, and the tumblr API to dump everything.
So revert.io was too much for what I was trying to do and plus I really hate to register accounts on websites (even using some temporary mail service) and so I decided to go for the solution showed by that blog. Unfortunately a lot has changed since that post was written in 2008 but the idea of using API to backup everything wasn’t bad at all, so I read this documentation and made a little script (I commented it so that you’ll be able to understand everything).
It’s worth to point out that I used here: a recursive function to shift the offset since Tumblr doesn’t allow to get more than 20 posts in a single call and the “jsonp=?” parameter into the URIs for cross domain requests (more here).
Keep in mind that I wrote this script to suit my purpose, but honestly once you understand how this easy stuff works it’s a cakewalk to adapt it to your needs (don’t forget what I was saying before: if you need to perform particular operations, the authentication key in my script won’t work and you should request one here).
So if we would only get the posted photos of a blog, the URI to use would be:
Or get all the videos:
So as you just saw, in order to get different type of posts you must change the URI, but if you already chose a post type and now you need some simple extra information about the post like the publication date, for example, you have just to explore the response object. Let’s see how Tumblr respond to this URI:
If you haven’t fallen asleep while you were reading, hold on, because there is an advice I’d like to give you that comes directly from the little experience that I have.
The main thing to understand when you’re working with libraries, API but also when you’re programming your own software is the importance of understanding the concepts. It’s very good to read documentations, manuals etc. but you cannot consider yourself a programmer just because you studied books and memorized instructions.
I always say that flexibility and understanding of concepts are the keys to detach yourself from any programming language or any environment you’re using, only in this way you will learn how things work.
PS = if you have some particular requests comment here or contact me, we’ll figure out something together.