I've been quite overwhelmed with all of this as I am completely new to working with APIs. I would like to populate a Google Sheet with a channel's videos so that I can always have its stats updated anytime I need while adding (or replacing the oldest) new videos to the list. I tried following a video guide and feel like I'm close but I'm having troubles.
NOTE: The playlistID I used below is for the Google Developers channel.
The video I watched showed an example where the videos being pulled were the top search results for a specific keyword. I would instead like to pull the last 50 videos from a specific channel.
The original code had the line:
var sr = YouTube.Search.list("snippet,id", { q: "guitars", maxResults: 50});
I changed that to:
var sr = YouTube.PlaylistItems.list("snippet,id", { playlistId: "UU_x5XG1OV2P6uZZ5FSM9Ttw", maxResults: 50});
The problem seems to be in this line:
var srVidsOnly = sr.items.filter(function(res){ return res.resourceId.kind === "youtube#playlistItems"});
I tried going through this and after many trial-and-error attempts, gave up. I'm not sure what this line is supposed to look like at all.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var sr = YouTube.PlaylistItems.list("snippet,id", { playlistId: "UU_x5XG1OV2P6uZZ5FSM9Ttw", maxResults: 50});
var srVidsOnly = sr.items.filter(function(res){ return res.resourceId.kind === "youtube#playlistItems"});
var modRes = srVidsOnly.map(function(v){ return [v.resourceId.videoId,v.snippet.title]; });
var ids = modRes.map(function(res){return res[0]; }).join(",");
var stats = YouTube.Videos.list("statistics", {id: ids});
var vidStats = stats.items.map(function(res){return [res.statistics.viewCount, res.statistics.likeCount]; });
activeSheet.getRange(2, 1, modRes.length, modRes[0].length).setValues(modRes);
activeSheet.getRange(2, 3, vidStats.length, vidStats[0].length).setValues(vidStats);
}
The error given is:
TypeError: Cannot read property "kind" from undefined. (line 6, file "Code")
Removing kind from the fifth line, like this:
var srVidsOnly = sr.items.filter(function(res){ return res.resourceId === "youtube#playlistItems"});
gives the error:
TypeError: Cannot read property "length" from undefined. (line 12, file "Code")
Following the initial video guide exactly worked (videos from search result). The sheet was formatted just as I wanted it. I just need videos from a specific channel rather than a search results.
Thanks