-
Notifications
You must be signed in to change notification settings - Fork 0
/
livestream.html
250 lines (220 loc) · 9.38 KB
/
livestream.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
<html lang="en">
<head>
<title>Legacy Church Livestream Test</title>
<!-- bl: Clover includes jQuery automatically, so adding this here for testing, but it should
NOT be copied to the website. -->
<script
src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
crossorigin="anonymous"></script>
<!-- bl: copy this into the "Custom <HEAD> Code" under Design Settings. this will apply to every page on the site. -->
<style>
.timer, #livestream {
margin: 0 auto;
text-align: center;
color: rgba(26,21,26,0.7);
font-weight: 100;
}
div.time {
display: inline-block;
line-height: 1;
padding: 20px;
}
span.countdown-label {
display: block;
font-size: 20px;
color: rgba(26,21,26,1);
}
#bodyTimer .time {
font-size: 100px;
}
#bodyTimer .time.seconds {
font-size: 50px;
}
#headerTimerSection .time {
font-size: 30px;
}
#headerTimerSection .time.seconds {
font-size: 20px;
}
#headerTimerDesc {
margin-top: 20px;
}
#headerTimerDesc, .livestream-welcome {
font-weight: bold;
font-size: 30px;
}
#bodyTimer, #livestream {
margin-top: -50px;
}
#livestreamIntro {
margin-bottom: 20px;
}
#headerTimerSection, #text-4897140, #livestream {
display: none;
}
#text-4897140 header {
display: none;
}
span.live-date {
color: #f77a4d;
}
.video-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<!-- bl: copy this into the end of the header section on the home page, AFTER the style above -->
<!-- bl: START header section -->
<div id="headerTimerSection">
<div class="timer">
<div id="headerTimerDesc">Legacy will be live again <span class="on-live-date">on <span class="live-date">X</span></span>, in:</div>
<div class="time days"></div>
<div class="time hours"></div>
<div class="time minutes"></div>
<div class="time seconds"></div>
</div>
</div>
<!-- bl: END header section -->
<div id="text-4897140">
<!-- bl: copy this into the livestream section body BELOW the header and the logo image on the home page -->
<!-- bl: IMPORTANT: this section (article#text-4897140) is HIDDEN in Clover, so you can't even see it. you'll need
to use the browser's DOM inspector to show it in order to edit the HTML contents! the section should appear
after the large image section (article#text-931818) -->
<!-- bl: START livestream countdown / body section -->
<div id="bodyTimer" class="timer">
<div class="livestream-welcome">Join us for our livestream here in:</div>
<div class="time days"></div>
<div class="time hours"></div>
<div class="time minutes"></div>
<div class="time seconds"></div>
</div>
<div id="livestream">
<div id="livestreamIntro" class="livestream-welcome">Join us now streaming live!</div>
<div id="livestreamPlayer" class="video-container"></div>
</div>
<!-- bl: END livestream countdown / body section -->
</div>
<!-- bl: copy this into the "Custom <HEAD> Code" under Design Settings. this will apply to every page on the site. -->
<!-- bl: START custom livestream scripts -->
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"
crossorigin="anonymous"></script>
<script type="text/javascript">
$(function() {
if (document.location.pathname !== '/' && document.location.pathname !== '/home') {
return;
}
// bl: in Luxon, Sunday is 7 (vs. 0 for JS Date)
const liveDay = 7;
const liveHour = 11;
const liveWindowHours = 1;
const liveWindowMinutes = 30;
function formatCountdownElement(amount, label, showLeadingZero) {
return (showLeadingZero && amount < 10 ? "0" + amount : amount) + "<span class='countdown-label'>" + label + "</span>";
}
function legacyTime() {
return luxon.DateTime.now().setZone('America/Denver').set({millisecond: 0});
}
async function isYouTubeLive() {
try {
// bl: there is actually a limit on the search API usage. we may hit it, so gracefully fall back if it happens.
const response = await fetch('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCDJ5xNBQ0fGnKJI5YgxAjLg&type=video&eventType=live&key=AIzaSyAxnVaGs_dBDBGDSs2GCt-K7f9fD2vc92Q');
const json = await response.json();
// bl: we'll show YouTube if we can't interact in the API for some reason (no JSON, no items).
// but if we successfully get no items, then we won't show the YouTube video yet.
return !json || !json.items || json.items.length > 0;
} catch(e) {
// bl: if there is an error, let's assume YouTube is live.
return true;
}
}
const headerTimerSection = $('#headerTimerSection');
// bl: this ID is from the test page.
const livestreamSection = $('#text-4897140');
const bodyTimer = $('#bodyTimer');
const livestream = $('#livestream');
const livestreamPlayer = $('#livestreamPlayer');
const imageSlide = $('#gallery-4871384');
const onLiveDate = $('.on-live-date');
const liveDate = $('.live-date');
const daysCount = $('.days');
const hoursCount = $('.hours');
const minutesCount = $('.minutes');
const secondsCount = $('.seconds');
function showLivestreamSection() {
headerTimerSection.hide();
imageSlide.hide();
livestreamSection.show();
}
async function makeTimer() {
let now = legacyTime();
const isLive = now.weekday === liveDay && now.hour >= liveHour && now.hour <= (liveHour + liveWindowHours) && (now.hour < (liveHour + liveWindowHours) || now.minute < liveWindowMinutes);
if (isLive) {
const isStreaming = await isYouTubeLive();
clearInterval(intervalId);
if (isStreaming) {
bodyTimer.hide();
showLivestreamSection();
livestream.show();
if (livestreamPlayer.is(':empty')) {
livestreamPlayer.html('<iframe width="560" height="315" src="https://www.youtube.com/embed/live_stream?channel=UCDJ5xNBQ0fGnKJI5YgxAjLg" frameborder="0" allowfullscreen=""></iframe>');
}
return;
}
// bl: if it's not streaming yet, then try again in 10 seconds.
setTimeout(function() { makeTimer(); }, 10000);
}
livestream.hide();
if (!livestreamPlayer.is(':empty')) {
livestreamPlayer.html('');
}
let endTime = legacyTime();
if (isLive) {
endTime = now;
onLiveDate.hide();
} else {
// get the next start date
const isSundayPreStream = now.weekday === liveDay && now.hour < liveHour;
if (!isSundayPreStream) {
endTime = endTime.plus({day: now.weekday === 7 ? 7 : 7 - now.weekday});
}
endTime = endTime.set({hour: liveHour, minute: 0, second: 0, millisecond: 0});
liveDate.html(endTime.toLocaleString(luxon.DateTime.DATETIME_FULL));
}
const secondsLeft = endTime.toUnixInteger() - now.toUnixInteger();
const days = Math.floor(secondsLeft / 86400);
const hours = Math.floor((secondsLeft - (days * 86400)) / 3600);
const minutes = Math.floor((secondsLeft - (days * 86400) - (hours * 3600 )) / 60);
const seconds = Math.floor((secondsLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));
daysCount.html(formatCountdownElement(days, days === 1 ? "Day" : "Days", false));
hoursCount.html(formatCountdownElement(hours, hours === 1 ? "Hour" : "Hours", true));
minutesCount.html(formatCountdownElement(minutes, minutes === 1 ? "Minute" : "Minutes", true));
secondsCount.html(formatCountdownElement(seconds, seconds === 1 ? "Second" : "Seconds", true));
// bl: within 30 minutes of start, show the countdown timer in its own section
if (secondsLeft <= 1800) {
showLivestreamSection();
} else {
headerTimerSection.show();
}
}
// bl: start the timer immediately on load
makeTimer();
const intervalId = setInterval(function() { makeTimer(); }, 1000);
});
</script>
<!-- bl: END custom livestream scripts -->
</body>
</html>