Write Your Own Linux Twitter Client In Less Time Than It Takes To Find One!
A command-line Twitter client in about 20 lines

Akkana Peck
Thursday, July 9, 2009 12:15:42 PM

figure 1
I know, I know. All the cool kids have been doing it for months now.
I'm way behind. But a couple of weeks ago, I finally took the plunge
and signed up for Twitter.
I was skeptical, but it's actually pretty interesting. It didn't take
me long to build up a list of people to follow. But I didn't want
to keep a tab open in my browser all day every day just to check on them.
I investigated the available Linux clients,
but none was quite what I was looking for. Either they required that
I install some big piece of infrastructure like Mono or Adobe Air,
or they had nasty bugs.
And then I found out about Python-Twitter. It provides Python
bindings for Twitter's public API, and it's super easy to use.
Turns out you can write your own Twitter client in less time than
it takes to read about the existing ones!
Install python-twitter
The first step is installing Python-Twitter. Cutting-edge distros like
Ubuntu 9.04 have it as a package; install python-twitter and you're done.
On older distros, you might have to get it from the source.
First, you'll need a package called SimpleJSON. Your distro
may have it already (it might, even if it doesn't have python-twitter.
But if not, get it from cheeseshop.python.org/pypi/simplejson.
Then go to
code.google.com/p/python-twitter,
click on Downloads
and grab the latest tarball. Expand it, build and install:
tar xvf python-twitter-0.6.tar.gz
cd python-twitter-0.6/
python setup.py build
sudo python setup.py install
Once it's installed, you're ready to start programming.
A command-line Twitter client in about 20 lines
To do anything with Twitter, you need to log in with a username
and password. That looks like this:
import twitter
username = "your_username"
password = "your_password"
api = twitter.Api(username=username, password=password)
Once you're logged in, you can get your "friends timeline" -- the
list of tweets you'd see if you went to twitter.com and logged in --
like this:
statuses = api.GetFriendsTimeline(username)
statuses is a list of the Python-Twitter status object.
A status represents one tweet, and has properties like user
(the author), text (the contents) and
created_at_in_seconds (the time when it was posted).
user is an object representing a Twitter user.
It has properties like name, screen_name and
profile_image_url.
Other Stories on LinuxPlanet
|
So you can print that list of tweets like this:
for s in statuses :
print s.user.name, "(", s.user.screen_name, ") :"
print s.text
print
Well, almost. There's one more thing you need to do. Tweets
come in as unicode, and Python's print can't handle
unicode characters unless you tell it how to encode them.
If you're not sure, UTF-8 is a good choice:
for s in statuses :
print s.user.name.encode("utf-8"), "(", s.user.screen_name, ") :"
print s.text.encode("utf-8")
print
Run it, and you'll see output something like this:
/var/lib/python-support/python2.6/twitter.py:10: DeprecationWarning: the md5 module is deprecated; use hashlib instead
import md5
donttrythis ( donttrythis ) :
I'm playing with a Canon 5D Mark II if you must know @el_es_gato. The footage it shoots is UNBELIEVABLE! I'm gonna make me a film.
Tim O'Reilly ( timoreilly ) :
Chevron patents on NiMH batteries holding back future of plug-in hybrid cars. http://bit.ly/awwJ3 Learned about in conversation at #aif09
PZ Myers ( pzmyers ) :
I have now been traveling for a full 24 hours...and still 2 hours from home.
Wall Street Journal ( WSJ ) :
Companies, Workers Tangle Over Law http://bit.ly/kya1p
Science News ( SciNewsBlog ) :
China's Internet Users Force Government to Back Down on Censorship: China's Internet Users Force Government to B.. http://tinyurl.com/qmorz9
Voilà! Your very own customizable Twitter client.
Add a loop and a time.sleep(300) to make it update every
five minutes (300 seconds).
Don't worry too much if you see that DeprecationWarning at the
beginning. That's a minor bug in python-twitter, but it doesn't hurt
anything; hopefully they'll fix it soon.
Next: A Twitter Window »