From 0c4e62d5fb031e25768cbdef24aa902ebf12afb4 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Sun, 9 May 2021 00:49:52 +0200 Subject: [PATCH] add content-type --- stream.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/stream.py b/stream.py index 4abf6d7..a568ca1 100755 --- a/stream.py +++ b/stream.py @@ -47,9 +47,13 @@ if icecast_server is not None and stream_server is not None: playlist += stream_server + key + "\n" def rewrite(current, proxy): + resp = requests.head(current) + ctype = resp.headers.get("Content-Type", "text/plain").lower() + if "mpegurl" not in ctype: + return None resp = requests.get(current) - ndata = resp.text - if resp.text is not None and proxy_server is not None: + ndata = None + if resp.text is not None: ndata = "" for line in resp.text.splitlines(): if line.startswith("#"): @@ -58,10 +62,13 @@ def rewrite(current, proxy): data = {} data["upstream"] = urllib.parse.urljoin(current, line) data["proxy"] = proxy - presp = requests.post(proxy_server, json=data) - ndata += presp.text + if proxy is None or proxy_server is None: + ndata += data["upstream"] + else: + presp = requests.post(proxy_server, json=data) + ndata += presp.text ndata += "\n" - print(ndata) + return ndata class MainHandler(tornado.web.RequestHandler): def handle_any(self, write): @@ -95,8 +102,12 @@ class MainHandler(tornado.web.RequestHandler): if write: self.write("stream not found") else: - rewrite(upstream, proxy_raw) - self.redirect(upstream, status=303) + data = rewrite(upstream, proxy_raw) + if data is None: + self.redirect(upstream, status=303) + else: + self.set_header("Content-Type", "application/vnd.apple.mpegurl") + self.write(data) def get(self): self.handle_any(True) def head(self):