FYI I have not written this code myself, I've sourced it online via forums like this since it does the job in needs to up to a certain point.
I'm lookin to scrape YouTube search results data based on keywords but need to do so between two dates in time that I can change within the code depending on the project. These dates in time will be determined by published date that can be extracted from the YouTube API.
In addition to this, I am looking at converting both of the Channel and Video IDs into URLs via the script. This is just for ease of use and saves any confusion when sharing the list with other people.
Here is the script I'm currently using:
function YoutubeScraper() { var spreadSheet = SpreadsheetApp.getActiveSpreadsheet() var activeSheet = spreadSheet.getActiveSheet(); // Retrieve YouTube.Search.list. var search = YouTube.Search.list("snippet", { q: "Gang Beasts", maxResults: 10 }); var results = search.items.map((item) => [item.snippet.channelId, item.id.videoId, item.snippet.title, item.snippet.publishedAt]); // Retrieve channel IDs and video IDs. var {channelIds, videoIds} = results.reduce((o, [channelId, videoId]) => { o.channelIds.push(channelId); o.videoIds.push(videoId); return o; }, {channelIds: [], videoIds: []}); // Retrieve YouTube.Videos.list. var videoList = YouTube.Videos.list("statistics", {id: videoIds.join(",")}); var videoStats = videoList.items.map((item) => [item.id, item.statistics.viewCount, item.statistics.likeCount, item.statistics.dislikeCount]).reduce((o, [id, ...v]) => Object.assign(o, {[id]: v}), {}); // Retrieve YouTube.Channels.list var channelList = YouTube.Channels.list("statistics", { id: channelIds.join(",") }); var channelStats = channelList.items.map((item) => [item.id, item.statistics.subscriberCount]).reduce((o, [id, ...v]) => Object.assign(o, {[id]: v}), {}); // Create an array for putting values and put the values to Spreadsheet. results = results.map(e => channelStats[e[0]] ? e.concat(channelStats[e[0]]) : e.concat(Array(2).fill(""))); results = results.map(e => videoStats[e[1]] ? e.concat(videoStats[e[1]]) : e.concat(Array(3).fill(""))); activeSheet.getRange(2, 1, results.length, results[0].length).setValues(results);}