YouTube Playlist Item API publishedAt field clarification

陌路散爱 提交于 2021-01-28 21:57:13

问题


I have tested YouTube's PlaylistItems.list API to fetch a channel's default playlist.

In the response payload, a video -- that was published on 2020-10-14T20:22:24Z -- is in the first object, and then the next video -- that was published on 2020-10-21T17:54:05Z -- is in the second object.

In this answer it is mentioned that:

The publishedAt getting returned from a Playlist query is the date the video was added to the playlist, rather than the date the video was published on YouTube

What might be the reason for my case?

Is there any specific sorting order maintained for this API?


回答1:


The reason you see the ordering you've exemplified (though you did not mentioned your playlist ID such that others to verify your claim) is two fold.

1. A video's publication time may well be different than its upload time

First thing is the following: according to the official docs of the Video resource's publishedAt property (the emphasis below is mine):

snippet.publishedAt (datetime)

The date and time that the video was published. Note that this time might be different than the time that the video was uploaded. For example, if a video is uploaded as a private video and then made public at a later time, this property will specify the time that the video was made public.

There are a couple of special cases:

  • If a video is uploaded as a private video and the video metadata is retrieved by the channel owner, then the property value specifies the date and time that the video was uploaded.
  • If a video is uploaded as an unlisted video, the property value also specifies the date and time that the video was uploaded. In this case, anyone who knows the video's unique video ID can retrieve the video metadata.

The value is specified in ISO 8601 format.

Therefore, in cases, it may very well be that a given video has different upload time and time of publication.

Furthermore, the official docs of PlaylistItems resource says the following w.r.t. two related properties:

snippet.publishedAt (datetime)

The date and time that the item was added to the playlist. The value is specified in ISO 8601 format.

contentDetails.videoPublishedAt (datetime)

The date and time that the video was published to YouTube. The value is specified in ISO 8601 format.

From the spec of snippet.publishedAt, it follows that, in the case of a playlist being the uploads playlist of a given channel (that is a playlist of which ID is of form UU...; note that, usually, a channel ID and its corresponding uploads playlist ID are related by s/^UC([0-9a-zA-Z_-]{22})$/UU\1/, though not documented officially), the value of snippet.publishedAt is the upload date of that video.

The second spec, indicates that the value of contentDetails.videoPublishedAt is that of the corresponding Video resource's snippet.publishedAt, the time of publication of that video.

2. The items returned by PlaylistItems.list API endpoint are (have to be) ordered by contentDetails.videoPublishedAt

Indeed, the ordering condition seems to be true. This feature is not documented officially, but, in my experience, the statement above holds true for every result set obtained from PlaylistItems.list endpoint.

I could make here the following argument justifying the necessity that the items resulted upon the invocation of PlaylistItems.list endpoint be ordered chronologically by contentDetails.videoPublishedAt (the newest first):

This argument is of a pragmatic kind: if the result set of PlaylistItems.list is not ordered as mentioned, then this endpoint becomes useless.

This is so, since, in this case, for one to obtain the most recently published video would have to fetch locally all the uploaded items (the number of which is limited by design to 20000), for to then scan that result set for the most recent one. Being compelled to fetch all uploaded items only for to obtain the newest one is pragmatically a nonsense. If the number of uploads of a given channel exceeds the limit of 20000, then the most recent video could possibly fall outside this boundary; thus, for such kind of channels, the most recently published video could not be obtained from the API at all.

Therefore, by way of contradiction, the result set has to be ordered chronologically by contentDetails.videoPublishedAt with the newest being the first.



来源:https://stackoverflow.com/questions/65002092/youtube-playlist-item-api-publishedat-field-clarification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!