Search code examples
twittertweepy

How to search GIFS using tweepy?


I am using tweepy to search for popular tweets that contain GIFS.Twitter gave some standard operators to filter the tweets.Click here to see it.
They have an operator that looks for tweets containing "puppy" and vine so they should have one for GIFS. Could you please help me search for popular tweets the contain GIFS?

I tried "filter:animated_gif" and "filter:gif"

api = tweepy.API(auth)
results = api.search(q=" filter:gif", lang="en")
for tweet in results:
    #rpp is recent public tweet
    print(f"{tweet.user.name}:{tweet.text}")

Solution

  • Seems like filter:images should work, as in:

    api = tweepy.API(auth) results = api.search(q=" filter:images", lang="en")
    

    This will bring back all images, but you can still tell which of them are gifs by looking at extended_entities/media/type, like this:

    {
        ...
        "extended_entities": {
            "media": [
                {
                    "id": 1182059952640122881,
                    "id_str": "1182059952640122881",
                    "indices": [
                        207,
                        230
                    ],
                    "media_url": "http://pbs.twimg.com/tweet_video_thumb/EGeFQ5xUYAE-94I.jpg",
                    "media_url_https": "https://pbs.twimg.com/tweet_video_thumb/EGeFQ5xUYAE-94I.jpg",
                    "url": "...",
                    "display_url": "pic.twitter.com/2wZccP1uMn",
                    "expanded_url": "https://twitter.com/NASAInSight/status/1182059967408271361/photo/1",
                    "type": "animated_gif",
                    "sizes": {
                        "thumb": {
                            "w": 150,
                            "h": 150,
                            "resize": "crop"
                        },
                        "large": {
                            "w": 1008,
                            "h": 956,
                            "resize": "fit"
                        },
                        "small": {
                            "w": 680,
                            "h": 645,
                            "resize": "fit"
                        },
                        "medium": {
                            "w": 1008,
                            "h": 956,
                            "resize": "fit"
                        }
                    },
                    "video_info": {
                        "aspect_ratio": [
                            252,
                            239
                        ],
                        "variants": [
                            {
                                "bitrate": 0,
                                "content_type": "video/mp4",
                                "url": "https://video.twimg.com/tweet_video/EGeFQ5xUYAE-94I.mp4"
                            }
                        ]
                    },
                    "ext_alt_text": null
                }
            ]
        },
        ...
    }
    

    Notice that type is "animated_gif".