diff --git a/backend/stream.py b/backend/stream.py index f338b25..cb44e07 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -85,6 +85,14 @@ class UpstreamHandler: future.cancel() +class NoDataError(Exception): + pass + + +class MissingTemplateError(Exception): + pass + + class MainHandler(tornado.web.RequestHandler): async def handle_any(self): handler = UpstreamHandler() @@ -99,72 +107,76 @@ class MainHandler(tornado.web.RequestHandler): self.set_status(404) self.write("Stream not found. (provider missing)") - async def get_data(self, handler): + async def get_data(self, handler) -> tuple[dict[str, str], str, str]: video_info = None - meta = None - title = None - if config.template_script is not None and config.template_html is not None: - provider_data = None - if handler.provider.startswith("nextcloud"): - provider_data = await stream_providers.get_nextcloud( - handler.upstream, handler.proxy, logger - ) - elif handler.provider.startswith("seafile"): - provider_data = await stream_providers.get_seafile( - handler.upstream, handler.proxy, logger - ) - else: - provider_data = await stream_providers.get_any( - handler.upstream, handler.proxy, logger - ) - if provider_data is None: - return (None, None, None) + if config.template_script is None or config.template_html is None: + raise MissingTemplateError() + provider_data = None + if handler.provider.startswith("nextcloud"): + provider_data = await stream_providers.get_nextcloud( + handler.upstream, handler.proxy, logger + ) + elif handler.provider.startswith("seafile"): + provider_data = await stream_providers.get_seafile( + handler.upstream, handler.proxy, logger + ) + else: + provider_data = await stream_providers.get_any( + handler.upstream, handler.proxy, logger + ) + if provider_data is None: + raise NoDataError() - video_info = {} - if handler.direct: - video_info["upstream"] = provider_data.upstream() - video_info["poster"] = provider_data.thumbnail() - else: - proxied = await handler.proxy.proxy_url( - [ - (provider_data.upstream(), provider_data.proxy_ctype()), - provider_data.thumbnail(), - ] - ) - video_info["upstream"] = proxied[0] - video_info["poster"] = proxied[1] + video_info = {} + if handler.direct: + video_info["upstream"] = provider_data.upstream() + video_info["poster"] = provider_data.thumbnail() + else: + proxied = await handler.proxy.proxy_url( + [ + (provider_data.upstream(), provider_data.proxy_ctype()), + provider_data.thumbnail(), + ] + ) + video_info["upstream"] = proxied[0] + video_info["poster"] = proxied[1] - video_info["ctype"] = provider_data.ctype() - meta = provider_data.meta() - title = provider_data.title() - return (video_info, meta, title) + video_info["ctype"] = provider_data.ctype() + return (video_info, provider_data.meta(), provider_data.title()) async def handle_raw(self, handler): - video_info = (await self.get_data(handler))[0] - if video_info is not None: + try: + video_info = (await self.get_data(handler))[0] self.redirect(url=video_info["upstream"], status=303) - else: + except NoDataError: self.set_status(404) self.write("HTML template missing.") + except MissingTemplateError: + self.set_status(404) + self.write("Video not found.") async def handle_render(self, handler): - video_info, meta, title = await self.get_data(handler) - if video_info is not None: - script = config.template_script.generate(info=json.dumps(video_info)) - b64 = str(base64.b64encode(script), "ascii") - data = {} - script_file = f"data:text/javascript;charset=utf-8;base64,{b64}" - data["script"] = script_file - data["videojs_version"] = config.videojs_version - data["chromecast_version"] = config.chromecast_version - data["font_awesome_version"] = config.font_awesome_version - rendered_html = config.template_html.generate( - data=data, meta=meta, title=title - ) - self.write(rendered_html) - else: + try: + video_info, meta, title = await self.get_data(handler) + except NoDataError: self.set_status(404) self.write("HTML template missing.") + return + except MissingTemplateError: + self.set_status(404) + self.write("Video not found.") + return + + script = config.template_script.generate(info=json.dumps(video_info)) + b64 = str(base64.b64encode(script), "ascii") + data = {} + script_file = f"data:text/javascript;charset=utf-8;base64,{b64}" + data["script"] = script_file + data["videojs_version"] = config.videojs_version + data["chromecast_version"] = config.chromecast_version + data["font_awesome_version"] = config.font_awesome_version + rendered_html = config.template_html.generate(data=data, meta=meta, title=title) + self.write(rendered_html) async def get(self): await self.handle_any()