diff --git a/stream.py b/stream.py index 18a9077..19f6d76 100755 --- a/stream.py +++ b/stream.py @@ -86,14 +86,12 @@ def get_proxy_url(proxy, current, path): presp = requests.post(proxy_server, json=data) return presp.text -def rewrite(current, provider, proxy): +def upstream_type(current, proxy): resp = requests.head(current, proxies=proxy.req) - ctype = resp.headers.get("Content-Type") - if ctype is None: - return None - else: - if "mpegurl" not in ctype.lower(): - return None + return resp.headers.get("Content-Type", "binary/octet-stream") + +def rewrite(current, provider, proxy, ctype): + resp = requests.get(current, proxies=proxy.req) ndata = None if resp.text is not None: @@ -203,7 +201,20 @@ class MainHandler(tornado.web.RequestHandler): if write: self.write("Stream not found.") else: - data = rewrite(upstream, provider, proxy) + ctype = upstream_type(upstream, proxy) + if "mpgurl" in ctype: + data = rewrite(upstream, provider, proxy) + else: + ldata = {} + ldata["upstream"] = upstream + ldata["proxy"] = proxy.proxy + ldata["proxied"] = isinstance(proxy.proxy, str) + links = [ldata] + resp = requests.post(proxy_server, json=links) + if isinstance(resp.text, list): + new_links = json.loads(resp.text) + if len(new_links) == 1: + upstream = new_links.pop() if data is None: self.redirect(upstream, status=303) else: