-
Notifications
You must be signed in to change notification settings - Fork 0
/
livestream.js
257 lines (222 loc) · 9.34 KB
/
livestream.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
let follow_id = []
let streamers = []
let possible_pagination = ""
async function getAfterPagination(user_id) {
let twitchHeader = new Headers();
twitchHeader.append('Authorization', 'Bearer ' + config.AUTH_TOKEN)
twitchHeader.append('Client-Id', config.CLIENT_ID)
var myInit = {
method: 'GET',
headers: twitchHeader,
mode: 'cors',
cache: 'default'
}
while(possible_pagination != null) {
let myRequest = new Request(`https://api.twitch.tv/helix/users/follows?from_id=${user_id}&after=${possible_pagination}`, myInit)
await fetch(myRequest).then(function(response) {
return response.json()
.then(jsonResponse => {
for (let i = 0; i <jsonResponse.data.length; i++){
follow_id.push(jsonResponse.data[i].to_id)
}
possible_pagination = jsonResponse.pagination.cursor
})
})
}
}
async function getUserId() {
let twitchHeader = new Headers();
twitchHeader.append('Authorization', 'Bearer ' + config.AUTH_TOKEN)
twitchHeader.append('Client-Id', config.CLIENT_ID)
var myInit = {
method: 'GET',
headers: twitchHeader,
mode: 'cors',
cache: 'default'
}
let myRequest = new Request(`https://api.twitch.tv/helix/users?login=terpomal`, myInit)
let userID = ""
userID = fetch(myRequest).then(function(response) {
return response.json()
.then(jsonResponse => {
return jsonResponse.data[0].id
})
})
return userID
}
async function getFollowedUsers(id) {
let twitchHeader = new Headers();
twitchHeader.append('Authorization', 'Bearer ' + config.AUTH_TOKEN)
twitchHeader.append('Client-Id', config.CLIENT_ID)
var myInit = {
method: 'GET',
headers: twitchHeader,
mode: 'cors',
cache: 'default'
}
let myRequest = new Request(`https://api.twitch.tv/helix/users/follows?from_id=${id}`, myInit)
await fetch(myRequest).then(function(response) {
return response.json()
.then(jsonResponse => {
for (let i = 0; i <jsonResponse.data.length; i++){
follow_id.push(jsonResponse.data[i].to_id)
}
possible_pagination = jsonResponse.pagination.cursor
})
})
if (possible_pagination != null) {
getAfterPagination(id)
}
}
async function constructStreamers() {
// streamers : [
// {
// streamer_id : int,
// streamer_name : String,
// profilePic : String,
// stream_live: Boolean,
// stream_title: String | null,
// viewer_count: Int | null,
// game_title: String | null,
// stream_url: String,
// },
// ...
// ]
let twitchHeader = new Headers();
twitchHeader.append('Authorization', 'Bearer ' + config.AUTH_TOKEN)
twitchHeader.append('Client-Id', config.CLIENT_ID)
var myInit = {
method: 'GET',
headers: twitchHeader,
mode: 'cors',
cache: 'default'
}
for (let i = 0; i < follow_id.length; i++) {
streamer_info = {}
streamer_info['streamer_id'] = follow_id[i]
//Get User Information
let myRequest = new Request(`https://api.twitch.tv/helix/users?id=${follow_id[i]}`, myInit)
await fetch(myRequest).then(function(response) {
response.json()
.then(jsonResponse => {
console.log(jsonResponse)
streamer_info['streamer_name'] = jsonResponse.data[0].display_name
streamer_info['streamer_profile_pic'] = jsonResponse.data[0].profile_image_url
})
})
myRequest = new Request(`https://api.twitch.tv/helix/streams?user_id=${follow_id[i]}`, myInit)
await fetch(myRequest).then(function(response) {
return response.json()
.then(jsonResponse => {
// if (jsonResponse.data.length !== 0){
// streamer_info['stream_live'] = true
// streamer_info['stream_title'] = jsonResponse.data[0].title
// streamer_info['viewer_count'] = jsonResponse.data[0].viewer_count
// streamer_info['game_title'] = jsonResponse.data[0].game_name
// }
// else
// {
// streamer_info['stream_live'] = false
// streamer_info['stream_title'] = null
// streamer_info['viewer_count'] = null
// streamer_info['game_title'] = null
// }
streamer_info['stream_live'] = false
streamer_info['stream_title'] = null
streamer_info['viewer_count'] = null
streamer_info['game_title'] = null
})
})
streamer_info['stream_url'] = `https://twitch.tv/${streamer_info['streamer_name'].toLowerCase()}`
streamers.push(streamer_info)
}
}
async function updateLiveStreams() {
let twitchHeader = new Headers();
twitchHeader.append('Authorization', 'Bearer ' + config.AUTH_TOKEN)
twitchHeader.append('Client-Id', config.CLIENT_ID)
var myInit = {
method: 'GET',
headers: twitchHeader,
mode: 'cors',
cache: 'default'
}
for (let i = 0; i < streamers.length; i++) {
myRequest = new Request(`https://api.twitch.tv/helix/streams?user_id=${streamers[i].streamer_id}`, myInit)
await fetch(myRequest).then(function(response) {
return response.json()
.then(jsonResponse => {
if (jsonResponse.data.length !== 0){
streamers[i].stream_live = true
streamers[i].stream_title = jsonResponse.data[0].title
streamers[i].viewer_count = jsonResponse.data[0].viewer_count
streamers[i].game_title = jsonResponse.data[0].game_name
}
})
})
}
}
function insertHTMLStreamer(streamer) {
content = document.getElementById("main-content")
var streamer_box = document.createElement("div")
streamer_box.setAttribute("id", "streamer");
streamer_box.setAttribute("class", "streamer");
streamer_box.className += (streamer.stream_live == true ? " live" : "" )
var streamer_profile_pic = document.createElement("img")
streamer_profile_pic.setAttribute("class", "pfp");
streamer_profile_pic.setAttribute("src", `${streamer.streamer_profile_pic}`);
streamer_box.appendChild(streamer_profile_pic)
var streamers_name = document.createElement("p")
streamers_name.setAttribute("class", "streamer-name");
streamers_name.innerHTML = `${streamer.streamer_name}`;
streamer_box.appendChild(streamers_name);
var streamers_game = document.createElement("p")
streamers_game.setAttribute("class", "streamer-game");
streamers_game.innerHTML = `Game: ${streamer.game_title == null ? "N/A" : streamer.game_title}`
streamer_box.appendChild(streamers_game);
if(streamer.viewer_count !== null) {
var streamer_count_div = document.createElement("div");
streamer_count_div.setAttribute("class", "view-count");
var viewer_icon = document.createElement("img");
var streamer_count = document.createElement("p");
viewer_icon.setAttribute("src", "icons/user.svg");
viewer_icon.setAttribute("height", "10");
viewer_icon.setAttribute("height", "10");
streamer_count.setAttribute("class", "viwer-number")
streamer_count.innerHTML = `${streamer.viewer_count}`;
streamer_count_div.appendChild(viewer_icon);
streamer_count_div.appendChild(streamer_count);
streamer_box.appendChild(streamer_count_div);
}
var streamer_url = document.createElement("a");
streamer_url.setAttribute("class", "arrow-link");
streamer_url.setAttribute("href", `${streamer.stream_url}`);
streamer_url.setAttribute("target", "_blank");
var arrow_icon = document.createElement("img");
arrow_icon.setAttribute("src", "icons/caret-right-solid.svg");
arrow_icon.setAttribute("height", "87");
arrow_icon.setAttribute("height", "100");
streamer_url.appendChild(arrow_icon);
streamer_box.appendChild(streamer_url);
content.appendChild(streamer_box);
}
document.addEventListener('DOMContentLoaded', async () => {
if(localStorage.getItem("followedStreamers") === null) {
const userID = await getUserId() //async and await is nw standard JS
await getFollowedUsers(userID)
await constructStreamers()
localStorage.setItem("followedStreamers", JSON.stringify(streamers));
}
else {
streamers = JSON.parse(localStorage.getItem("followedStreamers"));
console.log(streamers);
}
//Update streamers count and game title
await updateLiveStreams();
streamers.sort((a,b) => b.viewer_count - a.viewer_count)
document.getElementById("search-box").style.display = "none";
for (let i = 0; i < streamers.length; i++) {
insertHTMLStreamer(streamers[i])
}
//Need to implement a way to limit amount of streamers shown, but increase if scroll down
})