user18765140
user18765140

Reputation: 53

Obtaining tweets image urls with tweepy v2 on Twitter API v2

what is an elegant way to access the urls of tweets pictures with tweepy v2? Twitter released a v2 of their API and tweepy adjusted their python module to it (tweepy v2).

Lets say for example I have a dataframe of tweets created with tweepy, holding tweet id and so on. There is a row with this example tweet: https://twitter.com/federalreserve/status/1501967052080394240

The picture is saved under a different url and the documentation on tweepy v2 does reveal if there is a way to access it. https://pbs.twimg.com/media/FNgO9vNXIAYy2LK?format=png&name=900x900

Reading thought the requests json obtained through tweepy.Client(bearer_token, return_type = requests.Response) did not hold the required links.

I am already using the following parameters in the client:

client.get_liked_tweets(user_id, 
                             tweet_fields = ['created_at', 'text', 'id', 'attachments', 'author_id', 'entities'], 
                             media_fields=['preview_image_url', 'url'],
                             user_fields=['username'],
                             expansions=['attachments.media_keys', 'author_id']
                             )

What would be a way to obtain or generate the link to the picture of the tweet? Preferably through tweepy v2 itself?

Thank you in advance.

Upvotes: 5

Views: 2402

Answers (1)

Mickaël Martinez
Mickaël Martinez

Reputation: 1843

The arguments of get_liked_tweets seem to be correct.

Have you looked in the includes dict at the root of the response?

The media fields are not in data, so you should have something like that:

{
    "data": {
        "attachments": {
            "media_keys": [
                "16_1211797899316740096"
            ]
        },
        "author_id": "2244994945",
        "id": "1212092628029698048",
        "text": "[...]"
    },
    "includes": {
        "media": [
            {
                "media_key": "16_1211797899316740096",
                "preview_image_url": "[...]",
                "url": "[...]"
            }
        ],
        "users": [
            {
                "id": "2244994945",
                "username": "TwitterDev"
            }
        ]
    }
}

Upvotes: 5

Related Questions