-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
240 lines (186 loc) · 7.85 KB
/
index.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
let key = "AIzaSyAj314hsQUuaB3n_Sebf_28uLfUpNr0boc";
let searchButton = document.getElementById("search-button");
let searchBar = document.getElementById("search-bar");
searchButton.addEventListener("click", () => {
let instructionText = document.getElementById("instructions");
if (instructionText === undefined || null) instructionText.textContent = "";
let mainDiv = document.getElementById("main");
mainDiv.innerHTML = "";
let searchTerm = searchBar.value;
getSearchResults(searchTerm, displayResults);
});
function getSearchResults(searchTerm, displayResults) {
fetch(
"https://www.googleapis.com/youtube/v3/search?" +
new URLSearchParams({
key: key,
part: "snippet",
maxResults: 20,
q: searchTerm,
}).toString()
)
.then((data) => data.json())
.then((data) => displayResults(data));
}
let channelIds = [];
let displayResults = (results) => {
let mainDiv = document.getElementById("main");
let items = results.items;
// video ids in order, to be used in fetching video details
let videoIds = [];
console.log(items);
for (const key in items) {
// skip non video results
if (items[key]["id"]["kind"] !== "youtube#video") continue;
let videoThumbnailUrl = items[key]["snippet"]["thumbnails"]["high"]["url"];
let videoTitle = items[key]["snippet"]["title"];
let videoDescription = items[key]["snippet"]["description"];
let videoId = items[key]["id"]["videoId"];
let channelTitle = items[key]["snippet"]["channelTitle"];
let channelId = items[key]["snippet"]["channelId"];
// initialize card div and append to main div
let cardDiv = document.createElement("div");
cardDiv.classList.add("card");
mainDiv.appendChild(cardDiv);
// video div
let videoDiv = document.createElement("div");
videoDiv.classList.add("video");
let thumbnailmg = document.createElement("img");
thumbnailmg.src = videoThumbnailUrl;
thumbnailmg.classList.add("thumbnail");
cardDiv.appendChild(videoDiv);
videoDiv.appendChild(thumbnailmg);
let videoInfo = document.createElement("div");
videoInfo.classList.add("video-info");
let videoTitleHTML = document.createElement("h3");
videoTitleHTML.textContent = videoTitle;
let channelInfoDiv = document.createElement("div");
channelInfoDiv.classList.add("channel-info");
let channelTitleHTML = document.createElement("span");
channelTitleHTML.classList.add("channel-title");
channelTitleHTML.textContent = channelTitle;
let videoDescriptionHTML = document.createElement("p");
videoDescriptionHTML.textContent = videoDescription;
videoInfo.appendChild(videoTitleHTML);
videoInfo.appendChild(channelInfoDiv);
channelInfoDiv.appendChild(channelTitleHTML);
videoInfo.appendChild(videoDescriptionHTML);
cardDiv.appendChild(videoInfo);
channelIds.push(channelId);
videoIds.push(videoId);
}
// get remaining info needed using another fetch
displayVideoInfo(videoIds, displayVideoInfoOnDocument);
};
let displayChannelImgs = (channelIds, displayChannelImgsOnDocument) => {
// using a loop since the api skips repeated channel ids
// repeated channel ids could be present if search results include more than one video for same channel
const channelDataPromises = channelIds.map((channelId) => {
return fetch(
"https://youtube.googleapis.com/youtube/v3/channels?" +
new URLSearchParams({
part: ["snippet", "contentDetails", "statistics"],
id: channelId,
key: key,
}).toString()
);
});
Promise.all(channelDataPromises).then((responses) =>
responses.forEach((response, index) =>
response.json().then((data) => displayChannelImgsOnDocument(data, index))
)
);
};
let displayChannelImgsOnDocument = (data, counter) => {
// displays channel icon in search results
let channelInfoDivs = document.getElementsByClassName("channel-info");
let channelTitledivs = document.getElementsByClassName("channel-title");
let hiddenDivs = document.getElementsByClassName("hidden");
let channelThumbnail =
data["items"][0]["snippet"]["thumbnails"]["default"]["url"];
let channelInfoDiv = channelInfoDivs.item(counter);
let channelTitle = channelTitledivs.item(counter);
let channelThumbnailSpan = document.createElement("span");
channelThumbnailSpan.textContent = channelThumbnail;
let hiddenDiv = hiddenDivs.item(counter);
hiddenDiv.appendChild(channelThumbnailSpan);
let channelThumbnailElement = document.createElement("img");
channelThumbnailElement.src = channelThumbnail;
channelInfoDiv.insertBefore(channelThumbnailElement, channelTitle);
};
let displayVideoInfo = (videoIds, displayVideoInfoOnDocument) => {
fetch(
"https://youtube.googleapis.com/youtube/v3/videos?" +
new URLSearchParams({
part: ["snippet", "contentDetails", "statistics"],
id: videoIds,
key: key,
}).toString()
)
.then((data) => data.json())
.then((data) => displayVideoInfoOnDocument(data));
};
// search page
let displayVideoInfoOnDocument = (results) => {
let videoInfoDivs = document.getElementsByClassName("video-info");
let channelInfoDivs = document.getElementsByClassName("channel-info");
let cardDivs = document.getElementsByClassName("card");
let counter = 0;
let items = results.items;
// loop over all cards and append missing video info details
for (const key in items) {
let cardDiv = cardDivs.item(counter);
let videoInfoDiv = videoInfoDivs.item(counter);
let channelInfoDiv = channelInfoDivs.item(counter);
let viewsCount = items[key]["statistics"]["viewCount"];
let timePublished = items[key]["snippet"]["publishedAt"];
let videoLikes = items[key]["statistics"]["likeCount"];
let videoDislikes = items[key]["statistics"]["dislikeCount"];
let videoId = items[key]["id"];
// split provided time and get the date only
let time = timePublished.split("T");
timePublished = time[0];
let views = document.createElement("span");
views.textContent = viewsCount + " views ";
let timePublishedHTML = document.createElement("span");
timePublishedHTML.textContent = "Published at: " + timePublished;
videoInfoDiv.insertBefore(views, channelInfoDiv);
videoInfoDiv.insertBefore(timePublishedHTML, channelInfoDiv);
let videoLikesHTML = document.createElement("span");
videoLikesHTML.classList.add("videoLikes");
videoLikesHTML.textContent = videoLikes + "\n";
let videoDislikesHTML = document.createElement("span");
videoDislikesHTML.classList.add("videoDislikes");
videoDislikesHTML.textContent = videoDislikes + "\n";
let videoIdHTML = document.createElement("span");
videoIdHTML.classList.add("videoId");
videoIdHTML.textContent = videoId + "\n";
let hiddenInfoDiv = document.createElement("div");
hiddenInfoDiv.classList.add("hidden");
hiddenInfoDiv.appendChild(videoLikesHTML);
hiddenInfoDiv.appendChild(videoDislikesHTML);
hiddenInfoDiv.appendChild(videoIdHTML);
cardDiv.appendChild(hiddenInfoDiv);
counter++;
}
displayChannelImgs(channelIds, displayChannelImgsOnDocument);
addCardClickListeners();
};
// adding click event listeners to video cards
let addCardClickListeners = () => {
let cardDivs = document.getElementsByClassName("card");
let hiddenDivs = document.getElementsByClassName("hidden");
// click event listeners for cards
for (let i = 0; i < cardDivs.length; i++) {
let card = cardDivs.item(i);
let hiddenDiv = hiddenDivs.item(i);
card.addEventListener("click", () => {
let cardDivText = card.innerText;
let hiddenDivText = hiddenDiv.innerText;
// save needed video info in session since on new page load all previous JS gets deleted
sessionStorage.setItem("cardInfo", cardDivText);
sessionStorage.setItem("hiddenInfo", hiddenDivText);
window.location.href = "video.html";
});
}
};