pythonapitwittertweepytwitter-api-v2

Twitter API v2 filtered stream in Tweepy


I am attempting to make a filtered stream on Twitter API v2, using tweepy library. Here is my code:

client = tweepy.StreamingClient(
bearer_token = "Bearer_Token"
)

client.add_rules(tweepy.StreamRule("tweets_count:1000"))

response = client.filter(
expansions="author_id",
user_fields="id"
)

The problem is that I am not able to set a limit for the tweets I want to receive. If I manually stop the stream (keyboard interrupt), the response will be empty. I want to be able to set a limit for the tweets or at least properly stop it (running in colab notebook), also I am not knowing where to find the received tweets along with their fields. I've searched alot but did not find an answer. Thanks for any help.


Solution

  • I think one of the problems you're having is that you've misunderstood the purpose of the "tweets_count" parameter.

    My reading of the API docs suggests that its purpose is to return tweets from users who have a number of tweets on file greater than the tweet_count you specify.

    It isn't intended to stop the stream once a maximum number of tweets have been returned.

    Streamers are normally done by creating a subclass of StreamingClient and overriding one of the methods.

    It's up to you how you process the tweets as they arrive, and in your case "response" is a data stream, not, say, a list or tuple.

    Here's some skeleton code that I used as a basis for a streamer:

    import tweepy
    
    bearer_token = "some token"
    
    class MyClient(tweepy.StreamingClient):
    
        def on_tweet(self, tweet):
            print(tweet)
            # or call some other processing
    
        def on_connect(self):
            print("connected to stream")
    
    
    
    client = MyClient(
    bearer_token=bearer_token,
    )
    
    client.add_rules(tweepy.StreamRule("tweets_count:1000"))
    
    client.filter(expansions="author_id", user_fields="id")