base64 encode script

This commit is contained in:
Roy Olav Purser 2021-05-12 09:50:51 +02:00
parent e28fcf1ad4
commit d18364f4cd
Signed by: roypur
GPG Key ID: E14D26A036F21656
4 changed files with 25 additions and 18 deletions

View File

@ -4,6 +4,7 @@ RUN ["mkdir", "/app"]
COPY ["stream.py", "/app/stream.py"]
COPY ["sources.py", "/app/sources.py"]
COPY ["index.html", "/app/index.html"]
COPY ["script.js", "/app/script.js"]
RUN ["chmod", "-R", "755", "/app"]
COPY ["tv.json", "/app/tv.json"]
RUN ["python3", "/app/sources.py"]

View File

@ -2,22 +2,9 @@
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/video.js/{{ videojs_version }}/video-js.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/video.js/{{ videojs_version }}/video.min.js"></script>
<script src="{{ script }}"></script>
</head>
<body>
<video class="video-js" id="player" controls>
</video>
<script>
let xhr = new XmlHttpRequest();
xhr.onreadystatechange = () => {
let ctype = xhr.getResponseHeader("Content-Type");
let player = videojs("player");
let source = {};
source.type = ctype;
source.src = "{{ stream }}";
player.src(source);
}
xhr.open("HEAD", "{{ stream }}", true);
xhr.send();
</script>
<video class="video-js" id="player" controls></video>
</body>
</html>

13
script.js Normal file
View File

@ -0,0 +1,13 @@
document.addEventListener("DOMContentLoaded", () => {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
let ctype = xhr.getResponseHeader("Content-Type");
let player = videojs("player");
let source = {};
source.type = ctype;
source.src = "{{ stream }}";
player.src(source);
}
xhr.open("HEAD", "{{ stream }}", true);
xhr.send();
});

View File

@ -62,11 +62,14 @@ if icecast_server is not None and stream_server is not None:
playlist += f'#EXTINF:0 radio="false", {name}\n'
playlist += stream_server + key + "\n"
template = None
template_html = None
template_js = None
videojs_version = None
try:
with open("/app/index.html", "r") as f:
template = tornado.template.Template(f.read().strip())
template_html = tornado.template.Template(f.read().strip())
with open("/app/script.js", "r") as f:
template_js = tornado.template.Template(f.read().strip())
with open("/app/videojs-version.txt", "r") as f:
videojs_version = f.read().strip()
except Exception as e:
@ -153,7 +156,10 @@ class MainHandler(tornado.web.RequestHandler):
def handle_render(self, provider, write):
if template is not None:
stream_path = f'{self.request.path}?provider={provider}'
rendered = template.generate(stream=stream_path, videojs_version=videojs_version)
rendered_js = template_js.generate(stream=stream_path);
b64 = str(base64.b64encode(rendered_js), "ascii")
script = f'data:text/javascript;charset=utf8;base64,{b64}'
rendered_html = template_html.generate(script=script, videojs_version=videojs_version)
self.write(rendered)
else:
self.set_status(404)