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):
ctype = "binary/octet-stream"
try:
async with self.session() as session: async with self.session() as session:
resp = await session.head(url) resp = await session.head(url)
return resp.headers.get("Content-Type", "binary/octet-stream") 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"]
jdata = None
try:
async with self.session() as session: async with self.session() as session:
resp = await session.post(proxy_server, json=data_list) resp = await session.post(proxy_server, json=data_list)
text = await resp.text() text = await resp.text()
jdata = json.loads(text) jdata = json.loads(text)
logger.info(jdata) 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:
try:
await delay.session.close() 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):
ndata = None
text = None
try:
async with proxy.session() as session: async with proxy.session() as session:
resp = await session.get(current) resp = await session.get(current)
text = await resp.text() text = await resp.text()
ndata = None 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,9 +263,13 @@ async def rewrite(current, provider, proxy):
links.append(ldata) links.append(ldata)
if isinstance(proxy_server, str): if isinstance(proxy_server, str):
ndata = "" ndata = ""
try:
async with proxy.session() as session: async with proxy.session() as session:
resp = await session.post(proxy_server, json=links) resp = await session.post(proxy_server, json=links)
link_text = await resp.text() link_text = await resp.text()
except Exception as e:
logger.info(e)
else:
if isinstance(link_text, str): if isinstance(link_text, str):
links = json.loads(link_text) links = json.loads(link_text)
for line in text.splitlines(): for line in text.splitlines():