-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
320 lines (270 loc) · 6.98 KB
/
index.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!doctype html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>KEXP</title>
<style>
* {
box-sizing: border-box;
}
body {
background-color: black;
color: #EEE;
font-size: 1.2rem;
line-height: 1.5rem;
margin: 0;
padding: 0;
min-height: 100vh;
min-width: 100vw;
display: flex;
align-items: center;
justify-content: center;
}
#container {
position: relative;
}
#bg {
filter: blur(50px) brightness(0.5);
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-color: black;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
height: 110vh;
width: 110vw;
pointer-events: none;
}
#info {
padding: 20px;
border: 5px gray ridge;
background-color: black;
display: flex;
align-items: center;
}
#artwork {
width: 200px;
height: 200px;
object-fit: cover;
object-position: center;
margin-right: 20px;
}
#info-inner {
vertical-align: middle;
display: inline-block;
}
#now-playing {
text-transform: uppercase;
font-weight: bold;
font-size: 1rem;
line-height: 1.3rem;
}
#artist {
font-weight: bold;
}
#song {
font-size: 1.6rem;
line-height: 1.9rem;
}
#release {
font-size: 1rem;
line-height: 1.3rem;
}
#player {
margin-top: 30px;
width: 100%;
}
#error {
color: #ffafaf;
font-weight: bold;
}
button:disabled,
button[disabled]{
color: #EEE;
}
</style>
</head>
<body>
<div id='bg'></div>
<div id='container'>
<div id='info'>
<img id='artwork' />
<div id='info-inner'>
<div id='now-playing'>Now Playing</div>
<div id='show'></div>
<br />
<div id='song'><span id='artist'></span> - <span id='title'></span></div>
<div id='release'></div>
<br />
<button id='updateButton'>Update Info</button> <button id='enableNotificationsButton'>Enable Notifications</button>
<br />
<div id='error'></div>
</div>
</div>
<audio id='player' controls />
</div>
<script>
const PLACEHOLDER_ARTWORK = 'https://kexp.org/static/assets/img/default.png';
const AIR_BREAK = 'Air Break';
let [
player,
show,
artist,
title,
release,
error,
artwork,
updateButton,
bg,
enableNotificationsButton
] = [
'player',
'show',
'artist',
'title',
'release',
'error',
'artwork',
'updateButton',
'bg',
'enableNotificationsButton'
].map(e => document.getElementById(e));
function getStreamURL() {
return `https://kexp-mp3-128.streamguys1.com/kexp128.mp3?${Date.now()}`;
}
function getAPIEndpoint(path = '', params = {}) {
let searchParams = new URLSearchParams();
for (let [k, v] of Object.entries(params)) {
searchParams.set(k, v);
}
return `https://api.kexp.org/v2/${path}?${searchParams.toString()}`;
}
async function fetchShows() {
let res = await fetch(getAPIEndpoint('shows', {
format: 'json',
limit: 1,
start_time_before: new Date().toISOString(),
}))
return res.json();
}
async function fetchPlays() {
let res = await fetch(getAPIEndpoint('plays', {
format: 'json',
limit: 1,
ordering: '-airdate',
airdate_before: new Date().toISOString(),
}))
return res.json();
}
function extractPlayMetadata(play) {
let metadata = {
title: play.song || '',
artist: play.artist || '',
artistUrl: '',
artworkUrl: play.thumbnail_uri || null,
program: '',
release: '',
airdate: play.airdate != null ? (new Date(play.airdate)) : null,
showId: play.show,
}
metadata.airdate = new Date(play.airdate);
if (['airbreak', 'Air Break', 'air break'].includes(play.play_type)) {
metadata.release = AIR_BREAK;
} else {
metadata.release = play.album || '';
}
return metadata;
}
function extractShowMetadata(show) {
let metadata = {
showId: show.id,
host: show.host_names.join(', '),
program: show.program_name,
airdate: new Date(show.airdate),
}
return metadata;
}
async function fetchNowPlaying() {
let [shows, plays] = await Promise.all([
fetchShows(),
fetchPlays(),
])
let show = extractShowMetadata(shows.results[0]);
let play = extractPlayMetadata(plays.results[0]);
let showHeader = `${show.program} with ${show.host}`;
return {
program: show.program,
host: show.host,
artist: play.artist,
title: play.title,
release: play.release,
artworkUrl: play.artworkUrl
}
}
let lastNowPlaying = null;
async function updateTitle() {
function resetButton() {
updateButton.disabled = false;
updateButton.innerText = 'Update Info';
}
updateButton.disabled = true;
updateButton.innerText = 'Updating Info...';
let nowPlaying;
try {
nowPlaying = await fetchNowPlaying();
} catch (err) {
resetButton();
error.textContent('Could not fetch show data - check console');
return;
}
resetButton();
error.textContent = '';
show.textContent = `${nowPlaying.program} with ${nowPlaying.host}`;
artist.textContent = nowPlaying.artist;
title.textContent = nowPlaying.title;
release.textContent = nowPlaying.release;
let nowPlayingText;
if (nowPlaying.release === AIR_BREAK) {
song.style.visibility = 'hidden';
nowPlayingText = AIR_BREAK;
} else {
song.style.visibility = 'visible';
nowPlayingText = `${nowPlaying.artist} - ${nowPlaying.title}`;
}
let artworkUrl = nowPlaying.artworkUrl || PLACEHOLDER_ARTWORK;
artwork.src = artworkUrl
bg.style['background-image'] = `url(${artworkUrl})`;
document.title = `KEXP: ${nowPlayingText}`;
if (Notification.permission === 'granted') {
if (lastNowPlaying === nowPlayingText) return;
if (lastNowPlaying === null) {
lastNowPlaying = nowPlayingText;
return;
}
lastNowPlaying = nowPlayingText;
new Notification('KEXP', {
body: `Now Playing: ${nowPlayingText}`,
icon: artworkUrl
})
}
}
setInterval(updateTitle, 1000 * 15);
updateTitle();
updateButton.onclick = updateTitle;
if (Notification.permission === 'granted') {
enableNotificationsButton.style.display = 'none';
}
enableNotificationsButton.onclick = async () => {
await Notification.requestPermission();
}
// every 3 hours or so, the livestream stops, so we just refresh the url every 1.5 hours
function refreshPlayer() {
player.pause()
player.src = getStreamURL();
player.play()
}
setInterval(refreshPlayer, 1000 * 60 * 60 * 1.5);
refreshPlayer();
</script>
</body>