TweetyPy: Python-based CLI client for Twitter | Comments (14)
Posted in Code on 24th March 2007, 2:35 am by Stuart
TweetyPy is now hosted on launchpad.net

Having recently created a Twitter bundle for Textmate I came to the conclusions that using PHP for the command was fairly limiting. Having wanted to take a look at Python for a while I decided this was the ideal opportunity to build something and learn at the same time. The goal was to write a command line Twitter client. I thumbed through Mark Pilgrim’s excellent book;”Dive into Python” on the train home and by the end of the evening I had a basic client up and running complete with nice argument handling using getopt.
This evening I have added support for timelines of “you and your friends”, the “public timeline” and lists of “friends” and “followers”. To make these look nice I utilised a cunning module called TextFormatter written by Hamish Lawson that wraps text into columns so that the output is neat and tidy.
TweetyPy can currently do the following:
- Update your status
- Display the public timeline
- Display the timeline of you and your friends
- Display your followers
- Display your friends
Usage Examples:
- Update your status
python tweetyPy -t 'update' -u blah@blah.com -p password -s "Sitting on the toilet"- Get the timeline of you and your friends
python tweetyPy -t 'friends_timeline' -u blah@blah.com -p password- Show list of followers
python tweetyPy -t 'followers' -u blah@blah.com -p password- Show help
python tweetyPy -h
Note: if you make tweetyPy executable that should do away with the need to call it from Python.
Eventually (time permitting) I will roll TweetyPy into the TextMate bundle and add all of the features that it has into TextMate. This will probably mean I will add a customisable output mode to TweetyPy so that you can view the time-lines in TextMate’s HTML Output window. That should certainly make the bundle a little more interesting than just being able to update your status.
Needless to say this is my first foray into the wonderful world of Python, therefore if you have any comments or suggestions leave a comment below. I’m sure there’s lots of room for improvement.
Releases:
- TweetyPy v0.22
- Fixed removal of relative_created_at from API. Will improve formatting in due course.
- TweetyPy v0.21
- Added reverse of feed, Basic HTML mode. Socket timeout. Better error handling. Other small fixes
- TweetyPy v0.2
- Fixed Usage which indicated wrong file + updated examples
- TweetyPy v0.1
- Initial Release
Todo list
- Improve on the time formatting now that relative_created_at has been removed from the API.
- Add a follow mode like tail -f (Will need to observer headers and only update with new items)
Add html mode - required if I am going use this from TextMate- Better authentication storage (keychain for mac? etc)
Reverse order so that most recent twitter is shown first (makes more sense on the CLI)Added User-Agent

*Starts pondering what sort of Django-powered apps he could build with this.*
[...] you want the result of some real coding to do what I am doing, TweetyPy was released today by Muffin Research Labs. They created a TextMate bundle for Twitter that allows [...]
This is really cool. Combined with Geektool, I can now get the latest tweets on my desktop to check when I feel like it. Awesome.
BTW - your example commands have the program name wrong. That one threw me for a second.
Thanks for doing this, it’s really appreciated.
- Neil.
This is great, I was using curl and then stripping out the tags to Twitter from the cli, but this is much cleaner. I was wondering if you could post a quick comment on how to edit the code to remove the need for -u and -p, to just embed my authorization information into the script so I can post without typing the extra data. Thanks for the code, much cleaner than what I had come up with my complete lack of programming skills.
@Neil: well spotted, thats now fixed.
[...] this hard core but I still think it’s pretty cool. Muffin has some slick python scripts for sending twitters via command line, if you are so [...]
Nice work Stuart! Please do let us Twitter devs know if there’s anything you’d like out of the API.
Nice tool!
An improvement could be something like this:
$ ./tweetyPy -t friends_timeline -u user@mail -p
$ Password:
By passing password as a value for an argument, it keeps stored in bash history, that isn’t a good idea…
@xoan: Good point. I will probably still allow passwords to be sent in plaintext but I can also add the option to prompt for a password using getpass. I will add that with a load of other improvements to the next release. Cheers for your comment!
[...] TweetyPy: Python-based CLI client for Twitter | Muffin Research Labs by Stuart Colville (tags: python shell twitter cli) [...]
I created tw.py (http://snippets.dzone.com/posts/show/4150), based on your tweetyPy. It sets and views the current status of users.
Hi, Stuart,
I’ve been playing with tweetyPy for the last day or so, and I’m really kind of enamored. I’m working on a bash-script concoction of watch, screen, and tweetyPy to build a command-line shadow of the variety of GUI Twitter tools. It might be easier to pick up Python, since I’ve been meaning to anyway, but, for the moment, this is it. For those folks who ssh into their boxes and want to use screen with a custom .screenrc which maintains a region with the timelines, and a region for tweeting, I’ll be happy to make it available when I’ve finished.
Yesterday, thinks worked swimmingly, basically. Today, however, it seems that there’s some Unicode business mucking up the timeline. I’m not sure if there’s something that could be changed in the Python, e.g. some headers business, or something else. Here’s the error I’m getting:
Traceback (most recent call last):
File "/home/muraii/utilities/tweetyPy", line 234, in ?
main(sys.argv[1:])
File “/home/muraii/utilities/tweetyPy”, line 228, in main
print result
UnicodeEncodeError: ‘latin-1′ codec can’t encode character u’\u2014′ in position 883: ordinal not in range(256)
Actually, this is a bit different, as it previously was in position 1014. I wager someone used an em-dash in their tweet, and the position change is due to new tweets pushing that offending character around.
Have you had any issues like this?
Thanks for your efforts on this.
Daniel
@Daniel: that’s great - I’ve kind of let this slip due to being up to my eyballs at the moment. If you could post this as a bug in launchpad then I’d be realy grateful I’ll see if I can release an update asap. https://launchpad.net/tweetypy
@Stuart:
Done. Hopefully it wasn’t too anecdotal. I’ve never filed a bug before.