Friday, January 18, 2008

fun with python: been twitter'ing with python

I'm not sure why, but python developer seems have ways to write python modules, to integrate with web service. Which to me interesting. There is one from google to integrate, google service. There is one for flickr, which I covered. Etc. Even twitter have one.

The twitter python binding can be found in
http://code.google.com/p/python-twitter/
Before you install it, you need to install simplejson.
http://cheeseshop.python.org/pypi/simplejson

To install simplejson, it is just get the tar.gz file. Then uncompress it,
Then in the same directory
On ubuntu:
sudo python setup.py install
on other linux as root:
python setup.py install
same on windows. just:
python setup.py install

The process of installing the python-twitter is the same cycle of python setup.py

There is a few standard thing we can do with the python-twitter module.

here I assume that, you run it, in a python shell, it is just a follow along tutorial here, and the >>> is not shown:
before that, import the module
import twitter
start using it, you need to authenticate yourself, using your twitter username and password.
api=twitter.Api("username","password")
to view public time line
status=api.GetPublicTimeline()
for s in status:
print s.text
or you can just
status=api.GetPublicTimeline()
print [s.text for s in status]
to view a user status, just replace user with the username of the user
userStatus=api.GetUserTimeline("user")
then read it by
print [t.text for t in userStatus]
Here is the important one, to post your own status using the python-twitter module
posting=apt.PostUpdate("your status")
There you go, few way to use python to interface with twitter. You can use this with a gui front end, or integrate with django, etc.

No comments:

Post a Comment