29

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 ) {                

                (...)

            });
        }
    );
Proqb
  • 335
  • 1
  • 3
  • 7
  • What's your use case? The `publishedAt` date for playlistItems should be when the video was added to the playlist, which is also when the video was uploaded, you're correct. But I doubt that the videos will be uploaded so far in advance before they're actually published that it'll make a huge deal. Worst case, you could get the last 15-20 videos, use videos.list and get the `publishedAt` value for each, then sort it that way. – not_a_bot Aug 18 '15 at 16:43
  • Unfortunately in my use case, we are dealing with a channel where there are many private videos, that are being internally reviewed and then made public (unfortunately it is in the same playlist). So for example we have a video that was uploaded 5 months ago, but made public just now - so you can imagine that with the playlistItem request it is nowhere near the first 10 results. The videos.list will work, but the problem is that with 1000 videos uploaded it is just not the fancy way to do it, since you will have to pass 1000 ids to the queryParam and then sort the whole thing by date. – Proqb Aug 19 '15 at 08:32

1 Answers1

61

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}
Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70