From 88329581127330a438b7987a38a55b6b835425b7 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Fri, 4 Jun 2021 08:54:56 +0200 Subject: [PATCH] follow redirect for content-type detection --- backend/stream.py | 15 ++------------- backend/stream_providers.py | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/backend/stream.py b/backend/stream.py index 28d1be8..c26a20b 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -223,19 +223,8 @@ class MainHandler(tornado.web.RequestHandler): video_info = {} video_info["upstream"] = proxied[0] video_info["poster"] = proxied[1] - - if isinstance(provider_data.ctype(), str): - video_info["ctype"] = provider_data.ctype() - else: - try: - async with handler.proxy.local() as session: - resp = await session.head(proxied[0]) - ctype = resp.headers.get("Content-Type", None) - if isinstance(ctype, str): - video_info["ctype"] = ctype - except Exception as e: - logger.info(e) - + video_info["ctype"] = provider_data.ctype() + 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}' diff --git a/backend/stream_providers.py b/backend/stream_providers.py index d6664c3..081b9d2 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -117,18 +117,20 @@ class StreamProvider(): proxies = {} proxies["http"] = "socks5://" + self.proxy proxies["https"] = "socks5://" + self.proxy - ctype = "binary/octet-stream" + ctype = None + upstream = data.upstream() try: - resp = requests.head(data.upstream(), proxies=proxies, timeout=5) + resp = requests.head(data.upstream(), proxies=proxies, timeout=5, allow_redirects=True) except Exception as e: self.logger.info("%s <%s>", e, self.upstream) else: - ctype = resp.headers.get("Content-Type", "text/plain").lower() - if ctype.startswith("text"): - self.logger.info("Bad Content-Type %s", data.upstream()) - ctype = None - elif "mpegurl" in ctype: - ctype = "application/vnd.apple.mpegurl" + if resp.ok: + upstream = resp.url + ctype = resp.headers.get("Content-Type", "text/plain").lower() + if ctype.startswith("text"): + ctype = None + elif "mpegurl" in ctype: + ctype = "application/vnd.apple.mpegurl" return StreamData(data.upstream(), ctype, data.thumbnail(), data.title(), data.description(), data.override) async def run(self): data = None