-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
145 lines (126 loc) · 4.29 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
// Options
const CLIENT_ID = process.env.CLIENT_ID_GOOGLE;
const DISCOVERY_DOCS = [ 'https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest' ];
const SCOPES = 'https://www.googleapis.com/auth/youtube.readonly';
// DOM ELEMENTS
const authorizeButton = document.getElementById('authorize-button');
const signoutButton = document.getElementById('signout-button');
const content = document.querySelector('#content');
const channelForm = document.querySelector('#channel-form');
const channelInput = document.querySelector('#channel-input');
const videos = document.querySelector('#video-container');
const defaultChannel = 'techguyweb';
channelForm.addEventListener('submit', (e) => {
e.preventDefault();
const channelName = channelInput.value;
getChannel(channelName);
});
// Auth2 Library
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
function initClient() {
gapi.client
.init({
discoveryDocs: DISCOVERY_DOCS,
clientId: CLIENT_ID,
scope: SCOPES
})
.then(() => {
// Listening SignIn status with an outside function
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// HandleCurrent state of user
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = authClicked;
signoutButton.onclick = signoutClicked;
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
content.style.display = 'block';
videos.style.display = 'block';
getChannel(defaultChannel);
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
content.style.display = 'none';
videos.style.display = 'none';
}
}
function authClicked() {
gapi.auth2.getAuthInstance().signIn();
}
function signoutClicked() {
gapi.auth2.getAuthInstance().signOut();
}
function showChannel(data) {
document.querySelector('#channel-data').innerHTML = data;
}
// Retrieving Channel
function getChannel(channel) {
gapi.client.youtube.channels
.list({
part: 'snippet,contentDetails,statistics',
forUsername: channel
})
.then((response) => {
console.log(response);
const channelInfo = response.result.items[0];
const stats = `
<ul class="collection">
<li class="collection-item"><h6 style="color:red">Title</h6>: ${channelInfo.snippet.title}</li>
<li class="collection-item"><h6 style="color:red">ID</h6>: ${channelInfo.id}</li>
<li class="collection-item"><h6 style="color:red">Subscribers Count</h6>: ${numberWithCommas(
channelInfo.statistics.subscriberCount
)}</li>
<li class="collection-item"><h6 style="color:red">Views</h6>: ${numberWithCommas(
channelInfo.statistics.viewCount
)}</li>
<li class="collection-item"><h6 style="color:red">Videos</h6>: ${numberWithCommas(
channelInfo.statistics.videoCount
)}</li>
</ul>
<p>Description: ${channelInfo.snippet.description}</p>
<hr>
<a class="btn grey darken-2" target="_blank" href="https://youtube.com/${channelInfo.snippet
.customUrl}">Go to Channel</a>
`;
showChannel(stats);
const playlistId = channelInfo.contentDetails.relatedPlaylists.uploads;
videoPlaylists(playlistId);
})
.catch((err) => {
console.log(err);
});
}
// Commas in number
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
function videoPlaylists(id) {
const requestVideos = gapi.client.youtube.playlistItems.list({
playlistId: id,
part: 'snippet',
maxResults: 12
});
requestVideos.execute((response) => {
console.log(response);
const playlistItems = response.result.items;
if (playlistItems) {
let videoDisplayed = '<br><h4 class="center-align latest">Latest Videos</h4>';
playlistItems.forEach((eachVideo) => {
const videoId = eachVideo.snippet.resourceId.videoId;
videoDisplayed += `
<div class="col s3">
<iframe style="margin-right : 20px" width="250" height="auto" src="https://www.youtube.com/embed/${videoId}" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
`;
});
videos.innerHTML = videoDisplayed;
} else {
videos.innerHTML = 'No video available on this channel';
}
});
}