diff --git a/frontend/index.html b/frontend/index.html index 11df797..8f0c2b8 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,17 +1,7 @@ - - - - - - - - - - - - + {% for item in meta %} + {% block item %}{% end %}{% end %} diff --git a/frontend/script.js b/frontend/script.js index 3e45bb9..79e295e 100644 --- a/frontend/script.js +++ b/frontend/script.js @@ -1,12 +1,21 @@ -let url = new URL("{{ stream }}", window.location.origin); -let upstream = url.href; +(() => { + let url = new URL("{{ stream }}", window.location.origin); + let upstream = url.href; -document.addEventListener("DOMContentLoaded", () => { - let bodies = document.getElementsByTagName("body"); - if(bodies.length === 1) { - let body = bodies[0]; - let xhr = new XMLHttpRequest(); - xhr.addEventListener("load", () => { + let xhr = new XMLHttpRequest(); + xhr.open("HEAD", upstream, true); + xhr.send(); + + let count = 2; + let handleCount = () => { + if(--count === 0) { + handle(); + } + } + + let handle = () => { + let bodies = document.getElementsByTagName("body"); + if(bodies.length === 1) { let video = document.createElement("video"); video.className = "video-js vjs-big-play-centered"; body.appendChild(video); @@ -25,8 +34,9 @@ document.addEventListener("DOMContentLoaded", () => { source.type = ctype; source.src = upstream; player.src(source); - }); - xhr.open("HEAD", upstream, true); - xhr.send(); + } } -}); + + document.addEventListener("DOMContentLoaded", handleCount); + xhr.addEventListener("load", handleCount); +})(); diff --git a/stream.py b/stream.py index e301a9f..15ce084 100755 --- a/stream.py +++ b/stream.py @@ -106,14 +106,28 @@ class UpstreamHandler(): proxies[provider] = current_list return def meta(self): - data = {} + data = [] try: embed_url = f'https://noembed.com/embed?url={self.upstream_safe}' logger.info(embed_url) resp = requests.get(embed_url) data_raw = json.loads(resp.text) if isinstance(data_raw, dict): - data = data_raw + data_new = {} + data_valid = True + data_new["og:title"] = data_raw.get("title") + data_new["og:description"] = data_raw.get("author_name") + data_new["og:image"] = data_raw.get("thumbnail_url") + data_new["og:video:height"] = data_raw.get("height") + data_new["og:video:width"] = data_raw.get("width") + data_new["og:image:height"] = data_raw.get("thumbnail_height") + data_new["og:image:width"] = data_raw.get("thumbnail_width") + for elem in data_new: + if not isinstance(elem, str): + data_valid = False + break + if data_valid: + data = list(data_new.items()) except Exception as e: logger.info(e) return data @@ -237,22 +251,13 @@ class MainHandler(tornado.web.RequestHandler): b64_js = str(base64.b64encode(rendered_js), "ascii") script = f'data:text/javascript;charset=utf-8;base64,{b64_js}' meta = handler.meta() - data = {} - if meta is not None: - data["og:title"] = meta.get("title", "") - data["og:description"] = meta.get("author_name", "") - data["og:image"] = meta.get("thumbnail_url", "") - data["og:video:height"] = meta.get("height", "180") - data["og:video:width"] = meta.get("width", "320") - data["og:image:height"] = meta.get("thumbnail_height", "180") - data["og:image:width"] = meta.get("thumbnail_width", "320") data["script"] = script data["videojs_version"] = videojs_version data["chromecast_version"] = chromecast_version data["custom_style"] = custom_style data["stream_url"] = handler.stream_url data["render_url"] = handler.render_url - rendered_html = template_html.generate(data=data) + rendered_html = template_html.generate(data=data, meta=meta) self.write(rendered_html) else: self.set_status(404)