Twitterminal - A Terminal-based Twitter client in Python
27/05/2008
So, I've just started looking at Python, and I love it. Last night I was playing around with django, which is ridiculously cool, and tonight I started a Terminal twitter client - I'm dubbing it Twitterminal. Imaginitive, eh?
It's crazy-simple, and I'm not bothered about people using it or ripping it off so go for your life! The Twitter class, twit.py:<!--more-->
import urllib2 as urllib import base64 import xml.dom.minidom
class Twitter: public = 'http://twitter.com/statuses/public_timeline.rss' friends = 'http://www.twitter.com/statuses/friends_timeline.xml' action = 'public' actions = ['public','friends'] items = []
def __init__(self):
pass
def auth(self,username,password):
self._username = username
self._password = password
self._b64 = base64.encodestring('%s:%s' % (self._username,self._password))
self._authhead = 'Basic %s' % self._b64
def setAction(self,action):
if action not in self.actions:
return False
self.action = action
def fetch(self):
if self.action == 'public':
url = self.public
self._req = urllib.Request(url)
self._handler = urllib.urlopen(self._req)
self.parsePublic()
elif self.action == 'friends':
url = self.friends
self._req = urllib.Request(url)
self._req.add_header('Authorization',self._authhead)
try:
self._handler = urllib.urlopen(self._req)
except IOError, e:
return False
self.parseFriends()
def parsePublic(self):
txt = self._handler.read()
feed = xml.dom.minidom.parseString(txt)
statuses = feed.getElementsByTagName('item')
for status in statuses:
self.items.append(status.getElementsByTagName('description')[0].childNodes[0].data)
def parseFriends(self):
feed = xml.dom.minidom.parseString(self._handler.read())
statuses = feed.getElementsByTagName('status')
for status in statuses:
self.items.append('%s: %s' % (
status.getElementsByTagName('screen_name')[0].childNodes[0].data,
status.getElementsByTagName('text')[0].childNodes[0].data))
def printall(self):
for item in self.items:
print '%sn' % item</pre>
and twitter.py
#!/usr/bin/python import sys from twit import Twitter
twit = Twitter() if len(sys.argv) == 1: twit.setAction('public') else: twit.setAction(sys.argv[1]) if (twit.action == 'friends'): # needs authentication - check sys - 2 is username - 3 is password if len(sys.argv) == 4: twit.auth(sys.argv[2],sys.argv[3]) else: twit.setAction('public') if twit.fetch() is False: print 'Username or password was incorrect' twit.printall()
To use it, you'll need to go to the directory that twit.py and twitter.py are in and run chmod +x ./twitter.py so that you can execute it, then use the following commands to access it:./twitter.py # this will retrieve the last 20 results from the public timeline
./twitter.py public # as ./twitter.py
./twitter.py friends username password # retrieves the last 20 tweets by your friends
there is pretty rudimentary functionality for checking if you've put the correct username or password in, and hopefully it will become more friendly once I get a bit more used to sys.argv and lists in general. This has only been used in python2.5.2 in OSX, so YMMV in other environments. Let me know in comments!
Also, follow me on twitter.
Also also, I've just noticed that the code is too wide for my layout. It's only temporary anyway - copying still works.