There are many Twitter API libraries available for Python. I wanted to find out which one was the best and what the strengths and weaknesses of each are. However there are too many out there to find and review all of them. Instead here’s a bunch of the most popular Python Twitter API wrappers with a small review of each one, and some sample code showing off the syntax.
This is the library that I personally use in most of my Twitter scripts. It’s very simple to use, but is not up to date with the latest developments at Twitter. It was written by DeWitt Clinton and available on Google Code. If you just want the basic API functionality this does a pretty decent job.
import twitter api = twitter.Api('username', 'password') statuses = api.GetPublicTimeline() print [s.user.name for s in statuses] users = api.GetFriends() print [u.name for u in users] statuses = api.GetUserTimeline(user) print [s.text for s in statuses] api.PostUpdate(username, password, 'I love python-twitter!')
Twyt is a pretty comprehensive library that seems to be pretty solid and well organized. In some cases there is added complexity to parse the return json objects from the Python Twitter API. It is written and maintained by Andrew Price.
from twyt import twitter, data t = twitter.Twitter() t.set_auth("username", "password") print t.status_friends_timeline() print t.user_friends() return_val = t.status_update("Testing 123") s = data.Status() s.load_json(return_val) print s t.status_destroy(s.id)
An up to date, Python wrapper for the Twitter API. Supports Twitter’s main API, Twitter’s search API, and (soon) using OAuth with Twitter/Streaming API. It is based on the Python Twitter library and is actively maintained by Ryan Mcgrath.
import twython twitter = twython.setup(authtype="Basic", username="example", password="example") twitter.updateStatus("See how easy this was?") friends_timeline = twitter.getFriendsTimeline(count="150", page="3") print [tweet["text"] for tweet in friends_timeline]
Tweepy is a pretty compelling Python Twitter API library. It’s up to date with the latest features of Twitter and actively being developed by Joshua Roesslein. It features OAuth support, Python 3 support, streaming API support and it’s own cache system. Retweet streaming was recently added. If you want to use the most up to date features of the Twitter API on Python, or use Python 3, then you should definitely check out Tweepy.
import tweepy api = tweepy.API.new('basic', 'username', 'password') public_timeline = api.public_timeline() print [tweet.text for tweet in public_timeline] friends_timeline = api.friends_timeline() print [tweet.text for tweet in friends_timeline] u = tweepy.api.get_user('username') friends = u.friends() print [tweet.screen_name for f in friends] api.update_status('tweeting with tweepy')
It’s pretty clear from the syntax samples that there’s not much difference between any of these Python Twitter API libraries when just getting the basics done. The difference only start to show up when you get into the latest features. OAuth, Streaming, and the retweet functions really differentiate these libraries. I hope this overview helps you find and choose the library that’s right for your project.