Skip to content
Advertisement

Tweepy does not return includes object using StreamingClient

I am using tweepy 4.10.1 to fetch Tweets using StreamingClient, but I am unable to load any media info or even includes object itself. I have tried the similar code using get_tweet() method and media and includes were received fine.

The code:

class TweetPrinter(tweepy.StreamingClient):
    def on_tweet(self, tweet):
        print(tweet.includes)


streaming_client = TweetPrinter('bearer-token')

streaming_client.add_rules(tweepy.StreamRule("from:xxxyyy"))

streaming_client.filter(tweet_fields=['author_id', 'created_at'],
                        media_fields=['preview_image_url', 'url'],
                        expansions=['attachments.media_keys'])

print(tweet.includes)

I am receiving following error:

raise AttributeError from None

When I use the same tweet id with get_tweet() method, I can retrieve media from includes fine.

client = tweepy.Client(config.BEARER)

ID = 'xxxxyyyy'

tweet = client.get_tweet(ID,
                         tweet_fields=['author_id', 'created_at'],
                         media_fields=['preview_image_url', 'url'],
                         expansions=['attachments.media_keys'])

print(tweet.includes)

According to Google, official docs and FAQ, I have tried all recommended steps I found

What am I missing here?

Advertisement

Answer

EDIT

I have discovered that using on_data() is the proper way how to retrieve all data from tweet. It covers all tweet, includes and other objects.

So the proper code should looks like this:

import orjson

class TweetPrinter(tweepy.StreamingClient):
    def on_data(self, raw_data):
        # Received as bytes, needs to be loaded by json (I use orjson)
        raw_data = orjson.loads(raw_data)
        print("data")
        print(raw_data.get('data', None))
        print("media")
        print(raw_data.get('includes', None))


Deprecated solution

The TweetPrinter class should contain function to handle when includes received like this:

class TweetPrinter(tweepy.StreamingClient):
    def on_tweet(self, tweet):
        print(tweet.data)

    def on_includes(self, includes):
        print(includes)

Thanks to this article, it helped me to find the proper solution :)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement