Are you backing up your data regularly? You probably are and if not you should :) What about your social media accounts, is that important to backup? For instance, Twitter: have you ever wanted to backup your tweets, direct messages, favorites…?
There are pleny of backup solutions for Twitter. However, it seems that most of them are 3rd party websites and I am not sure if you can automate them easily (to integrate them with a backup script for instance). Wouldn’t it be cool if you could drive this task using only the command line?
Recently, I came accross the Twitter CLI gem. It’s a very powerful tool to query Twitter from the command line. In this post, I will show you how to leverage it to backup your Twitter data.
The t gem
The Twitter CLI gem (also known as the t gem) is an open source ruby gem that you install and use to query Twitter from the command line. It’s a Ruby gem. Gross, right? You have to install Ruby. That’s OK, installing Ruby is actually quite easy: checkout rvm or if you are running Ubuntu, checkout the new Ruby packages from Brightbox.
Once you have your ruby environment up and running, the installation is trivial:
Once that’s done, if you type t in your shell, you should see the t help screen which lists all the available commands. Configuration will be the next step. You need to authorize t to use your Twitter account. Everything is explained on t’s website, so I will not repeat it here.
You should now be ready to use t. If you want to see some usage examples, jump back to t’s website. It contains some good examples.
Backing up your Twitter account
I want to backup my tweets (timeline), my retweets, my favorites, my direct messages and the list of people I am following.
Using the timeline subcommand I can retrieve all of my tweets. The following command will retrieve the 3000 most recent tweets for my username and save them as CSV in tweets.csv. You have to specify a number as the default behavior is to retrieve only the first 20.
The retweets do not seem to be included in the timeline. Here is the command to fetch them. It’s very similar to the previous command. Note that in this case, it does not require you to specify the username
The favorites command:
For the direct messages, there are actually two separate commands: one for received messages and one for sent.
Finally, for the list of people I follow:
Putting it all together
Now that we have the basic commands needed for the backup task, we can put them all together in a shell script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
We now have a very easy way to backup a Twitter account from the CLI. As I am finishing up this post I realize it would be cool to have the backup feature available as a t command. I am thinking about submitting a pull request that would allow something like:
What do you think?