forked from klu0926/youtubeCommentPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
201 lines (163 loc) · 7.25 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* API KEY
How to Get a YouTube API Key
Log in to Google Developers Console.
Create a new project.
On the new project dashboard, click Explore & Enable APIs.
In the library, navigate to YouTube Data API v3 under YouTube APIs.
Enable the API.
Create a credential.
A screen will appear with the API key.
*/
// videoID
// example: https://www.youtube.com/watch?v=aojsUEPIhZQ&t=6215s&ab_channel=CharluluStory
// id comes after the "v=": aojsUEIhZQ
//Youtube API HOW TO: //
//https://developers.google.com/youtube/v3/code_samples/code_snippets//
// Youtube API
const snipitsAPI = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=";
const commentsAPI = "https://www.googleapis.com/youtube/v3/commentThreads?";
const API_KEY = 'AIzaSyC2UIxuyuVcI4-tShB6QY6gjN151eM9zPI';
let videoId = '';
// Model
let commentsArray = [];
let nameList = [];
let previousWinners = [];
// Toggle dark mode
const darkModeButton = document.getElementById('darkModeButton');
const darkModeIcon = document.getElementById('darkModeIcon');
darkModeButton.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
darkModeIcon.textContent = document.body.classList.contains('dark-mode') ? '☀️' : '🌙';
});
const soundDuration = 4000;
// Drum roll button
const drumRollButton = document.getElementById('drumRollButton');
drumRollButton.addEventListener('click', () => {
chooseWinner('yes');
});
// Winner button
const winnerButton = document.getElementById('winnerButton');
winnerButton.addEventListener('click', () => {
chooseWinner();
});
// Choose a winner
function chooseWinner(playAudio) {
hideComment();
if (nameList.length === 0) {
alert("¡Ingresa el link del video!");
} else {
const spinDuration = soundDuration;
const intervalTime = 70;
var spinTimes = 30;
let i = 0;
let number = 0;
let winner = "";
if (playAudio == 'yes') {
spinTimes = Math.floor(spinDuration / intervalTime)
const audio = new Audio('drum_roll_sound.mp3');
audio.play();
}
const myInterval = setInterval(myWinnerDisplay, intervalTime);
function myWinnerDisplay() {
i++;
if (i > spinTimes) {
clearInterval(myInterval);
displayComment();
} else {
do {
number = Math.floor(Math.random() * nameList.length);
winner = nameList[number];
} while (previousWinners.includes(winner) && previousWinners.length < nameList.length);
document.querySelector("#winner").innerText = winner;
}
}
function displayComment() {
const message = commentsArray[number].snippet.topLevelComment.snippet.textDisplay;
document.querySelector("#winnerCommentLabel").style.display = "block";
document.querySelector("#winnerComment").innerHTML = message;
previousWinners.push(winner);
console.log(`previousWinners = ${previousWinners}`);
}
}
}
function hideComment() {
document.querySelector("#winnerCommentLabel").style.display = "none";
document.querySelector("#winnerComment").textContent = "";
}
function hideWinner() {
document.querySelector("#winner").textContent = "";
}
document.querySelector("#sendButton").addEventListener("click", (event) => {
const inputValue = document.querySelector("#urlInput").value;
hideWinner();
hideComment();
document.getElementById("loadingNameList").style.display = "block";
document.getElementById("loadingWinner").style.display = "block";
document.querySelector("#nameList").innerHTML = "";
document.querySelector("#title").innerHTML = "";
let regex = /v=([^&]+)|be\/([^&]+)/;
const match = inputValue.match(regex);
if (!match || (!match[1] && !match[2])) {
alert("URL inválida. Ingresá una URL de Youtube válida.");
document.getElementById("loadingNameList").style.display = "none";
document.getElementById("loadingWinner").style.display = "none";
return;
}
const videoId = match[1] || match[2];
console.log(`match =${match}`);
console.log(`match length=${match.length}`);
console.log(`videoID =${videoId}`);
const URL = `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${videoId}&key=${API_KEY}`;
const maxResult = 250;
const URL2 = `https://youtube.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=${maxResult}&order=time&videoId=${videoId}&key=${API_KEY}`;
getData(URL, URL2);
async function getData(videoURL, commentsURL, nextPageToken = '') {
try {
const responseInfo = await fetch(videoURL);
if (!responseInfo.ok) {
throw new Error("No se pudo obtener la información del video.");
}
const finalCommentsURL = nextPageToken ? `${commentsURL}&pageToken=${nextPageToken}` : commentsURL;
const responseComments = await fetch(finalCommentsURL);
if (!responseComments.ok) {
throw new Error("No se pudieron obtener los comentarios.");
}
const infoData = await responseInfo.json();
const commentsData = await responseComments.json();
if (!infoData.items || !infoData.items.length) {
throw new Error("No se encontró información del vídeo.");
}
const videoTitle = infoData.items[0].snippet.title;
document.querySelector("#title").innerHTML = videoTitle;
commentsArray = [...commentsArray, ...commentsData.items];
if (commentsData.nextPageToken) {
await getData(videoURL, commentsURL, commentsData.nextPageToken);
} else {
nameList = [];
document.querySelector("#nameList").innerHTML = "";
document.querySelector("#nameTot").innerText = `Usuarios:`;
commentsArray.map(item => {
nameList.push(item.snippet.topLevelComment.snippet.authorDisplayName);
});
console.log(`nameList (before deduplication) = ${nameList}`);
const uniqueNameList = [...new Set(nameList)];
console.log(`nameList (after deduplication) = ${uniqueNameList}`);
uniqueNameList.map(name => {
const sp = document.createElement("span");
sp.innerText = name;
sp.classList.add("name-tag", "bg-primary", "text-white");
document.querySelector("#nameList").appendChild(sp);
});
console.log(`nameTot = ${uniqueNameList.length}`);
document.querySelector("#nameTot").innerText = `Usuarios: ${uniqueNameList.length} en total`;
document.getElementById("loadingNameList").style.display = "none";
document.getElementById("loadingWinner").style.display = "none";
}
} catch (error) {
alert(`Error: ${error.message}`);
console.error(error);
document.getElementById("loadingNameList").style.display = "none";
document.getElementById("loadingWinner").style.display = "none";
}
}
});