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.
Once you have the keys, plug them into this script, or one like it:
#!/usr/bin/env python """ twittercli.py 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 bit.ly with your own username and key, provided by bit.ly 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': api.update_status(sys.argv) elif choice == 'withShort': data = bit.shorten(sys.argv) api.update_status(sys.argv+data['url']) elif choice == 'tweets': tweets = api.home_timeline(count=7) for tweet in tweets: print '@'+tweet.author.screen_name+": "+tweet.text print elif choice == 'mentions': mentions = api.mentions(count=5) for mention in mentions: print '@'+mention.author.screen_name+": "+mention.text print else: pass # the first argument passed from the cli indicates choice, the others provide # further data for update or update with a shortened url do_the_twitter(sys.argv)
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:
PATH=$PATH:"/path/to/the/script/folder" alias twitter='python /path/to/the/script/folder/twittercli.py'
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:
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:
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.