diff --git a/stream.py b/stream.py index 6ef818d..dc54722 100755 --- a/stream.py +++ b/stream.py @@ -43,9 +43,14 @@ class ProxyElem(): def __repr__(self): return str(self.proxy) async def content_type(self, url): - async with self.session() as session: - resp = await session.head(url) - return resp.headers.get("Content-Type", "binary/octet-stream") + ctype = "binary/octet-stream" + try: + async with self.session() as session: + resp = await session.head(url) + ctype = resp.headers.get("Content-Type", "binary/octet-stream") + except Exception as e: + logger.info(e) + return ctype async def proxy_url(self, current, path): data = {} data_list = [data] @@ -61,11 +66,16 @@ class ProxyElem(): data["proxy"] = self.proxy if proxy_server is None: return data["upstream"] - async with self.session() as session: - resp = await session.post(proxy_server, json=data_list) - text = await resp.text() - jdata = json.loads(text) - logger.info(jdata) + + jdata = None + try: + async with self.session() as session: + resp = await session.post(proxy_server, json=data_list) + text = await resp.text() + jdata = json.loads(text) + except Exception as e: + logger.info(e) + if isinstance(jdata, list) and len(jdata) == 1: return jdata[0] else: @@ -149,7 +159,10 @@ class UpstreamHandler(): proxies[self.provider] = current_list break for delay in delays: - await delay.session.close() + try: + await delay.session.close() + except Exception as e: + logger.info(e) async def meta(self): data = [] try: @@ -221,10 +234,14 @@ except Exception as e: logger.info(e) async def rewrite(current, provider, proxy): - async with proxy.session() as session: - resp = await session.get(current) - text = await resp.text() ndata = None + text = None + try: + async with proxy.session() as session: + resp = await session.get(current) + text = await resp.text() + except Exception as e: + logger.info(e) if text is not None: links = [] for line in resp.text.splitlines(): @@ -246,22 +263,26 @@ async def rewrite(current, provider, proxy): links.append(ldata) if isinstance(proxy_server, str): ndata = "" - async with proxy.session() as session: - resp = await session.post(proxy_server, json=links) - link_text = await resp.text() - if isinstance(link_text, str): - links = json.loads(link_text) - for line in text.splitlines(): - if line.startswith("#EXT-X-KEY:METHOD="): - matches = re.findall(r'(?<=URI=").+(?=")', line) - if len(matches) == 1: - new_url = links.pop(0) - ndata += re.sub(r'URI=".+"', f'URI="{new_url}"', line) - elif line.startswith("#"): - ndata += line - else: - ndata += links.pop(0) - ndata += "\n" + try: + async with proxy.session() as session: + resp = await session.post(proxy_server, json=links) + link_text = await resp.text() + except Exception as e: + logger.info(e) + else: + if isinstance(link_text, str): + links = json.loads(link_text) + for line in text.splitlines(): + if line.startswith("#EXT-X-KEY:METHOD="): + matches = re.findall(r'(?<=URI=").+(?=")', line) + if len(matches) == 1: + new_url = links.pop(0) + ndata += re.sub(r'URI=".+"', f'URI="{new_url}"', line) + elif line.startswith("#"): + ndata += line + else: + ndata += links.pop(0) + ndata += "\n" return ndata class MainHandler(tornado.web.RequestHandler): async def handle_any(self, redir):