Menu

twitter from the command line

I’m planning to move my home site and course sites over to a static site generator once the semester is over. Though I haven’t decided for certain, I’m leaning towards jekyll, in part because reading the documentation it makes the most sense, as does the liquid templating language. In preparation I’ve been trying to figure out how to take care of a few things I’ve done with wordpress plugins. One of those things is to tweet when a site is updated. I’m leaning towards using fabric to deploy the site to my server. Fabric essentially allows one to run terminal commands or python code snippets on local or remote servers from a single file. So, the workflow would be:

Write a new page or post.

Regenerate the site.

Push it to the server.

Tweet that the site is updated, together with a short url.

Jekyll is a ruby site generator, but I figure I’ll script it with as much python as I can. To solve the twitter part of the workflow, I decided to put together a python script using the libraries tweepy and bit.ly_api, which provide python wrappers for twitter and bit.ly’s APIs. The idea is to be able to tweet a new post from a fab file, but this script also allows you to look at recent tweets or mentions, post a status update, or post a status update with a bit.ly-shortened url from the cli. Once I’m at the site-generation phase, I’ll pass the URL of the new page to the script, and include a twitter update automatically.

There are two steps necessary to make this work:

Install tweepy and bitly_api.

Follow the instructions here to authenticate your soon-to-be-new twitter command line application using OAuth. Make sure to keep track of the consumer and access public and private keys produced by the process.

To use the script from the command line, you have to restart terminal or enter $source ~/.bash_profile, and then simply enter the alias, your choice, and any additionally needed arguments. For example, to update status:

$twitter update "My very compelling <140 characters of wisdom."

Note that it’s simply better to use double quotations in case your tweet includes an apostrophe. To update status with a url to be shortened:

$twitter withShort "My compelling tweet now includes a short url at the end: " http://example.com

To retrieve the last seven statuses on your timeline:

$twitter tweets

Why seven? That’s the number of tweets that will show on an 80×24 terminal window without needing to scroll. You could just as easily tweak the script to accept a number argument. Finally, to get your most recent @mentions, it’s simply:

$twitter mentions

That’s it for now. I will probably edit the script to interface with OS X’s keychain so that secret keys don’t need to be sitting around in plain text files on your system.

Update: I edited the script to allow retweets, replies, and use of OS X’s keychain. The new one is here.

[…] have scripts for monitoring server memory, batch renaming photos, tweeting from the command line (here and here), bursting and OCRing pdfs, posting to wordpress.com using markdown, using easygui for […]