skip to Main Content

Whenever a user logs in to my application and searches I have to start a streaming API for fetching data required by him.

Here is my stream API class

import tweepy
import json
import sys

class TweetListener(tweepy.StreamListener):

    def on_connect(self):
        # Called initially to connect to the Streaming API
        print("You are now connected to the streaming API.")

    def on_error(self, status_code):
        # On error - if an error occurs, display the error / status code
        print('An Error has occured: ' + repr(status_code))
        return False

    def on_data(self, data):
        json_data = json.loads(data)
        print(json_data)

Here is my python code file which calls class above to start Twitter Streaming

import tweepy
from APIs.StreamKafkaApi1 import TweetListener
consumer_key = "***********"
consumer_secret = "*********"
access_token  = "***********"
access_secret = "********"
hashtags = ["#ipl"]

def callStream():
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)
    api = tweepy.API(auth,wait_on_rate_limit=True)

    tweetListener = TweetListener(userid,projectid)
    streamer = tweepy.Stream(api.auth, tweetListener)
    streamer.filter(track=hashtags, async=True)
if __name__ == "__main__":
    callStream()

But if I hit more than twice my application return error code 420.
I thought to change API(using multiple keys) used to fetch data whenever Error 420 occurs.

How to get error raised by the on_error method of TweetListener class in def callStream()

2

Answers


  1. Per the Twitter error response code documentation

    Returned when an application is being rate limited for making too many
    requests.

    The Twitter streaming API does not support more than a couple of connections per user and IP address. It is against the Twitter Developer Policy to use multiple application keys to attempt to circumvent this and your apps could be suspended if you do.

    Login or Signup to reply.
  2. I would like to add onto @Andy Piper’s answer. Response 420 means your script is making too many requests and has been Rate Limited. To resolve this, here is what I do(in class TweetListener):

    def on_limit(self,status):
        print ("Rate Limit Exceeded, Sleep for 15 Mins")
        time.sleep(15 * 60)
        return True
    

    Do this and the error will be handled.

    If you persist on using multiple keys. I am not sure but try exception handling on TweetListener and streamer, for tweepy.error.RateLimitError and use recursive call of the function using next API key?

    def callStream(key):
        #authenticate the API keys here
        try:
            tweetListener = TweetListener(userid,projectid)
            streamer = tweepy.Stream(api.auth, tweetListener)
            streamer.filter(track=hashtags, async=True)
        except tweepy.TweepError as e:
            if e.reason[0]['code'] == "420":
                callStream(nextKey)
        return True
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search