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):