-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclips_alt.html
265 lines (245 loc) · 9.24 KB
/
clips_alt.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
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
258
259
260
261
262
263
264
265
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Twitch Clips Player</title>
<style>
body {
background-color: #121212;
color: #fff;
font-family: Arial, sans-serif;
text-align: center;
padding: 10px;
margin: 0;
}
.video-container {
position: relative;
width: 1280px;
height: 720px;
margin: 10px auto;
border: 5px solid #333;
border-radius: 10px;
overflow: hidden;
}
video {
width: 100%;
height: 100%;
display: block;
}
.progress-bar-overlay {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 5px;
background-color: rgba(51, 51, 51, 0.7);
}
.progress-bar {
height: 100%;
width: 0;
background-color: rgba(102, 102, 102, 0.9);
}
.console-container {
width: 1270px;
margin: 10px auto;
background-color: #000;
border: 5px solid rgb(20, 83, 20);
border-radius: 10px;
height: 16em;
overflow: hidden;
padding: 6px;
position: relative;
}
.console {
color: #00FF00;
font-family: "Courier New", Courier, monospace;
text-align: left;
line-height: 1.2em;
position: absolute;
bottom: 0;
width: 100%;
}
.fullscreen-video {
width: 100vw;
height: 100vh;
margin: 0;
border: none;
border-radius: 0;
}
</style>
</head>
<body>
<div id="appContainer">
<!-- The content will be dynamically updated based on the debug option -->
</div>
<script>
const debug = false; // Set to false to show only the video in fullscreen
const twitchChannel = "YOUR_TWITCH_CHANNEL";
const clientId = "YOUR_CLIENT_ID";
const clientSecret = "YOUR_CLIENT_SECRET";
let clips = [];
let currentClipIndex = 0;
const maxLogLines = 10;
function initializeApp() {
const appContainer = document.getElementById('appContainer');
appContainer.innerHTML = '';
if (debug) {
appContainer.innerHTML = `
<div class="video-container">
<video id="twitchClipPlayer" autoplay></video>
<div class="progress-bar-overlay">
<div class="progress-bar" id="progressBar"></div>
</div>
</div>
<div class="console-container" id="consoleContainer">
<div class="console" id="consoleOutput"></div>
</div>
`;
} else {
appContainer.innerHTML = `
<video id="twitchClipPlayer" class="fullscreen-video" autoplay></video>
`;
document.body.style.padding = "0";
}
}
function logToConsole(message) {
if (debug) {
const consoleOutput = document.getElementById('consoleOutput');
if (consoleOutput) {
const newLog = document.createElement('div');
newLog.innerText = message;
consoleOutput.appendChild(newLog);
const logLines = consoleOutput.children;
if (logLines.length > 10) {
consoleOutput.removeChild(logLines[0]);
}
consoleOutput.scrollTop = consoleOutput.scrollHeight;
}
}
}
async function fetchTwitchToken() {
logToConsole('Connecting to Twitch API...');
const response = await fetch('https://id.twitch.tv/oauth2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `client_id=${clientId}&client_secret=${clientSecret}&grant_type=client_credentials`
});
const data = await response.json();
logToConsole('Authorization successful.');
return data.access_token;
}
async function fetchBroadcasterID(token) {
logToConsole('Finding broadcaster ID...');
const response = await fetch(`https://api.twitch.tv/helix/users?login=${twitchChannel}`, {
headers: {
'Authorization': `Bearer ${token}`,
'Client-Id': clientId
}
});
const data = await response.json();
if (data.data && data.data.length > 0) {
logToConsole('Broadcaster ID found.');
return data.data[0].id;
} else {
throw new Error('Broadcaster ID not found.');
}
}
async function fetchTwitchClips(token, broadcasterId) {
logToConsole('Fetching clips...');
const response = await fetch(`https://api.twitch.tv/helix/clips?broadcaster_id=${broadcasterId}&first=100`, {
headers: {
'Authorization': `Bearer ${token}`,
'Client-Id': clientId
}
});
const data = await response.json();
if (data.data) {
logToConsole(`${data.data.length} clips found.`);
return data.data.map(clip => ({
url: clip.thumbnail_url.replace('-preview-480x272.jpg', '.mp4'),
title: clip.title,
creator_name: clip.creator_name,
created_at: clip.created_at,
game: clip.game_id
}));
} else {
throw new Error('No clips found.');
}
}
async function loadClips() {
try {
const token = await fetchTwitchToken();
const broadcasterId = await fetchBroadcasterID(token);
clips = await fetchTwitchClips(token, broadcasterId);
if (clips.length === 0) {
logToConsole('No clips found.');
return;
}
await fetchGameNames(token);
shuffleClips();
playNextClip();
} catch (error) {
logToConsole('Error loading clips.');
console.error('Error:', error);
}
}
async function fetchGameNames(token) {
logToConsole('Fetching category names...');
const gameIds = [...new Set(clips.map(clip => clip.game))];
if (gameIds.length > 0) {
const response = await fetch(`https://api.twitch.tv/helix/games?id=${gameIds.join('&id=')}`, {
headers: {
'Authorization': `Bearer ${token}`,
'Client-Id': clientId
}
});
const data = await response.json();
const gameMap = {};
data.data.forEach(game => {
gameMap[game.id] = game.name;
});
clips.forEach(clip => {
clip.game = gameMap[clip.game] || 'Unknown';
});
}
}
function shuffleClips() {
logToConsole('Shuffling clips...');
for (let i = clips.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[clips[i], clips[j]] = [clips[j], clips[i]];
}
logToConsole('Clips shuffled.');
}
function playNextClip() {
const clip = clips[currentClipIndex];
const formattedDate = new Date(clip.created_at).toLocaleDateString('en-US', { month: '2-digit', day: '2-digit', year: 'numeric' });
logToConsole(`Playing clip ${currentClipIndex + 1} of ${clips.length}:\nClipped by: ${clip.creator_name} on ${formattedDate} while streaming in the "${clip.game}" Category\nClip Title: ${clip.title}`);
const player = document.getElementById('twitchClipPlayer');
player.src = clip.url;
player.onended = () => {
currentClipIndex++;
if (currentClipIndex >= clips.length) {
currentClipIndex = 0;
shuffleClips();
}
logToConsole('Loading next clip...');
playNextClip();
};
if (debug) {
player.ontimeupdate = () => {
const progressBar = document.getElementById('progressBar');
const progress = (player.currentTime / player.duration) * 100;
progressBar.style.width = progress + '%';
};
}
}
// Initialize the app based on the debug setting
initializeApp();
loadClips();
</script>
</body>
</html>