I am using YouTube Data API and can get subscriber count, view count etc. by using the statistics parameter. I am aware that statistics cannot be used to get total likeCount and commentCount for a channel. But the API allows to get the playlist videos using contentDetails.relatedPlaylists.uploads.
So, my question is
How can I use the contentDetails.relatedPlaylists.uploads to fetch individual videos' likes and comments and append them into a JS variable?
EDIT
The embedded videos with titles are showing perfectly. But commentCount for the video does not show up.
Here is the code
function requestVideoPlaylist(playlistId) {
const requestOptions = {
playlistId: playlistId,
part: 'snippet,contentDetails,statistics',
maxResults: 10
};
const request = gapi.client.youtube.playlistItems.list(requestOptions);
request.execute(response => {
console.log(response);
const playListItems = response.result.items;
if (playListItems) {
let output = '<br><h4 class="center-align">Latest Videos</h4>';
// Loop through videos and append output
playListItems.forEach(item => {
const videoId = item.snippet.resourceId.videoId;
const videoTitle = item.snippet.title;
const videoComment = item.statistics.commentCount;
output += `
<div class="col s3">
<iframe width="100%" height="auto" src="https://www.youtube.com/embed/${videoId}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
"${videoTitle}" "${videoComment}"
</div>
`;
});
// Output videos
videoContainer.innerHTML = output;
} else {
videoContainer.innerHTML = 'No Uploaded Videos';
}
});
}
Help will be greatly appreciated.
For getting the commentNumber from the statistics part of Videos: list in plain JavaScript (because what you seem to do with the library seems correct but not working):
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
getJSON('https://www.googleapis.com/youtube/v3/videos?part=statistics&id=VIDEO_ID&key=API_KEY',
function(err, data) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
alert('The video commentCount: ' + data.items[0].statistics.commentCount);
}
});