new and improved twitter cli, now with more twitter sauce

Over the weekend I posted a python script to check one’s timeline, mentions, or post an update from the terminal in Mac OS X. I’ve tweaked it a bit for better security and to add the ability to reply or retweet something from your feed. Of course, this isn’t a nice gui app, so it’s slightly more complicated than one might be used to. But, then again, why bother to check twitter or tweet anything from the command line anyway? Just for fun, of course. Actually, I do like that I can be working, and check the most recent tweets in my stream without the temptation to go through several hours of stuff and end up in link distraction wonderland.

On the security front, it just feels wrong to me to have secret keys lying around in plain text files like an American on vacation in Rio with his wallet hanging out and a $2000 camera around his neck. The simplest way to deal with this on a Mac is to use its built-in keychain. There is an elegant little python module to do just that: keyring. Using keyring, adding a password or key to the Mac’s keychain is as simple as this line of code: keyring.set_password('application name', 'user', 'password'). You can exchange the name you gave your twitter app, your user name, and the ACCESS and CONSUMER secret keys for the password, and those keys will be safe. Do the same with your API key. No need to put them directly in your code. In fact, it would be easy to simply add the keyring code to the application authorization process described here so that you wouldn’t have to cut and paste anything at all. At any rate, the script now calls the Mac keychain to get the secret credentials for authorization for both the twitter and APIs.

On functionality, I added status id numbers to the end of retrieved tweets and mentions. With that number, you can reply to or simply retweet a status. So, a status prints in the terminal like this:

@parezcoydigo:  trying to decide on a family trip to LA or to DC in 
June around thatcamp. : 62622894181724160

The trailing number is the status ID. To reply to the status, add the status ID as an argument. I would enter this in the terminal:

$twitter reply "@parezcoydigo blah blah blah." 62622894181724160

The name of the person you are replying to is not automatically prepended to the message, so you have to enter that, but regardless the status will be linked from twitter’s perspective.

Retweeting is even simpler. Simply pass the status ID in the terminal:

$twitter retweet 62622894181724160

Happy tweeting. Here’s the full text of the revised script:

#!/usr/bin/env python


Created by Chad Black on 04/22/2011


import sys
import tweepy
import bitly_api
import keyring

CONSUMER_SECRET = keyring.get_password('CONSUMER_SECRET', 'parezcoydigo')
ACCESS_KEY = '28424167-jwvywgfEjqcXxEbQU3Y36w2md5SyL6NC5JI5NR2bd'
ACCESS_SECRET = keyring.get_password('ACCESS_SECRET', 'parezcoydigo')

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

bitlyKey=keyring.get_password('bitly', 'parezcoydigo')
bit = bitly_api.Connection('parezcoydigo', bitlyKey)

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=20)
		for tweet in tweets:
			print '@'":  "+tweet.text+" : "+str(
	elif choice == 'mentions':
		mentions = api.mentions(count=5)
		for mention in mentions:
			print '@'":  "+mention.text+" : "+str(
	elif choice == 'reply':
		api.update_status(status=sys.argv[2], in_reply_to_status_id=sys.argv[3])
	elif choice == 'retweet':
	else: pass


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

Tagged with: , ,
Posted in programming, Uncategorized
6 comments on “new and improved twitter cli, now with more twitter sauce
  1. […] my other home ← rolling your own and getting pwned new and improved twitter cli, now with more twitter sauce → […]

  2. Thanks, used this to integrate my twitter feed into GeekTool. :)


  3. ctb says:

    Nice! So, how often do you have GeekTool refresh your feed?

  4. Hi, sorry I didn’t enable email notifications so I only just read your reply.

    I have GeekTool set to refresh the the script every 5 minutes (300 seconds). I’ve been doing some work to get the formatting to align columns, also there was an issue with GeekTool reading the Unicode characters because the default output of python is ASCII. (It seemed to run find in the terminal output but in GeekTool it would print all text up to the unicode character then stop).

    I fixed this using ‘tweet.text.encode(“utf-8”)’ in the print command..

    Next I’m looking at doing a search term function so I can monitor hashtags.

    If your interested this is the work in progress script, hope you don’t mind I stripped some functions I didn’t need, I’m not a programmer so if I’m doing anything n00bish just let me know.

    Once I get it all finalised I’m planning to post it as a Geeklet so others can use it

  5. […] I started searching for an command line Twitter access using OAuth… then I stumbled on a blog post containing an Python script which could retrieve user timelines, post tweets etc. designed to run […]

  6. […] 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 pythonic […]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ 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.

Get every new post delivered to your Inbox.

Join 60 other followers

%d bloggers like this: