-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch-streamers.js
205 lines (188 loc) · 6.86 KB
/
twitch-streamers.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
(function () {
let users = [
"freecodecamp",
"fakeymcfake23k",
"colinbenders",
"adamkoebel",
"kaypikefashion",
"pianoimproman",
"domesticdan",
"annaprosser",
"outside_the_vox",
"fakeymcfake24k",
"vinesauce",
"sleightlymusical"
];
let usersClean = [];
let online = [];
let offline = [];
let tries = {};
function setupDataStructs() {
users = users.map(user => user.toLowerCase());
users.forEach(user => (tries[user] = {
users: 0,
channels: 0,
streams: 0
}));
}
function* counter() {
let i = 0;
while (true) {
yield i.toString();
i++;
}
}
let count = counter();
function jsonP(url, callbackName) {
let script = document.createElement("script");
let callback = callbackName + count.next().value;
function jCallback(r) {
return response => { //arg length of 1 means a response was received, otherwise error reject
arguments.length == 1 ? r(response) : r(arguments[1]);
window[callback] = null;
delete window[callback];
};
}
return new Promise((resolve, reject) => {
window[callback] = jCallback(resolve);
script.onerror = jCallback(reject, "onerror");
setTimeout(jCallback(reject, "timed out"), 5000);
script.src = `${url}?callback=${callback}`;
document.head.appendChild(script);
script.remove();
});
}
function addClass(list, className) {
list.forEach(function (el) {
if (!el.classList.contains(className)) el.classList.add(className);
});
}
function removeClass(list, className) {
list.forEach(function (el) {
if (el.classList.contains(className)) el.classList.remove(className);
});
}
//display user's div only if it's in specified array
function setDisplay(userDivs, arr) {
for (let i = 0; i < userDivs.length; ++i) {
let curr = userDivs[i];
arr.indexOf(curr.id) > -1 ?
(curr.style.display = "block") :
(curr.style.display = "none");
}
}
function addRemoveClass(addArr, removeArr, className) {
addClass(addArr, className);
removeClass(removeArr, className);
}
function attachBtns() {
let showAll = document.getElementById("all");
let showOl = document.getElementById("online");
let showOff = document.getElementById("offline");
let userDivs = document.getElementsByClassName("users");
showAll.onclick = function () {
addRemoveClass([showAll], [showOl, showOff], "selected");
setDisplay(userDivs, usersClean);
};
showOl.onclick = function () {
addRemoveClass([showOl], [showAll, showOff], "selected");
setDisplay(userDivs, online);
};
showOff.onclick = function () {
addRemoveClass([showOff], [showOl, showAll], "selected");
setDisplay(userDivs, offline);
};
}
function makeDiv(user) {
let div = document.createElement("div");
let img = document.createElement("img");
let a = document.createElement("a");
let h2 = document.createElement("h2");
let p = document.createElement("p");
let circle = document.createElement("div");
div.id = user.toLowerCase();
div.classList.add("users");
a.target = "_blank";
circle.classList.add("circle");
div.appendChild(img);
a.appendChild(h2);
div.appendChild(a);
div.appendChild(circle);
div.appendChild(p);
document.getElementById("users").appendChild(div);
}
function processUsers(user, response) {
if (!response.status) { //user is valid
let div;
makeDiv(user);
div = document.getElementById(user);
div.getElementsByTagName("img")[0].src = response.logo;
div.getElementsByTagName("h2")[0].textContent = response.display_name;
usersClean.push(user);
tries[user]["users"] = 0;
return "done";
} else return null;
}
function processChannels(user, response) {
let div = document.getElementById(user);
let a = div.getElementsByTagName("a")[0];
a.href = response.url;
tries[user]["channels"] = 0;
}
function processStreams(user, response) {
let div = document.getElementById(user);
let circle = div.getElementsByClassName("circle")[0];
let p = div.getElementsByTagName("p")[0];
let stream = response.stream;
if (stream === null) {
p.textContent = "Offline";
removeClass([circle], "blink");
if (offline.indexOf(user) === -1) offline.push(user);
if (online.indexOf(user) > -1) online.splice(online.indexOf(user), 1);
} else {
p.textContent = stream.channel.status;
addClass([circle], "blink");
if (online.indexOf(user) === -1) online.push(user);
if (offline.indexOf(user) > -1) offline.splice(offline.indexOf(user), 1);
}
tries[user]["streams"] = 0;
}
function showError() {
document.getElementById("error").style.display = "block";
document.getElementById("container").style.display = "none";
if (streamCheck) clearInterval(streamCheck);
}
function popUserData(user, reqType, callback) {
let url = `https://wind-bow.gomix.me/twitch-api/${reqType}/${user}`;
return jsonP(url, "parseData")
.then(r => callback(user, r))
.catch(err => {
console.log(err, user, reqType);
if (err === "onerror") showError();
if (err === "timed out") {
if (tries[user][reqType] < 2) {
console.log("trying " + user + " " + reqType);
tries[user][reqType] += 1;
reqType !== "users" ?
popUserData(user, reqType, callback) :
processUserArr([user]);
} else showError();
}
});
}
async function processUserArr(userArr) {
for (const user of userArr) {
let state = await popUserData(user, "users", processUsers);
if (state === "done") {
popUserData(user, "channels", processChannels);
popUserData(user, "streams", processStreams);
}
}
}
setupDataStructs();
processUserArr(users);
attachBtns();
let streamCheck = setInterval(() => {
usersClean.forEach(user => popUserData(user, "streams", processStreams));
}, 10000);
})();