add error handler aiohttp

This commit is contained in:
Roy Olav Purser 2021-05-14 16:47:09 +02:00
parent c8d51d2e3d
commit 1a3a99ec72
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -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):