JP's blog

Tech talk with a french twist.

Backing Up Your Twitter Account With "t"

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:

Installing the gem
1
gem install t

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.

Backing up tweets
1
t timeline @jphpsf --csv --number 3000 > tweets.csv

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

Backing up retweets
1
t retweets --csv --number 3000 > retweets.csv

The favorites command:

Backing up favorites
1
t favorites --csv --number 3000 > favorites.csv

For the direct messages, there are actually two separate commands: one for received messages and one for sent.

Backing up direct messages
1
2
t direct_messages --csv --number 3000 > dm_received.csv
t direct_messages_sent --csv --number 3000 > dm_sent.csv

Finally, for the list of people I follow:

Backing up followings
1
t followings --csv > followings.csv

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.

Simple Twitter backup 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
#!/bin/sh
# Simple Twitter backup script

export DAY=`date +'%Y-%m-%d'`

echo "Backing up tweets..."
t timeline @jphpsf --csv --number 3000 > tweets-$DAY.csv
echo "Backing up retweets..."
t retweets --csv --number 3000 > retweets-$DAY.csv
echo "Backing up favorites..."
t favorites --csv --number 3000 > favorites-$DAY.csv
echo "Backing up DM received..."
t direct_messages --csv --number 3000 > dm_received-$DAY.csv
echo "Backing up DM sent..."
t direct_messages_sent --csv --number 3000 > dm_sent-$DAY.csv
echo "Backing up followings..."
t followings --csv > followings-$DAY.csv

echo -e "\nBacked up the following:"
echo -e "- "`wc -l tweets-$DAY.csv|cut -d" " -f 1`" tweets"
echo -e "- "`wc -l retweets-$DAY.csv|cut -d" " -f 1`" retweets"
echo -e "- "`wc -l favorites-$DAY.csv|cut -d" " -f 1`" favorites"
echo -e "- "`wc -l dm_received-$DAY.csv|cut -d" " -f 1`" DM received"
echo -e "- "`wc -l dm_sent-$DAY.csv|cut -d" " -f 1`" DM sent"
echo -e "- "`wc -l followings-$DAY.csv|cut -d" " -f 1`" followings"

echo -e "\nDone\n"

Wrapping up

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:

Backup command
1
t backup -o /path/to/backup

What do you think?

Possibly related posts

Comments