Muffinresearch Labs by Stuart Colville

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

TweetyPy output

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:

  1. Update your status
  2. Display the public timeline
  3. Display the timeline of you and your friends
  4. Display your followers
  5. 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

  1. Improve on the time formatting now that relative_created_at has been removed from the API.
  2. Add a follow mode like tail -f (Will need to observer headers and only update with new items)
  3. Add html mode - required if I am going use this from TextMate
  4. Better authentication storage (keychain for mac? etc)
  5. Reverse order so that most recent twitter is shown first (makes more sense on the CLI)
  6. Added User-Agent

Post Tools

Comments: Add yours

1. On March 24th, 2007 at 4:07 am Jeff Croft said:

*Starts pondering what sort of Django-powered apps he could build with this.*

2. On March 24th, 2007 at 4:23 am Twitter - what do you think? - Page 4 - The Apple Blog Community said:

[...] 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 [...]

3. On March 24th, 2007 at 2:14 pm Neil Ford said:

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.

4. On March 24th, 2007 at 4:47 pm Divigation said:

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.

5. On March 24th, 2007 at 6:47 pm Stuart Colville said:

@Neil: well spotted, thats now fixed.

6. On March 26th, 2007 at 1:32 am Twitter Hacks » Blog Archive » Hardcore Geeks: Command Line Twitter said:

[...] 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 [...]

7. On March 26th, 2007 at 3:08 am Alex Payne said:

Nice work Stuart! Please do let us Twitter devs know if there’s anything you’d like out of the API.

8. On March 29th, 2007 at 4:54 pm xoan said:

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…

9. On March 30th, 2007 at 7:42 am Stuart Colville said:

@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!

10. On April 6th, 2007 at 11:17 am links for 2007-04-06 « Breyten’s Dev Blog said:

[...] TweetyPy: Python-based CLI client for Twitter | Muffin Research Labs by Stuart Colville (tags: python shell twitter cli) [...]

11. On June 17th, 2007 at 7:12 am Andrew Pennebaker said:

I created tw.py (http://snippets.dzone.com/posts/show/4150), based on your tweetyPy. It sets and views the current status of users.

12. On August 7th, 2007 at 3:37 pm Daniel Black said:

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

13. On August 7th, 2007 at 3:41 pm Stuart Colville said:

@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

14. On August 7th, 2007 at 3:57 pm Daniel Black said:

@Stuart:

Done. Hopefully it wasn’t too anecdotal. I’ve never filed a bug before.







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Standalone mac battery charger|(0)

Got a spare mac battery? I’ve often wondered why up until now no-one’s produced a standalone charger so that you can charge batteries without having to plug them into the mac. Fortunately Fastmac.com have produced a standalone charger that allows you to do just that. and it’s compatible with iBooks, Powerbooks, macbooks and Macbook Pros. It’s also 110/200v. Exactly what I was looking for!

Django Admin Ominigraffle Stencil|(0)

Colleague Alex Lee has created a nice stencil for omingraffle with the Django Admin UI components, perfect for wireframing customised admin screens. For more details and to download the stencil see Alex’s Blog csensedesign.co.uk

Photos on Flickr

© Copyright 2004-08 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by 1&1 This page was baked in 1.020s.