"Translator" Tweepy 3.5.0 Doc (6) Streaming with Tweepy

Source: Internet
Author: User
Tags auth error code stream api
Streaming with Tweepy

Tweepy is the authorization to process the Twitter stream API, connect, create/delete sessions, read information, and process routing information (. Partially routing Messagaes) has become easier.

This tutorial is designed to help you step through the first steps of using Twitter streaming with Tweepy. Some features of the Tweepy stream are not included in this tutorial, please see Tweepy's related source streaming.py for more information.

The Twitter stream also requires API authorization. Check out the authentication tutorial to get relevant information.


Summary

The Twitter streaming API is used to download Twitter messages in real-time, which is useful for getting tweets from large traffic, or for creating a site flow that requires real-time data. Check out the Twitter streaming API documentation

The streaming API is very different from the rest API because the rest API is used primarily to download data from Twitter, while the stream API is used to send messages to a persistent session. This requires the streaming API to be able to download data in real-time, more than the rest API. In

Tweepy, a tweepy. The stream instance establishes a stream session and sends the message to the Streamlistener instance. Stream Listener's On_data method can get all the information and invoke the function based on the type of information. The default Steramlistener can classify common Twitter messages and send them to the appropriate methods, but these methods are just stubs (what's this?).

Therefore, using the streaming API has the following three steps


1. Create a class that inherits from Streamlistener

2. Declare a Stream object with this class.

3. Use this Stream object to connect to the Twitter API


Step 1: Create the Streamlistener

The following simple stream listener will print its status. Streamlistener's On_data method makes it easy to pass data to the On_status method. Here's how to create a Mystreamlistener class that inherits from the Streamlistener class and how to override On_status.

Import tweepy
#override tweepy. Streamlistener to add logic to On_status
class Mystreamlistener (tweepy. Streamlistener):

    def on_status (self, status):
        print (Status.text)

Step 2: Create a flow (stream)

This flow requires an API. View authentication Tutorial Learn how to get API objects. Once we have an API object and status listener, we can create our own stream objects.

Mystreamlistener = Mystreamlistener ()
mystream = tweepy. Stream (auth = Api.auth, Listener=mystreamlistener ())

Step 3: Start a stream

Asynchronous Stream (Async streaming)

Unless the connection ends, the stream will not break, blocking the tread. The tweepy provides a simple async parameter to filter async and then the stream can execute the new thread continuously. Example:

Mystream.filter (track=[' python '], async=true)


Handling Errors

When using Twitter's streaming API, you must be careful about rate limit. If the client tries to connect to the stream API in the time window more than the limit count, it is subject to a 420 error. The amount of time the client waits after each error receives an exponential increase in the number of error attempts.

Tweepy Stream Listener can effectively send the error message to the On_error stub. We can use On_error to get a 420 error and cancel the stream connection.

Class Mystreamlistener (Tweepy. Streamlistener):

    def on_error (self, status_code):
        if Status_code = = 420:
            #returning False in On_data Disconnects the stream
            return False

For more information about the Twitter API error code, see Twitter Response Codes documendation.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.