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,19 +223,8 @@ 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]
video_info["ctype"] = provider_data.ctype()
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)
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")
script_file = f'data:text/javascript;charset=utf-8;base64,{b64}' script_file = f'data:text/javascript;charset=utf-8;base64,{b64}'

View File

@ -117,18 +117,20 @@ 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:
ctype = resp.headers.get("Content-Type", "text/plain").lower() if resp.ok:
if ctype.startswith("text"): upstream = resp.url
self.logger.info("Bad Content-Type %s", data.upstream()) ctype = resp.headers.get("Content-Type", "text/plain").lower()
ctype = None if ctype.startswith("text"):
elif "mpegurl" in ctype: ctype = None
ctype = "application/vnd.apple.mpegurl" elif "mpegurl" in ctype:
ctype = "application/vnd.apple.mpegurl"
return StreamData(data.upstream(), ctype, data.thumbnail(), data.title(), data.description(), data.override) return StreamData(data.upstream(), ctype, data.thumbnail(), data.title(), data.description(), data.override)
async def run(self): async def run(self):
data = None data = None