Streaming APIs with Tweepy

We can use the tweepy library to connect to the Twitter API. In this example, we will use a streaming API to process data in real time.

In the same way as we have done before with the Twitter module, we can create our function for connecting with OAuth credentials:

def twitter_connection(file):
'''Create the object from which the Twitter API will be consumed,
reading the credentials from a file, defined in path parameter.'''
(CONSUMER_KEY,CONSUMER_SECRET,OAUTH_TOKEN,OAUTH_TOKEN_SECRET) = open(file, 'r').read().splitlines()
# We instanced the authorization manager
auth = tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN,OAUTH_TOKEN_SECRET)
return (tweepy.API(auth), auth)

The first thing we can do is create a class inherited from tweepy.StreamListener. This will be the class that is listening the flow of tweets and will process a tweet that matches the term we are looking for:

class StreamListener(tweepy.StreamListener):

'''When a Tweet matches our targetTerms it will be passed to this function'''
def on_data(self, data):
data = json.loads(data)
print(data['text'])
return True

# If we reach the limit of calls alert and wait 10 "
def on_limit(self, track):
print('[!] Limit: {0}').format(track)
sleep(10)

# In case of an error, interrupt the listener
# https://dev.twitter.com/overview/api/response-codes
def on_error(self, status):
print('[!] Error: {0}').format(status)
return False

We can create a function that uses the Tweepy API to extract information about trending topics from Twitter:

def getTrendingTopics(woeid=1):
trends = api.trends_place(1)[0]['trends']
# We extract the name of the trends and return them as a list
trendList = [trend['name'] for trend in trends]
return trendList

To tell the listener what our keywords will be and for it to be able to use them, we will add the following function to our project:

def streamAPI(auth):
# instantiate our listener
l = StreamListener()
# We start the streamer with the OAuth object and the listener
streamer = tweepy.Stream(auth=auth, listener=l)
# We define the terms that we want to track
targetTerms = [‘python’]
#We start the streamer, passing it our trackTerms
streamer.filter(track=targetTerms)

We'll call it, passing it our authentication auth object in the following way:

try:
streamAPI(auth)
except KeyboardInterrupt, e:
exit(1)

In this screenshot, we can see the execution of the twitter_stream.py script used to track Python terms when they appear in the Twitter timeline, and tweets tagged with Python:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset