Playing around with Twitter API using Tweepy

Yes, this is the kind of stuff I find myself doing when I’m bored.  Just a quick tutorial on using the Twitter python library called Tweepy.  Examples include authentication, print your statuses, and print your mentions.

First, you need to authenticate using OAuth as simple authentication has been disabled by Twitter.  I will not repeat what has already been done, so this is the best way to do it.  If you have no problems with that example, then at the end you will have a simple python script which allows you to update your status via the command line.  However, the most important piece is the authentication:

   1:  #!/usr/bin/env python
   3:  import sys
   4:  import tweepy
   6:  CONSUMER_KEY = 'Your stuff'
   7:  CONSUMER_SECRET = 'Your stuff'
   8:  ACCESS_KEY = 'Your stuff'
   9:  ACCESS_SECRET = 'Your stuff'
  11:  auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  12:  auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
  13:  api = tweepy.API(auth)

This will allow you to authenticate against Twitter and be able to work with the API.  Now you can add to your python script the code below.

How about printing your status updates?

   1:  statuses = tweepy.Cursor(api.user_timeline).items()
   3:  for status in statuses:
   4:          print " "
   5:          print status.text

How about printing your mentions?

   1:  mymentions= tweepy.Cursor(api.mentions).items()
   3:  for status in mymentions:
   4:          print " "
   5:          print status.text


The Tweepy API reference has a ton of great information.  As well as the Twitter API wiki.


One Comment

Leave a Reply