-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.jl
229 lines (190 loc) · 6.14 KB
/
utils.jl
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
function hfun_bar(vname)
val = Meta.parse(vname[1])
return round(sqrt(val), digits=2)
end
function hfun_m1fill(vname)
var = vname[1]
return pagevar("index", var)
end
function lx_baz(com, _)
# keep this first line
brace_content = Franklin.content(com.braces[1]) # input string
# do whatever you want here
return uppercase(brace_content)
end
# Thanks @tlienart
using Franklin, JSON
using Markdown, Dates
include("youtube_videos.jl")
const DATEFMT = dateformat"yyyy-mm-dd HH:MMp"
const TZ = "America/New_York"
function hfun_doc(params)
fname = join(params[1:max(1, length(params)-2)], " ")
head = params[end-1]
type = params[end]
doc = eval(Meta.parse("@doc $fname"))
txt = Markdown.plain(doc)
# possibly further processing here
body = Franklin.fd2html(txt, internal=true)
return """
<div class="docstring">
<h2 class="doc-header" id="$head">
<a href="#$head">$head</a>
<div class="doc-type">$type</div></h2>
<div class="doc-content">$body</div>
</div>
"""
end
function hfun_youtube_placeholder(params)
id = params[1]
return """
<div id="videoContainer" >
<div id="player"></div>
</div>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '300',
width: '100%',
videoId: '$(get(videos, id, id))',
playerVars: {
'autoplay': 0,
'rel': 0,
'cc_load_policy': 1
}
});
}
function changeYouTubeSource(startTime, endTime) {
var youtubeIframe = document.getElementById('player');
var youtubeIframeSrc = document.getElementById('player').getAttribute('src');
var trimmedIframeUrl = '';
var iframeUrlTimeStamp = '';
if (youtubeIframeSrc.match(/&start=/g)) {
var mediaFragmentIndex = youtubeIframeSrc.indexOf('&start=');
trimmedIframeUrl = youtubeIframeSrc.slice(0, mediaFragmentIndex);
if (endTime === 0) {
iframeUrlTimeStamp = trimmedIframeUrl + '&start=' + startTime;
} else {
iframeUrlTimeStamp = trimmedIframeUrl + '&start=' + startTime + '&end=' + endTime;
}
}
if (youtubeIframeSrc.match(/&start=/g) === null) {
if (endTime === 0) {
iframeUrlTimeStamp = youtubeIframeSrc + '&start=' + startTime;
} else {
iframeUrlTimeStamp = youtubeIframeSrc + '&start=' + startTime + '&end=' + endTime;
}
}
setTimeout(function() {
var iframeAutoplayUrl = iframeUrlTimeStamp.replace('autoplay=0', 'autoplay=1');
youtubeIframe.setAttribute('src', iframeAutoplayUrl);
}, 1000);
}
</script>
"""
end
function hfun_yt_tsp(params)
start = params[1]
final = params[2]
s = Time(0) + Second(start)
if second(s) < 10
display = "$(minute(s)):0$(second(s))"
elseif hour(s) < 1
display = "$(minute(s)):$(second(s))"
else
display = display = "$(hour(s)):0$(minute(s)):$(second(s))"
end
title = join(params[3:end], " ")
return """
<br>
<a href='#player' onclick='changeYouTubeSource($start,$final)'> $display</a> $title
"""
end
function hfun_youtube(params)
id = params[1]
return """
<iframe id="$id" width="100%" height="300"
src="https://www.youtube.com/embed/$(get(videos, id, id))"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
"""
end
function hfun_youtube_start(params)
id = params[1]
start = params[2]
return """
<iframe id="$id" width="50%" height="30"
src="https://www.youtube.com/embed/$(get(videos, id, id))?start=$start"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
"""
end
function hfun_showtime(params)
id = params[1]
str = locvar(id)
if isnothing(str)
@warn "Unknown datetime variable $str"
return ""
end
try
DateTime(str, DATEFMT)
catch err
@warn "There was an error parsing date $str, the format is yyyy-mm-dd HH:MMp (see ?DateFormat)"
rethrow(err)
end
end
function parse_duration(str)
str = replace(str, r"^PT"=>"")
hrex, mrex, srex = Regex.(string.("^([0-9]+)", ["H","M","S"]))
t = 0
hmatch = match(hrex, str)
if !isnothing(hmatch)
h = parse(Int, hmatch[1])
t += 60*60*h
str = replace(str, hrex=>"")
end
mmatch = match(mrex, str)
if !isnothing(mmatch)
m = parse(Int, mmatch[1])
t += 60*m
str = replace(str, mrex=>"")
end
smatch = match(srex, str)
if !isnothing(smatch)
s = parse(Int, smatch[1])
t += s
str = replace(str, srex=>"")
end
t
end
function hfun_go_live()
seq = locvar("sequence")
airtime = locvar("airtime")
if isnothing(seq)
@warn "airtime set, but no `sequence` variable not defined." *
"sequence is an array of video IDs to play in order on this page"
end
vid_ids = [get(videos, s, s) for s in seq]
f = tempname()
# Get the duration of each video
download("https://www.googleapis.com/youtube/v3/videos?id=$(join(vid_ids, ","))&part=contentDetails&key=AIzaSyDZhbWHc2PTEFTx173MaTgddnWCGPqdbB8", f)
dict = JSON.parse(String(read(f)))
durations = [parse_duration(video["contentDetails"]["duration"])
for video in dict["items"]]
jrepr(x) = sprint(io->JSON.print(io, x))
"""
<script src="/assets/moment.min.js"></script>
<script src="/assets/moment-timezone.js"></script>
<script src="/assets/live-player.js"></script>
<script>
play_live($(jrepr(string(DateTime(airtime, DATEFMT)))), $(jrepr(TZ)), $(jrepr(seq)), $(jrepr(durations)))
</script>
"""
end