i am trying to fetch multiple channel id’s using a array of different channnel names inorder to use 100 search quota units for a max of 50 channel names and 1 unit per channel id fetched instead of a 100 search quota units per channel name. in my research i’ve had mixed results on if it is possible or not. i am trying to optimise my 10,000 unit quota as much as possible. the code below works but it use 100 search units per channel name and 1 unit per id fetched. ( POST http://localhost:4000/api/youtube/fetch-multiple-channels ) {
“channelNames”: [ “MrBeast”, “Sidemen”, “mkbhd” ]
}
const fetchChannelIds = async (channelNames) => {
const channelIds = {};
for (const name of channelNames) {
// Encode the channel name to ensure it's URL-safe
const encodedName = encodeURIComponent(name);
// Fetch channels that exactly match the query
const searchResponse = await axios.get(`https://www.googleapis.com/youtube/v3/search?part=snippet&type=channel&q=${encodedName}&maxResults=5&key=${API_KEY}`);
await updateQuota('search_channel', 1);
console.log('Search response:', JSON.stringify(searchResponse.data, null, 2));
if (!searchResponse.data.items) {
throw new Error(`No channel data found for channel: ${name}`);
}
// Find the exact channel by comparing the channel title and description (which may contain the custom URL)
const exactChannel = searchResponse.data.items.find(item => {
const titleMatch = item.snippet.title.toLowerCase() === name.toLowerCase();
const descriptionMatch = item.snippet.description.toLowerCase().includes(name.toLowerCase());
return titleMatch || descriptionMatch;
});
if (exactChannel) {
channelIds[name.toLowerCase()] = exactChannel.id.channelId;
}
}
return channelIds;
};