Is it possible to get “type” (photo, video, animated_gif) in attachments array inside each tweet. For example,

"data": [
        {
            "text": "tweet data",
            "id": "14752352352352",
            "attachments": {
                "type": "video" 
                "media_keys": [
                    "7_14715424479939"
                ]
            }
        },
    ]

Or is it possible to get tweet id inside includes objects for each media item. For example.

"includes": {
        "media": [
            {
                "tweet_id": "14752352352352",
                "preview_image_url": "https://pbs.twimg.com/",
                "media_key": "7_1471542447993",
                "type": "video"
            },
            {
                "tweet_id": "14752352352352",
                "media_key": "3_1471539498605",
                "type": "photo",
                "url": "https://pbs.twimg.com/media"

            },
     ]
}

The problem is that i want to know, which media belongs to which tweet. So if “type” is mentioned inside attachment array, then i can just filter out all tweets which contains media and then perform additional request if its video based on that tweet id. But it just attachments object with media_keys and I can’t find an end point to retrieve that media via media_key.

Similarly, includes object just contains media array with media_key, url and type but doesn’t tell which image/video belongs to which tweet. So again, i can’t perform an additional request if it’s media type is video because tweet id is not available.

Any help would be appreciated.

The media_key is part of the tweet, and that matches up with the media_key in includes. But unfortunately there’s no reverse, there’s no tweet IDs in media objects, so you have to go the other way - look through all the tweets and match on media_key.

I don’t know what code / library you’re using but this is the way you can match everything up in Python for example: User include in list of tweet · Issue #6 · twitterdev/open-evolution · GitHub

@IgorBrigadir Thanks for the reply.

I have thought about this but the process is painful. I have to compare and match media key and then filter out all those tweets and then make additional query. I was planning to try that but how easy it would be if media items just go inside each tweet where they belong. Otherwise “type” or “tweet_id” can also help similar to examples above.

I’m using Vue.js. Any idea/link how to do this with javascript?

I don’t know javascript very well but you should be able to use javascript objects sort of like the python dicts - and build your own Key / Value object with tweets that reference media keys and that way you’ll be able to look up tweet ids by media key. I think implementing a version of this in javascript might be easier than the twarc variant TwitterAPI/TwitterAPI.py at master · geduldig/TwitterAPI · GitHub

1 Like

It was rather easier than i thought with the help of StackOverFlow.

Here is what I did by using Axios if anyone needs.

let tweets = res.data.data;
let media = res.data.includes.media;

let props = ["id"];
let result = tweets.filter(function (o1) {
    // filter out items in media
    return media.some(function (o2) {
        if (o2.type == "video") {
            return o1.attachments.media_keys.includes(
                o2.media_key
            );
        }
    });
})
.map(function (o) {
    // use reduce to make objects with only the required properties
    return props.reduce(function (newo, id) {
        newo[id] = o[id];
        return newo;
    }, {});
});

console.log(result);

It will filters out tweets if media type is video and returns a new array of objects with only tweet id’s. Then I can loop over that new array and make additional requests.

1 Like

Bumping into this topic:-
Is the media_key to tweet_id relationship 1:1?
can someone please comment on this?