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 bit.ly 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 bit.ly 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
"""
twittercli.py
Created by Chad Black on 04/22/2011
"""
import sys
import tweepy
import bitly_api
import keyring
CONSUMER_KEY = 'RwJyUDoHClBB6REoccKDAg'
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':
api.update_status(sys.argv[2])
elif choice == 'withShort':
data = bit.shorten(sys.argv[3])
api.update_status(sys.argv[2]+data['url'])
elif choice == 'tweets':
tweets = api.home_timeline(count=20)
for tweet in tweets:
print '@'+tweet.author.screen_name+": "+tweet.text+" : "+str(tweet.id)
print
elif choice == 'mentions':
mentions = api.mentions(count=5)
for mention in mentions:
print '@'+mention.author.screen_name+": "+mention.text+" : "+str(mention.id)
print
elif choice == 'reply':
api.update_status(status=sys.argv[2], in_reply_to_status_id=sys.argv[3])
elif choice == 'retweet':
api.retweet(id=sys.argv[2])
else: pass
do_the_twitter(sys.argv[1])


Thanks, used this to integrate my twitter feed into GeekTool.
Result: https://skitch.com/gmanual/fue7t/fullscreen
Nice! So, how often do you have GeekTool refresh your feed?
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..
http://twitpic.com/6dk9zv
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.
http://dl.dropbox.com/u/857964/twittercli.py
Once I get it all finalised I’m planning to post it as a Geeklet so others can use it http://www.macosxtips.co.uk/geeklets/