This commit is contained in:
parent
78e5974a92
commit
52f51bc6d5
@ -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,72 +107,76 @@ 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(
|
handler.upstream, handler.proxy, logger
|
||||||
handler.upstream, handler.proxy, logger
|
)
|
||||||
)
|
elif handler.provider.startswith("seafile"):
|
||||||
elif handler.provider.startswith("seafile"):
|
provider_data = await stream_providers.get_seafile(
|
||||||
provider_data = await stream_providers.get_seafile(
|
handler.upstream, handler.proxy, logger
|
||||||
handler.upstream, handler.proxy, logger
|
)
|
||||||
)
|
else:
|
||||||
else:
|
provider_data = await stream_providers.get_any(
|
||||||
provider_data = await stream_providers.get_any(
|
handler.upstream, handler.proxy, logger
|
||||||
handler.upstream, handler.proxy, logger
|
)
|
||||||
)
|
if provider_data is None:
|
||||||
if provider_data is None:
|
raise NoDataError()
|
||||||
return (None, None, None)
|
|
||||||
|
|
||||||
video_info = {}
|
video_info = {}
|
||||||
if handler.direct:
|
if handler.direct:
|
||||||
video_info["upstream"] = provider_data.upstream()
|
video_info["upstream"] = provider_data.upstream()
|
||||||
video_info["poster"] = provider_data.thumbnail()
|
video_info["poster"] = provider_data.thumbnail()
|
||||||
else:
|
else:
|
||||||
proxied = await handler.proxy.proxy_url(
|
proxied = await handler.proxy.proxy_url(
|
||||||
[
|
[
|
||||||
(provider_data.upstream(), provider_data.proxy_ctype()),
|
(provider_data.upstream(), provider_data.proxy_ctype()),
|
||||||
provider_data.thumbnail(),
|
provider_data.thumbnail(),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
video_info["upstream"] = proxied[0]
|
video_info["upstream"] = proxied[0]
|
||||||
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):
|
||||||
video_info = (await self.get_data(handler))[0]
|
try:
|
||||||
if video_info is not None:
|
video_info = (await self.get_data(handler))[0]
|
||||||
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):
|
||||||
video_info, meta, title = await self.get_data(handler)
|
try:
|
||||||
if video_info is not None:
|
video_info, meta, title = await self.get_data(handler)
|
||||||
script = config.template_script.generate(info=json.dumps(video_info))
|
except NoDataError:
|
||||||
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:
|
|
||||||
self.set_status(404)
|
self.set_status(404)
|
||||||
self.write("HTML template missing.")
|
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):
|
async def get(self):
|
||||||
await self.handle_any()
|
await self.handle_any()
|
||||||
|
Loading…
Reference in New Issue
Block a user