Stream Tweets asynchronously with Tweepy AsyncStream

Viewed 858

I'm trying to run AsyncStream Tweepy, but I ran into a problem

My code

from __future__ import absolute_import, print_function
from tweepy.streaming import Stream
from tweepy import OAuthHandler
from tweepy import Stream
from pprint import pprint
from tweepy.asynchronous import AsyncStream 
import asyncio

async def main():

    stream = StdOutListener(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    await stream.filter(follow=['1082189695252074496'])
    await asyncio.sleep(1.5)


class StdOutListener(AsyncStream):
    
    
    async def on_status(self, status):
        print(status_json)

    async def on_error(self, status):
        print(status)
            
if __name__ == '__main__':
    asyncio.run(main())

When I run it in .py file, it doesn't work and returns the error "An HTTP: 420 error occurred in the stream". I also run the code in Jupyter Notebook, only instead of async io.run(main ()), I write await main(), it also returns this error, BUT the stream works and it returns a response.

Why does it work in Jupyter Notebook, but does not work in the .py file. How can this be fixed?

1 Answers

According to the Tweepy documentation section on Handling Errors:

If clients exceed a limited number of attempts to connect to the streaming API in a window of time, they will receive error 420. The amount of time a client has to wait after receiving error 420 will increase exponentially each time they make a failed attempt.

Tweepy’s Stream Listener passes error codes to an on_error stub. The default implementation returns False for all codes, but we can override it to allow Tweepy to reconnect for some or all codes...

Here's another reference to the Twitter API documentation section on HTTP Error Codes.

  1. The first isssue is that print(status_json) should be print(status._json).

  2. The second issue is that the on_status method needs to conditionally return True or False based on status._json, like so:

    async def on_status(self, status):
        if hasattr(status, "_json"):
            print(status._json)
            # returning non-False continues the stream
            return True
        else:
            # returning False disconnects the stream
            return False
    
  3. The third issue is that the on_error method needs to conditionally return True or False based on the value of status, like so:

    async def on_error(self, status):
        if status == 420:
            # returning False disconnects the stream
            return False
        else:
            # returning non-False continues the stream
            return True
    
Related