From d418b429aa92d1b5f1731de2189fa9f1ea6fb9e5 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Tue, 25 May 2021 09:39:34 +0200 Subject: [PATCH] load thumbnail --- backend/stream.py | 32 +++++++++++++++++++++++++------- backend/stream_providers.py | 31 +++++++++++++------------------ frontend/script.js | 15 +++++++++++++-- 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/backend/stream.py b/backend/stream.py index 2a13447..32cf142 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -146,14 +146,16 @@ class UpstreamHandler(): self.upstream_safe = None self.render = False self.stream = False + self.poster = False async def setup(self, handler): self.provider = handler.get_query_argument("provider", None) render_str = handler.get_query_argument("render", "false") - self.render = False - self.stream = False + poster_str = handler.get_query_argument("poster", "false") if self.provider in providers.keys(): if render_str.lower() == "true": self.render = True + elif poster_str.lower() == "true": + self.poster = True else: self.stream = True @@ -203,7 +205,7 @@ class UpstreamHandler(): await session.close() async def meta(self): - data = [] + data = {} try: embed_url = f'https://noembed.com/embed?url={self.upstream_safe}' async with self.proxy.session() as session: @@ -230,12 +232,14 @@ class UpstreamHandler(): text = await resp.text() parser = MetaParser() parser.feed(text) - data_filtered = parser.meta_data - data = list(data_filtered.items()) + data = parser.meta_data + data = data_filtered except Exception as e: logger.info(e) - logger.info(data) return data + async def meta_list(self): + meta_data = await self.meta() + return list(meta_data.items()) if icecast_server is not None and stream_server is not None: try: @@ -344,6 +348,8 @@ class MainHandler(tornado.web.RequestHandler): await self.handle_render(handler) elif handler.stream: await self.handle_stream(handler, redir) + elif handler.poster: + await self.handle_poster(handler) else: logger.info(f'provider missing {self.request.uri}') self.set_status(404) @@ -351,13 +357,14 @@ class MainHandler(tornado.web.RequestHandler): async def handle_render(self, handler): if script_file is not None and template_html is not None: - meta = await handler.meta() + meta = await handler.meta_list() title = handler.render_url video_title = None for elem in meta: if isinstance(elem[1], str): if elem[0] == "og:title": video_title = elem[1] + break data["script"] = script_file data["videojs_version"] = videojs_version @@ -370,6 +377,17 @@ class MainHandler(tornado.web.RequestHandler): self.set_status(404) self.write("HTML template missing.") + async def handle_poster(self, handler): + meta = await handler.meta() + image = meta.get("og:image") + if isinstance(image, str): + url = await handler.proxy.proxy_url(image, None) + if isinstance(url, str): + self.redirect(url, status=303) + return + self.set_status(404) + self.write("Poster not found") + async def handle_stream(self, handler, redir): upstream = None if handler.provider == "nextcloud": diff --git a/backend/stream_providers.py b/backend/stream_providers.py index 6bd70d1..aec38d3 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -69,22 +69,14 @@ class YoutubeRunner(StreamProvider): return await asyncio.to_thread(self.stream) async def get_ytdl(upstream, proxy, logger): - url = None - try: - runner = YoutubeRunner(upstream, proxy) - url = await runner.run() - except Exception as e: - logger.info(e) - return url - + runner = YoutubeRunner(upstream, proxy) + result = await runner.run() + return result + async def get_streamlink(upstream, proxy, logger): - url = None - try: - runner = StreamlinkRunner(upstream, proxy) - url = await runner.run() - except Exception as e: - logger.info(e) - return url + runner = StreamlinkRunner(upstream, proxy) + result = await runner.run() + return result async def get_any(upstream, proxy, logger): tasks = [] @@ -92,9 +84,12 @@ async def get_any(upstream, proxy, logger): tasks.append(asyncio.create_task(get_ytdl(upstream, proxy, logger))) result = None for task in asyncio.as_completed(tasks, timeout=5.0): - result = await task - if isinstance(result, str): - break + try: + result = await task + if isinstance(result, str): + break + except Exception as e: + logger.info(e) for task in tasks: if not task.done(): task.cancel() diff --git a/frontend/script.js b/frontend/script.js index 4f3c6d2..a612291 100644 --- a/frontend/script.js +++ b/frontend/script.js @@ -1,14 +1,24 @@ (() => { - const findUpstream = () => { + const findUpstreamVideo = () => { const search = new URLSearchParams(location.search); search.set("render", "false"); + search.set("poster", "false"); + const url = new URL(location.origin); + url.pathname = location.pathname; + url.search = search.toString(); + return url.href; + } + const findUpstreamPoster = () => { + const search = new URLSearchParams(location.search); + search.set("render", "false"); + search.set("poster", "true"); const url = new URL(location.origin); url.pathname = location.pathname; url.search = search.toString(); return url.href; } - const upstream = findUpstream(); + const upstream = findUpstreamVideo(); const xhr = new XMLHttpRequest(); xhr.open("HEAD", upstream, true); xhr.send(); @@ -37,6 +47,7 @@ options.plugins.chromecast = {}; options.plugins.chromecast.addButtonToControlBar = false; const player = videojs(video, options); + player.poster(findUpstreamPoster()); const source = {}; source.type = ctype; source.src = upstream;