-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (106 loc) · 3.17 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
import navbar from "./components/navbar.js";
// console.log(navbar);
let navbar_div = document.querySelector("#navbar");
navbar_div.innerHTML = navbar();
let API = "AIzaSyAUOrcRTExluwZtFn44PZwEX0Qj6n-wD6g";
//var datas;
async function mostPopular() {
try {
const res = await fetch(
`https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=100&chart=mostPopular®ionCode=IN&key=${API}`
); //200&chart=mostPopular®ionCode=IN&key=${API_KEY}`
let data = await res.json();
let datas = data.items;
// appendMovies(datas);
//videofilter(data);
let myPromise = new Promise(function (resolve, reject) {
setInterval(() => {
let movie = datas;
if (movie != null) {
resolve(movie);
} else {
reject("ERR! Your promise cannot be resolve");
}
}, 1000);
});
async function main() {
try {
let response = await myPromise;
append(response);
} catch (error) {
console.log(error);
}
}
main();
// console.log(data.items);
} catch (error) {
console.log("ERR! Server cannot give data ");
}
}
mostPopular();
function append(data) {
let container = document.querySelector("#container");
container.innerHTML = null;
data.forEach(({ snippet, id: { videoId } }) => {
let title = snippet.title;
let image = snippet.thumbnails.high.url;
let channelName = snippet.channelTitle;
let div = document.createElement("div");
let img = document.createElement("img");
img.src = image;
let channel = document.createElement("p");
channel.innerText = channelName;
channel.style.fontSize = "smaller";
let name = document.createElement("h4");
name.innerText = title;
let data = {
snippet,
videoId,
};
div.addEventListener("click", function () {
localStorage.setItem("video_details", JSON.stringify(data));
window.location.href = "playVideo.html";
});
div.append(img, name, channel);
container.append(div);
});
}
// document.onclick = () => {
// datas.sort((a, b) => a.datas.snippet.title - b.datas.snippet.title);
// };
window.searchVideos = async () => {
let query = document.getElementById("query").value;
console.log(query);
try {
let res = await fetch(
`https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=25&q=${query}&key=${API}`
);
let data = await res.json();
let datas = data.items;
append(datas);
//videofilter(data);
// console.log(datas);
} catch (error) {
console.log("ERR! Server cannot give data ");
}
};
let id;
window.debounce = (func, delay) => {
if (id) {
clearTimeout(id);
}
id = setTimeout(() => {
func();
}, delay);
};
document.getElementById("title").addEventListener("click", title);
async function title() {
let res = await fetch(
`https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=100&chart=mostPopular®ionCode=IN&key=${API}`
); //200&chart=mostPopular®ionCode=IN&key=${API_KEY}`
let data = await res.json();
let det = data.items;
det.sort((a, b) => a.snippet.channelTitle - b.snippet.channelTitle);
append(det);
console.log(det);
}