Community Data Science Course (Spring 2016)/Day 6 Lecture

From CommunityData

Code to get a single Tweet

import encoding_fix
import tweepy
from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET

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

api = tweepy.API(auth)

public_tweets = api.home_timeline(count=1)

single_tweet = public_tweets[0]

print(single_tweet._json)

All the lines up to tweepy.API(auth) are "boilerplate" meaning they are always the same. They take care of authenticating with Python.

Once we have access to Twitter's API, we can use it to do many things. The example above gets the first tweet from your timeline. You can print it as json by doing public_tweets[0]._json.