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