follow redirect for content-type detection

This commit is contained in:
Roy Olav Purser 2021-06-04 08:54:56 +02:00
parent aad477ef90
commit 8832958112
Signed by: roypur
GPG Key ID: E14D26A036F21656
2 changed files with 12 additions and 21 deletions

View File

@ -223,18 +223,7 @@ class MainHandler(tornado.web.RequestHandler):
video_info = {} video_info = {}
video_info["upstream"] = proxied[0] video_info["upstream"] = proxied[0]
video_info["poster"] = proxied[1] video_info["poster"] = proxied[1]
if isinstance(provider_data.ctype(), str):
video_info["ctype"] = provider_data.ctype() 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)
script = template_script.generate(info=json.dumps(video_info)) script = template_script.generate(info=json.dumps(video_info))
b64 = str(base64.b64encode(script), "ascii") b64 = str(base64.b64encode(script), "ascii")

View File

@ -117,15 +117,17 @@ class StreamProvider():
proxies = {} proxies = {}
proxies["http"] = "socks5://" + self.proxy proxies["http"] = "socks5://" + self.proxy
proxies["https"] = "socks5://" + self.proxy proxies["https"] = "socks5://" + self.proxy
ctype = "binary/octet-stream" ctype = None
upstream = data.upstream()
try: 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: except Exception as e:
self.logger.info("%s <%s>", e, self.upstream) self.logger.info("%s <%s>", e, self.upstream)
else: else:
if resp.ok:
upstream = resp.url
ctype = resp.headers.get("Content-Type", "text/plain").lower() ctype = resp.headers.get("Content-Type", "text/plain").lower()
if ctype.startswith("text"): if ctype.startswith("text"):
self.logger.info("Bad Content-Type %s", data.upstream())
ctype = None ctype = None
elif "mpegurl" in ctype: elif "mpegurl" in ctype:
ctype = "application/vnd.apple.mpegurl" ctype = "application/vnd.apple.mpegurl"