forked from NOALBS/BRB-twitch-clips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclips.html
221 lines (191 loc) · 8.37 KB
/
clips.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>BRB Clips by 715209 (tokon2000 edit)</title>
<style>
body {
margin: 0;
padding: 0;
}
video {
width: 100%;
height: 100vh;
}
</style>
</head>
<body>
<video id="player" autoplay="true"></video>
<script>
class Video {
constructor(elementId, twitchstuff) {
this.video = document.getElementById(elementId);
this.username = twitchstuff.username;
this.clientId = twitchstuff.clientId;
this.clientSecret = twitchstuff.clientSecret;
this.clips = [];
this.oauth = window.localStorage.getItem("oauth");
this.currentClipIndex = parseInt(window.localStorage.getItem("currentClipIndex")) || 0;
this.daysAgo = twitchstuff.daysAgo;
this.clipCount = twitchstuff.clipCount;
this.fetchTopOverall = twitchstuff.fetchTopOverall;
this.fetchTopRecent = twitchstuff.fetchTopRecent;
this.video.addEventListener(
"ended",
this.endHandler.bind(this),
false
);
this.init();
}
async init() {
if (this.oauth == null) await this.getOauth();
await this.getUserId();
this.get();
}
async getClips() {
this.clips = [];
let startDate = new Date();
startDate.setDate(startDate.getDate() - this.daysAgo);
const formattedStartDate = startDate.toISOString();
let endDate = new Date();
const formattedEndDate = endDate.toISOString();
if (this.fetchTopOverall) {
await this.getClipsRequest();
}
if (this.fetchTopRecent) {
await this.getClipsRequest(`&started_at=${formattedStartDate}&ended_at=${formattedEndDate}`);
}
this.currentClipIndex = 0;
window.localStorage.setItem("currentClipIndex", this.currentClipIndex);
this.play();
}
async getOauth() {
let response = await fetch(
`https://id.twitch.tv/oauth2/token?client_id=${this.clientId}&client_secret=${this.clientSecret}&grant_type=client_credentials`,
{ method: "POST" }
);
let data = await response.json();
window.localStorage.setItem("oauth", data.access_token);
this.oauth = data.access_token;
}
async twitchRequest(query) {
let response = await fetch(
`https://api.twitch.tv/helix/${query}`,
{
headers: {
"Client-ID": this.clientId,
Authorization: `Bearer ${this.oauth}`,
},
}
);
switch (response.status) {
case 200:
return await response.json();
break;
case 401:
await this.getOauth();
return await this.twitchRequest(query);
break;
}
}
async getUserId() {
let local = JSON.parse(window.localStorage.getItem("user"));
if (local != null && local.username == this.username) {
this.broadcasterId = local.broadcasterId;
} else {
localStorage.clear();
try {
let data = await this.twitchRequest(
`users?login=${this.username}`
);
this.broadcasterId = data.data[0].id;
window.localStorage.setItem(
"user",
JSON.stringify({
broadcasterId: this.broadcasterId,
username: this.username,
})
);
} catch (error) {
console.log(
"Got an error requesting userid:",
error
);
}
}
}
async getClipsRequest(params) {
try {
let data = await this.twitchRequest(
`clips?broadcaster_id=${
this.broadcasterId
}&first=${this.clipCount}${params != null ? params : ""}`
);
data.data.forEach((clip) => {
let regexp = /(https:\/\/clips-media-assets2.twitch.tv\/.*)-preview/g;
let match = regexp.exec(clip.thumbnail_url);
if (!this.clips.some((c) => c.src == match[1]))
this.clips.push({
src: match[1],
clipped_by: clip.creator_name,
});
});
this.save();
} catch (error) {
console.log("Got an error requesting clips:", error);
}
}
save() {
this.shuffle(this.clips);
window.localStorage.setItem(
"clips",
JSON.stringify(this.clips)
);
}
get() {
let clips = window.localStorage.getItem("clips");
if (clips != null && clips != "" && clips != "[]") {
this.clips = JSON.parse(clips);
this.playNextClip();
} else {
console.log("clips are empty time to fetch");
this.getClips();
}
}
play() {
this.video.src = `${this.clips[this.currentClipIndex].src}.mp4`;
}
playNextClip() {
this.currentClipIndex++;
if (this.currentClipIndex < this.clips.length) {
window.localStorage.setItem("currentClipIndex", this.currentClipIndex);
this.play();
} else {
this.getClips();
}
}
// https://stackoverflow.com/a/12646864
shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
endHandler() {
this.playNextClip();
}
}
let snowman = new Video("player", {
username: "YOURUSERNAME",
clientId: "YOURCLIENTID",
clientSecret: "YOURCLIENTSECRET",
daysAgo: 30, // number of days for startDate
clipCount: 20, // number of clips to be retrieved
fetchTopOverall: false, // whether the top overall clips should be retrieved
fetchTopRecent: true // whether the top most recent clips should be retrieved
});
</script>
</body>
</html>