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 looking to scrape YouTube search results data based on keywords between two dates in time, for example; September 19th - October 4th.
What code and where can I add this to the script?
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);
}