This is a sample script for retrieving the values of subscriberCount
of the channel from the video URLs of YouTube using Google Apps Script.
In this sample, the video URLs are retrieved from Spreadsheet. And, the retrieved values of subscriberCount
are put to the Spreadsheet. The sample Spreadsheet is as follows.
Please copy and paste the following script to the script editor of Spreadsheet. Before you use this script, please enable YouTube Data API v3 at Advanced Google services. Ref And, please set the sheet name.
function myFunction() {
// Retrieve the YouTube URLs from the column "A" of Spreadsheet.
const sheetName = "Sheet1"; // Please set the sheet name.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getRange("A2:A" + sheet.getLastRow());
const values = range.getValues().map(([a]) => a.split("=").pop().trim());
const check = values.slice();
// Retrieve subscriberCount from the YouTube URLs.
const limit = 50;
const obj = [...Array(Math.ceil(values.length / limit))].reduce((obj, _) => {
const res1 = YouTube.Videos.list(["snippet"], {
id: values.splice(0, limit),
maxResults: limit,
}).items.reduce(
(o, { id, snippet: { channelId } }) => ((o[id] = channelId), o),
{}
);
const res2 = YouTube.Channels.list(["statistics"], {
id: Object.values(res1),
maxResults: limit,
}).items.reduce(
(o, { id, statistics: { subscriberCount } }) => (
(o[id] = Number(subscriberCount)), o
),
{}
);
Object.entries(res1).forEach(([k, v]) => (obj[k] = res2[v] ? res2[v] : ""));
return obj;
}, {});
// Put the values of subscriberCount to the column "B" of the sheet.
range.offset(0, 1).setValues(check.map((a) => [obj[a] || ""]));
}
- I answered this sample script to this thread in the Stackoverflow.
Your code is a great help for me as i am wasting my time by scraping YouTube data hours and hours using data scraping software's.
Now let me come to the point can you please add "view count" of video in your script with subscribers.
And if you have time can you please help me i have a list of 300 keywords in column A I want to scrape every keyword result sort by (videoupload:today type:video duration:under4mins sortby:viewcount).
I am attaching a pic for refrence.
sorry if i make it too long as i have no programming background so its really hard for me to achieve this with youtube api.
Looking forward for your kind reply