Getting Started with Tweepy

Tweepy is a "wrapper" written in Python that makes it easier to work with the Twitter API.

You can install it on your own system by going to your Terminal/command-line and typing in:

    pip install tweepy

Registering a Twitter Application

(if you've already done this, you can go ahead and do it again and create a new app. Or just use your old credentials)

Assuming you've already created a Twitter account, the next thing you need to do is register an "application" with Twitter. You're basically telling Twitter that you want to create a new app/interface/site using Twitter data, and so you need to go through Twitter's authentication process.

Note that this is not specific to using Tweepy, this is the standard process needed for any third-party code/app to call Twitter on a user's behalf. Once you go through this process, you can use the credentials for any other program you might write.

Setting up the app

Visit https://apps.twitter.com/

img

Click Create a New App button

Fill out the fields, scroll to the bottom, read the Developer Agreement agree to them, and then click the Create your Twitter application button.

Configuring your app

If you plan to send tweets via the command line, change the Access level to Read and Write

img

Now click the Keys and Access Tokens tab (near the beginning of the page) and you should see this:

img

Take note of two of those fields, Consumer Key (API Key) and Consumer Secret (API Secret), which we'll copy later.

Create an access token

Scroll down until you see the subheading, Your Access Token.

img

Click the Create my access token button, which should cause a page refresh of the settings page. Below is a screenshot and the highlighted fields that you need to copy for the next step: Consumer Key (API Key), Consumer Secret (API Secret), Access Token, and Access Token Secret

img

Working with Tweepy

The easiest way to do this is to start up iPython from the command-line, so you can play with the commands interactively.

From the Tweepy docs, I've adapted code from the intro.

import tweepy

CONSUMER_KEY = "XXX"
CONSUMER_SECRET = "XXX"
ACCESS_TOKEN = "XXX"
ACCESS_TOKEN_SECRET = "XXX"

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

Once that api variable has been set to the object returned by tweepy.API(auth), you're free to make some Twitter API calls. To print out the last 20 tweets by @Stanford:

stanford_tweets = api.user_timeline('stanford')
for tweet in stanford_tweets:
    print( tweet.created_at, tweet.text)

To send a Tweet from your account (assuming you allowed your app to have read/write permissions):

api.update_status(status = "Hey, I'm tweeting with Tweepy!")

If these commands work, then you're all set up with Twitter and Tweepy. We'll expand on the use cases in other tutorials. Some of the extensions will be quite simple, such as making the authentication process a one-line command.