diff --git a/backend/stream.py b/backend/stream.py index e316175..8b4665c 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -5,6 +5,7 @@ import os import re import base64 import logging +import distutils.util import asyncio import tornado.web import tornado.routing @@ -120,6 +121,7 @@ stream_providers.setup(proxy_keys) class UpstreamHandler(): def __init__(self): self.provider = None + self.raw = False self.valid = False self.proxy = ProxyElem(None) self.upstream = None @@ -141,6 +143,12 @@ class UpstreamHandler(): return (True, proxy) async def setup(self, handler): self.provider = handler.get_query_argument("provider", None) + raw_str = handler.get_query_argument("raw", None) + if isinstance(raw_str, str): + try: + self.raw = bool(distutils.util.strtobool(raw_str)) + except ValueError as e: + logger.info(e) if self.provider in providers.keys(): self.valid = True path = handler.request.path @@ -211,13 +219,18 @@ class MainHandler(tornado.web.RequestHandler): handler = UpstreamHandler() await handler.setup(self) if handler.valid: - await self.handle_render(handler) + if handler.raw: + await self.handle_raw(handler) + else: + await self.handle_render(handler) else: logger.info(f'provider missing {self.request.uri}') self.set_status(404) self.write("Stream not found. (provider missing)") - - async def handle_render(self, handler): + async def get_data(self, handler): + video_info = None + meta = None + title = None if template_script is not None and template_html is not None: provider_data = None if handler.provider.startswith("nextcloud"): @@ -227,26 +240,35 @@ class MainHandler(tornado.web.RequestHandler): else: provider_data = await stream_providers.get_any(handler.upstream, handler.proxy, logger) proxied = await handler.proxy.proxy_url([provider_data.upstream(), provider_data.thumbnail()]) - video_info = {} 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) + async def handle_raw(self, handler): + video_info, meta, title = await self.get_data(handler) + if video_info is not None: + self.redirect(url=video_info["upstream"], status=303) + else: + self.set_status(404) + self.write("HTML template missing.") + async def handle_render(self, handler): + video_info, meta, title = await self.get_data(handler) + if video_info is not None: script = template_script.generate(info=json.dumps(video_info)) b64 = str(base64.b64encode(script), "ascii") script_file = f'data:text/javascript;charset=utf-8;base64,{b64}' - data["script"] = script_file data["videojs_version"] = videojs_version data["chromecast_version"] = chromecast_version data["font_awesome_version"] = font_awesome_version - rendered_html = template_html.generate(data=data, meta=provider_data.meta(), title=provider_data.title()) + rendered_html = template_html.generate(data=data, meta=meta, title=title) self.write(rendered_html) else: self.set_status(404) self.write("HTML template missing.") - async def get(self): await self.handle_any(True) async def head(self): diff --git a/tv.json b/tv.json index c501948..9a7abb3 100644 --- a/tv.json +++ b/tv.json @@ -1,6 +1,6 @@ { - "/direkte/nrk1?provider=nrk": {"radio": false, "name": "NRK 1"}, - "/direkte/nrk2?provider=nrk": {"radio": false, "name": "NRK 2"}, - "/direkte/nrk3?provider=nrk": {"radio": false, "name": "NRK 3"}, - "/direkte/nrksuper?provider=nrk": {"radio": false, "name": "NRK Super"} + "/direkte/nrk1?provider=nrk&raw=true": {"radio": false, "name": "NRK 1"}, + "/direkte/nrk2?provider=nrk&raw=true": {"radio": false, "name": "NRK 2"}, + "/direkte/nrk3?provider=nrk&raw=true": {"radio": false, "name": "NRK 3"}, + "/direkte/nrksuper?provider=nrk&raw=true": {"radio": false, "name": "NRK Super"} }