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:

  1. Write a new page or post.
  2. Regenerate the site.
  3. Push it to the server.
  4. 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’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 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:

  1. Install tweepy and bitly_api.
  2. 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.

Once you have the keys, plug them into this script, or one like it:

#!/usr/bin/env python

Created by Chad Black on 04/22/2011


import sys
import tweepy
import bitly_api

# replace the hashes with your keys pulled from step 2 above.
CONSUMER_KEY = '#######'
CONSUMER_SECRET = '########'
ACCESS_KEY = '######'
ACCESS_SECRET = '#########'

# authenticate a session with twitter
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

# connect to with your own username and key, provided by
bit = bitly_api.Connection('username', 'key')

# on the command line, when calling the script choose whether to update (update),
# update with a shortened url included (withShort), retrieve the last 7 tweets to your 
# timeline (tweets), or retrieve the last 5 @mentions (mentions).

def do_the_twitter(choice):
    if choice == 'update':
    elif choice == 'withShort':
	data = bit.shorten(sys.argv[3])
    elif choice == 'tweets':
	tweets = api.home_timeline(count=7)
	for tweet in tweets:
		print '@'":  "+tweet.text
    elif choice == 'mentions':
	mentions = api.mentions(count=5)
	for mention in mentions:
		print '@'":  "+mention.text
    else: pass

# the first argument passed from the cli indicates choice, the others provide
# further data for update or update with a shortened url


Save this script, and then add it to your bash PATH by editing your .profile or .bash_profile file, which is usually in your home directory. Add these two lines:

alias twitter='python /path/to/the/script/folder/'

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: "

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.


Associate Professor of Early Latin America Department of History University of Tennessee-Knoxville

Tagged with: ,
Posted in programming
2 comments on “twitter from the command line
  1. […] my other home ← twitter from the command line […]

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


Hacer juicio ú dictamen acerca de alguna cosa... significando que el objeto excita el juicio ú dictamen en la persona que le hace.

Deducir ante el Juez la accion ú derecho que se tiene, ó las excepciones que excluyen la accion contrária.

RAE 1737 Academia autoridades
Buy my book!

Chad Black

I, your humble contributor, am Chad Black. You can also find me on the web here.
%d bloggers like this: