fix error message
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
Roy Olav Purser 2022-02-05 21:56:06 +01:00
parent 78e5974a92
commit 52f51bc6d5
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -85,6 +85,14 @@ class UpstreamHandler:
future.cancel() future.cancel()
class NoDataError(Exception):
pass
class MissingTemplateError(Exception):
pass
class MainHandler(tornado.web.RequestHandler): class MainHandler(tornado.web.RequestHandler):
async def handle_any(self): async def handle_any(self):
handler = UpstreamHandler() handler = UpstreamHandler()
@ -99,11 +107,10 @@ class MainHandler(tornado.web.RequestHandler):
self.set_status(404) self.set_status(404)
self.write("Stream not found. (provider missing)") 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 video_info = None
meta = None if config.template_script is None or config.template_html is None:
title = None raise MissingTemplateError()
if config.template_script is not None and config.template_html is not None:
provider_data = None provider_data = None
if handler.provider.startswith("nextcloud"): if handler.provider.startswith("nextcloud"):
provider_data = await stream_providers.get_nextcloud( provider_data = await stream_providers.get_nextcloud(
@ -118,7 +125,7 @@ class MainHandler(tornado.web.RequestHandler):
handler.upstream, handler.proxy, logger handler.upstream, handler.proxy, logger
) )
if provider_data is None: if provider_data is None:
return (None, None, None) raise NoDataError()
video_info = {} video_info = {}
if handler.direct: if handler.direct:
@ -135,21 +142,31 @@ class MainHandler(tornado.web.RequestHandler):
video_info["poster"] = proxied[1] video_info["poster"] = proxied[1]
video_info["ctype"] = provider_data.ctype() video_info["ctype"] = provider_data.ctype()
meta = provider_data.meta() return (video_info, provider_data.meta(), provider_data.title())
title = provider_data.title()
return (video_info, meta, title)
async def handle_raw(self, handler): async def handle_raw(self, handler):
try:
video_info = (await self.get_data(handler))[0] video_info = (await self.get_data(handler))[0]
if video_info is not None:
self.redirect(url=video_info["upstream"], status=303) self.redirect(url=video_info["upstream"], status=303)
else: except NoDataError:
self.set_status(404) self.set_status(404)
self.write("HTML template missing.") self.write("HTML template missing.")
except MissingTemplateError:
self.set_status(404)
self.write("Video not found.")
async def handle_render(self, handler): async def handle_render(self, handler):
try:
video_info, meta, title = await self.get_data(handler) video_info, meta, title = await self.get_data(handler)
if video_info is not None: 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)) script = config.template_script.generate(info=json.dumps(video_info))
b64 = str(base64.b64encode(script), "ascii") b64 = str(base64.b64encode(script), "ascii")
data = {} data = {}
@ -158,13 +175,8 @@ class MainHandler(tornado.web.RequestHandler):
data["videojs_version"] = config.videojs_version data["videojs_version"] = config.videojs_version
data["chromecast_version"] = config.chromecast_version data["chromecast_version"] = config.chromecast_version
data["font_awesome_version"] = config.font_awesome_version data["font_awesome_version"] = config.font_awesome_version
rendered_html = config.template_html.generate( rendered_html = config.template_html.generate(data=data, meta=meta, title=title)
data=data, meta=meta, title=title
)
self.write(rendered_html) self.write(rendered_html)
else:
self.set_status(404)
self.write("HTML template missing.")
async def get(self): async def get(self):
await self.handle_any() await self.handle_any()