pythontwittertweepy

How to stream tweets of members of twitter List using tweepy python module?


I'm working with the Tweepy Python module and need help figuring out how to stream the statuses from a specific Twitter list timeline.

Currently, I can retrieve statuses from a list timeline by running:

list_status = api.list_timeline(list_id=list_id, count=1)
print(list_status[0].text)

This works, but it only fetches the latest status on each execution. I could loop this code, compare the retrieved status with the previous one, and print only new statuses, but that feels inefficient.

What I'm looking for is a way to stream statuses from the list timeline so that my code block is triggered automatically whenever a new status is posted in the list.

I've successfully set up streaming for the user's home timeline, keywords, and hashtags, but I haven't found a way to stream statuses specifically from a Twitter list timeline.

Is there a method in Tweepy (or another approach) that allows streaming updates from a Twitter list in real-time?


Solution

  • Unfortunately this is not available in tweepy or in twitter API so we have to tweak something like polling or scheduling

    1. get members of list.
    2. get ids of all memebrs

    For example:

    Get ids

    list_id = 'your_list_id' 
    members = ...
    user_ids = ...
    

    and start polling for extracted IDs

    class MyStreamListener(tweepy.StreamingClient):
        def on_tweet(self, tweet):
            print(tweet.text)
    
        def on_errors(self, errors):
            print(errors)
    
    stream = MyStreamListener(bearer_token=bearer_token)
    
    rules = [tweepy.StreamRule(value="from:" + user_id) for user_id in user_ids]
    stream.add_rules(rules)
    
    # Start the stream
    stream.filter()