109 lines
3.5 KiB
JavaScript
109 lines
3.5 KiB
JavaScript
// Utility functions for searching youtube
|
|
|
|
/**
|
|
* Fetch
|
|
* @param {string} gameName
|
|
* @param {number} channelsNumber
|
|
* @returns Promise<void>
|
|
*/
|
|
async function fetchGame(gameName, channelsNumber) {
|
|
console.log("🔎 Searching Youtube for " + channelsNumber + " channels about " + gameName + ".")
|
|
const url = `https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=${channelsNumber}&q=${gameName}&relevanceLanguage=FR®ionCode=FR&key=${process.env.YT_API_KEY}`;
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
console.log(res.status, res.statusText);
|
|
throw res.status;
|
|
}
|
|
const json = await res.json();
|
|
return json
|
|
}
|
|
|
|
/**
|
|
* Check if the channel has published something, anything, in the last 30 days
|
|
* @param {string} channelId
|
|
* @returns Boolean
|
|
*/
|
|
async function isChannelActive(channelId) {
|
|
console.log("🔎 Checking if " + channelId + " is active.")
|
|
|
|
const now = new Date();
|
|
const nowIso = now.toISOString();
|
|
const aMonthAgo = now.setMonth(now.getMonth() - 1);
|
|
const aMonthAgoIso = new Date(aMonthAgo).toISOString();
|
|
const url = `https://youtube.googleapis.com/youtube/v3/search?part=snippet&channelId=${channelId}&maxResults=1&publishedAfter=${aMonthAgoIso}&publishedBefore=${nowIso}&type=video&key=${process.env.YT_API_KEY}`;
|
|
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
console.log(res.status, res.statusText, res);
|
|
throw res.status;
|
|
}
|
|
const json = await res.json();
|
|
if (json.items.length > 0) {
|
|
console.log("🔎 Channel " + channelId + " is active.")
|
|
return true
|
|
} else {
|
|
console.log("🔎 Channel " + channelId + " is not active.")
|
|
return false
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Pull the last 10 videos and returns a median view count
|
|
* @param {string} channelId
|
|
* @returns Float
|
|
*/
|
|
async function getMedianViewCount(channelId, videoCount) {
|
|
const url = `https://youtube.googleapis.com/youtube/v3/search?part=snippet&order=date&channelId=${channelId}&maxResults=${videoCount}&type=video&key=${process.env.YT_API_KEY}`;
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
console.log(res.status, res.statusText);
|
|
return `<p>${res.statusText}</p>`;
|
|
}
|
|
|
|
const json = await res.json();
|
|
const videoIds = json.items.map((x) => x.id.videoId);
|
|
|
|
const url2 = `https://youtube.googleapis.com/youtube/v3/videos?part=statistics&id=${videoIds.join()}&key=${process.env.YT_API_KEY}`;
|
|
const res2 = await fetch(url2);
|
|
if (!res2.ok) {
|
|
console.log(res2.status, res2.statusText);
|
|
return `<p>${res2.statusText}</p>`;
|
|
}
|
|
const json2 = await res2.json();
|
|
const views = json2.items.map((x) => parseInt(x.statistics.viewCount));
|
|
|
|
const medianViews =
|
|
views.reduce((accumulator, currentValue) => {
|
|
return accumulator + currentValue;
|
|
}, 0) / json2.items.length;
|
|
return medianViews
|
|
}
|
|
|
|
|
|
async function searchGameInChannel(gameName, channelId) {
|
|
const url =
|
|
`https://youtube.googleapis.com/youtube/v3/search?part=snippet&channelId=${channelId}&maxResults=50&q=${gameName}&type=video&key=${process.env.YT_API_KEY}`
|
|
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
console.log(res.status, res.statusText);
|
|
return `<p>${res.statusText}</p>`;
|
|
}
|
|
const json = await res.json();
|
|
|
|
const videos = []
|
|
for (item of json.items) {
|
|
if (item.snippet.title.toLowerCase().includes(gameName.toLowerCase()) && item.id.videoId) {
|
|
videos.push(item)
|
|
}
|
|
}
|
|
|
|
// SI FIN DE LA LISTE MULTIPAGE
|
|
// Recursive function
|
|
return videos
|
|
}
|
|
|
|
module.exports = { fetchGame, isChannelActive, getMedianViewCount, searchGameInChannel };
|