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?
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. For the uploads playlist of a channel, the items returned by PlaylistItems.list API endpoint are (have to be) ordered in reverse chronological order 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 in reverse chronological order (newest first) by contentDetails.videoPublishedAt:
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 in reverse chronological order by contentDetails.videoPublishedAt.
Related
In YouTube's Data API, data are returned in fields, and fields are grouped in parts. In the YouTube documentation, YouTube Data API Overview, the section, How to use the part parameter, gives a list of the parts available for videos:
snippet, contentDetails, fileDetails, player, processingDetails, recordingDetails, statistics, status, suggestions, topicDetails
There are two things I have not found in the documentation:
The list of parts available for channels and playlists.
The list of fields available in each part for videos, channels, and playlists.
Where can I get that information?
Also, some fields are available to the public, while others are only available to the owner of the subject resource. So in the list of fields, I need to know which I can query and which are restricted.
Found the answer:
Channels
Parts: docs/channels/list#parameters
Fields: docs/channels#properties
Restrictions: docs/channels/list#auth
Playlists
Parts: docs/playlists/list#parameters
Fields: docs/playlists#properties
Restrictions: If there are any, they don't seem to be documented.
Playlist items
Parts: docs/playlistItems/list#parameters
Fields: docs/playlistItems#properties
Restrictions: If there are any, they don't seem to be documented.
Videos
Parts: docs/videos/list#parameters (The list at that link gives three additional parts that are not in the list at How to use the part parameter: id, liveStreamingDetails, and localizations.)
Fields: docs/videos#properties
Restrictions: Stated in the descriptions of the fileDetails, processingDetails, and suggestions fields.
===================================
Fields that are missing from my previous answer are
contentDetails
regionRestriction
hasCustomThumbnail
status
failureReason
rejectionReason
publishAt (But this should never appear in a public listing because it only applies to videos with privacyStatus = 'private`.)
selfDeclaredMadeForKids (But only returned if authorized.)
statistics
dislikeCount (But only returned if authorized.)
player
embedHeight
embedWidth
recordingDetails
location
recordingDate
liveStreamingDetails
6 fields
localizations
key (language)
I have used a method to get a partial list of the public fields available for videos. This is a hack and I hope someone will post a better answer, preferably a link to the answer in official YouTube documentation.
Until we get that answer, here is the hack I used:
First, I took the list of the ten parts given for videos and submitted it in an API call for three arbitrary videos:
https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,fileDetails,player,processingDetails,recordingDetails,statistics,status,suggestions,topicDetails&id=XqZsoesa55w,kJQP7kiw5Fk,F4tHL8reNCs&key=<key>
The result was this error message:
The request is not properly authorized to access video file or
processing information. Note that the fileDetails,
processingDetails, and suggestions parts are
only available to that video's owner.
That is information. It tells me that those three parts are not publicly available, so I can't query them on videos I don't own.
So next I submitted the same request with those three parts removed:
https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,player,recordingDetails,statistics,status,topicDetails&id=XqZsoesa55w,kJQP7kiw5Fk,F4tHL8reNCs&key=<key>
I then took the response from that and parsed the JSON to extract the labels at the third and fourth level of nesting. These are mostly the part and field names. The first three rows of the following table are fields; the rest are parts with their constituent fields.
Field or part
Field
kind
etag
id
snippet
publishedAt
channelId
title
description
thumbnails
channelTitle
tags
categoryId
liveBroadcastContent
defaultLanguage
localized
defaultAudioLanguage
contentDetails
duration
dimension
definition
caption
licensedContent
contentRating
projection
status
uploadStatus
privacyStatus
license
embeddable
publicStatsViewable
madeForKids
statistics
viewCount
likeCount
favoriteCount
commentCount
player
embedHtml
topicDetails
topicCategories
recordingDetails
Notice that part recordingDetails has no fields listed. That doesn't mean it has no fields. It means that none of the videos for the IDs submitted have recordingDetails in their data. That is why I said at the beginning that what I've got is a partial list. It is only based on the data retrieved for those three videos. Another example of this issue is that only two of the three videos submitted have a commentCount in their data. If none of them had data for that field, then I would not know about the existence of that field from this exercise.
So there's my partial list of public fields for videos. Can someone give us a better answer pointing to documentation of all the fields for videos, channels, and playlists?
I need to get daily uploaded videos in a playlist so thought to get videos for a playlist sorted by published date.
So is there any option to pass published date while fetching data for playlist?
I tried 2 options
option 1 )
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=15&playlistId=PLP-nGFpz3fa_boeLhG4m0Ie_8vmCvp5oH&key={YOUTUBE_API_KEY}
does result return from this URL is in sorted order? I found its not in sorted order.
Can I pass some date parameters related to date in this or some sort related parameters?
option2 )
I tried using "Youtube Search API"
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list
here we have date option but only can pass channelId not playlistId.
So can we pass playlistId in search API to get all videos for playlist?
Is there any other way which I can use?
Thanks
The PlaylistItems:list does not have any way to order results - the playlist order is returned in the order specified by the owner. Nor does Search:list allow you to specify a playlist.
Does the playlist owner add new videos to the beginning or end of the playlist? If the beginning, you could try fetching the first X videos and comparing the publish date to the current date.
Looking at the guide using Videos.list, nowhere does it mention that this feature is available, where the list is readily sorted upon return of the response body. Try putting the results in a container first where you will implement your own comparison of dates to sort the items. This SO thread seems to confirm this.
Given the uploads playlist of a channel I can query the playlist using playlistItems.list. Querying for snippet gives us publishedAt. However the publishedAt date does not match up with querying each video individually or the time on the web client. The time given by querying the video itself matches up with the time given by the website. Also the returned videos are ordered by this date however this is not the order given on the actual channel.
What does this time represent and is it possible to get the uploads playlist ordered by (actual) publish date?
From the playlistItems documentation:
snippet.publishedAt
datetime The date and time that the item was added
to the playlist. The value is specified in ISO 8601
(YYYY-MM-DDThh:mm:ss.sZ) format.
This differs from the publishedAt value for videos.list, which gives you the actual date it was published. If you wanted the videos in the actual order they were uploaded, you'd have to get the ID of each video in the uploads playlist, then the videos.list publishedAt value for each one, then put them in order.
I am using the Youtube v3 api (video list function) to retrieve the statistics for some videos uploaded by a client. I filter the list by the video ids.
Does anyone have any idea how long the "id" parameter could be for such request? The reference only states this:
id - string - The id parameter specifies a comma-separated list of the YouTube video ID(s) for the resource(s) that are being retrieved. In a video resource, the id property specifies the video's ID.
In theory 50 IDs... you technically can only query 50 videos at a time, so unless you use next page tokens and keep the IDs, you're limited to 50... Personally I wouldn't go above 50 at a time...
I have made an internal movie site in .Net.
I play the movie trailers using jw player. Now I want to know how to calculate the number of views for a video?
Is it possible through code?
PLease help.
NBL I dont have any database. I add the videos through an xml and the code reads the xml.
A simple approach is to tally the number of pageloads for the page that contains the video, rather than the number of times the video itself is played. First, create a table in your database that contains these fields:
DateTime date // date of pageloads -- we'll get to this in a minute
int videoID // Unique Identifier for the video loaded
int count // Number of pageloads
When I tally pageloads, I do them by day so I can compile statistics over time. Of course, you can use a different granularity depending on your particular needs.
Additionally, I don't particularly like writing to the database with each pageload, so I have a class I've written that caches the hits, then writes them after every hundred hits or so.
In this class, I also retain each user's IP address. This allows me to ferret out duplicate pageloads. A subsequent task, which I'm working through in my own hitcounter, is to triage humans, legitimate spiders and unwelcome bots.
Not sure exactly what you need.. but you can handle the play command (button or loading... however your videos are played), and attach that to a counter that you save in your database.
We would need a lot more (code set, video codec and such) before giving more.
You can also create a separate table with these columns :
VideoId
IP adress
And add a row each times an user watch a video...
Then you will calculate unique watch and not duplicate
(Instead of ip adress you can store userId if your users are registered)
The database would have a table for videos, including a Hits column.
When you request the page from the server, the server would execute a stored procedure that adds +1 to the 'Hits' column.
If you are embedding videos on your page, it's worth being aware of YouTube's policy on the question of autoplaying those videos, in relation to the official viewcount. If in the player you set autoplay = true, or equivalent, then the YouTube view count doesn't increase. This is to counter spammy viewcount auto-augmentation pages. The user has to click on the play button, and watch all, or at least most, of the video to count as a view.