Raccoon-Influencer-Search/libs/utils.js
2024-07-11 20:43:47 +02:00

73 lines
2.5 KiB
JavaScript

const { checkIfGameExists, isChannelActive } = require("./youtube");
const models = require("../models");
const Game = models.Game;
const Channel = models.Channel;
/**
* Cleans the list of channels by extracting the IDs and testing if channel is active.
* @param {string} gameName
* @returns Promise<void>
*/
async function cleanChannelsList(channelsList) {
const allChannels = channelsList.items.map((x) => x.snippet.channelId);
const validChannels = []
for (let channelId of allChannels) {
const channelIsValid = await isChannelActive(channelId);
if (channelIsValid) {
validChannels.push(channelId)
}
}
console.log("🔎 Found " + validChannels.length + " channel(s).")
return validChannels
}
async function templateData(gameName, gameDb, channelsDb) {
let html = `
<p>We retrieved ${channelsDb.length} channel(s) talking about <strong>${gameName}</strong>. The last time we retrieved data for this game was <strong>${gameDb.updatedAt.toLocaleDateString("fr-FR", {})}</strong> at <strong>${gameDb.updatedAt.toLocaleTimeString("fr-FR", {})}</strong>.</p>
<table class="striped">
<thead>
<tr>
<th scope="col">Channel</th>
<th scope="col">Channel Id</th>
<th scope="col">Median Views</th>
<th scope="col">Video Count</th>
<th scope="col">Video List</th>
</tr>
</thead>
<tbody>`;
for (let channel of channelsDb) {
let videos = JSON.parse(channel.videos)
console.log(videos)
let videosTxt = ""
for (vid of videos) {
videosTxt += `<li style="white-space:nowrap">
<a target="_blank" href="https://www.youtube.com/watch?v=${vid.id.videoId}">${vid.snippet.title}</a>
</li>`
}
html += `
<tr>
<td>${channel.name}</td>
<td><a target="_blank" href="https://youtube.com/channel/${channel.channelId}">${channel.channelId}</a></td>
<td>${channel.medianViews}</td>
<td>${channel.videosCount}</td>
<td>
<details>
<summary>
See video titles and links
</summary>
<ul>
${videosTxt}
</ul>
</details>
</td>
</tr>
`;
}
html += ` </tbody></table>`;
return html;
}
module.exports = { cleanChannelsList, templateData };