From 432b67eb4a5ef5a6970fcb1c65b3460dfce68b28 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Thu, 13 May 2021 13:29:49 +0200 Subject: [PATCH] separate embed endpoint --- frontend/index.html | 2 +- stream.py | 32 ++++++++++++++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/frontend/index.html b/frontend/index.html index 07834f7..983f457 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,7 +1,7 @@ - + diff --git a/stream.py b/stream.py index 0ed6894..3a45d93 100755 --- a/stream.py +++ b/stream.py @@ -72,6 +72,8 @@ if icecast_server is not None and stream_server is not None: template_html = None template_js = None +template_embed = tornado.template.Template('') + videojs_version = None castjs_version = None custom_style = None @@ -155,13 +157,13 @@ def rewrite(current, provider, proxy): class MainHandler(tornado.web.RequestHandler): def handle_any(self, write): provider = self.get_query_argument("provider", None) - render = self.get_query_argument("render", False) + render = self.get_query_argument("render", "false") + embed = self.get_query_argument("embed", "false") if isinstance(provider, str): - if isinstance(render, str): - if render.lower() == "true": - self.handle_render(provider, write) - else: - self.handle_stream(provider, write) + if render.lower() == "true": + self.handle_render(provider) + elif embed.lower() == "true": + self.handle_embed(provider) else: self.handle_stream(provider, write) else: @@ -170,23 +172,25 @@ class MainHandler(tornado.web.RequestHandler): if write: self.write("Stream not found. (provider missing)") - def handle_render(self, provider, write): + def handle_render(self, provider): if template_js is not None and template_html is not None: stream_path = f'{self.request.path}?provider={provider}' rendered_js = template_js.generate(stream=stream_path); b64_js = str(base64.b64encode(rendered_js), "ascii") script = f'data:text/javascript;charset=utf-8;base64,{b64_js}' - embed_json = {} - embed_json["version"] = "1.0" - embed_json["type"] = "video" - embed_json["html"] = f'' - b64_frame = str(base64.b64encode(bytes(json.dumps(embed_json), "utf-8")), "ascii") - oembed = f'data:application/json+oembed;charset=utf-8;base64,{b64_frame}' - rendered_html = template_html.generate(script=script, videojs_version=videojs_version, chromecast_version=chromecast_version, custom_style=custom_style, oembed=oembed) + rendered_html = template_html.generate(script=script, videojs_version=videojs_version, chromecast_version=chromecast_version, custom_style=custom_style, provider=provider, path=self.request.path) self.write(rendered_html) else: self.set_status(404) self.write("HTML template missing.") + def handle_embed(self, provider): + embed_json = {} + embed_json["version"] = "1.0" + embed_json["type"] = "video" + embed_json["html"] = f'' + embed_json["html"] = str(template_embed.generate(path=self.request.path, provider=provider), "utf-8") + self.set_header("Content-Type", "application/json+oembed; charset=utf-8") + self.write(json.dumps(embed_json)) def handle_stream(self, provider, write): upstream = None