Youtube API 3 get latest videos

2019-01-23 16:46发布

问题:

So I am trying to grab 10 latest videos from user uploads.

Now the only problem is that the only date visible in the playlistitems request is publishedAt which is the date when the video was uploaded - not the date of when it was made public, which makes a huge difference.

I noticed that I can grab the correct date via the video request, but it just does not seem to be the best place to do it.

Let me show you an example of what I am dealing with.

Let's take Maroon5 channel.

forUserName: Maroon5VEVO

GET https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=Maroon5VEVO&key={YOUR_API_KEY}

https://developers.google.com/youtube/v3/docs/channels/list#try-it

Here is where we grab the:

uploadsId: UUN1hnUccO4FD5WfM7ithXaw

So we can query:

GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUN1hnUccO4FD5WfM7ithXaw&maxResults=50&key={YOUR_API_KEY}

https://developers.google.com/youtube/v3/docs/playlistItems/list#try-it

and then we can have a look at some videos.

Let's grab the latest one. For me it is this one:

"title": "Maroon 5 - This Summer's Gonna Hurt Like A ... (Explicit)", "videoId": "Wa64gOwuIyE"

And most importantly :

"publishedAt": "2015-06-01T17:41:58.000Z",

Now let's grab more details of this video, by running this query:

GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=Wa64gOwuIyE&key={YOUR_API_KEY}

https://developers.google.com/youtube/v3/docs/videos/list#try-it

Here we get more detailed view with a date that is ... different !

"publishedAt": "2015-06-01T20:00:01.000Z",

That means that the publishedAt date in playlists is actually the date of the upload - not when the video was made public.

In our list of 10 latest items we want the LATEST published videos , and not latest uploaded vids.

If you know a way how to approach it , please share.

Here is my snippet for now (working with a wrong publishedAt date)

 $.get(
        "https://www.googleapis.com/youtube/v3/playlistItems",{
        part : 'snippet',
        maxResults : 10,
        playlistId : UPLOADS_PID,
        key: YOUR_API_KEY},
        function(data) {

            $.each( data.items, function(i, item ) {                

                (...)

            });
        }
    );

回答1:

I believe you can use /search endpoint. As you want to grab 10 latest videos from user uploads, you can use channel id instead of playlist id.

Request:

GET https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={CHANNEL_ID}&maxResults=10&order=date&type=video&key={YOUR_API_KEY}