From 5b3717e5b0c52a198835c9e7140c4d9dfc3f6a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Sun, 29 Dec 2024 09:37:13 +0800 Subject: [PATCH] fix webrender (#1512) * Update bugtracker.py * Update help.py * Update screenshot_image.py * Update help.py * Update image.py * Update image_table.py --- core/utils/image.py | 23 ++++++++++------ core/utils/image_table.py | 31 +++++++++++++-------- modules/bugtracker/bugtracker.py | 8 ++---- modules/core/help.py | 38 ++++++++++++++------------ modules/wiki/utils/screenshot_image.py | 14 ++++------ 5 files changed, 63 insertions(+), 51 deletions(-) diff --git a/core/utils/image.py b/core/utils/image.py index fbafa46dfc..ee66fe726a 100644 --- a/core/utils/image.py +++ b/core/utils/image.py @@ -90,6 +90,7 @@ async def msgchain2image(message_chain: Union[List, MessageChain], d = {'content': html_content, 'element': '.botbox'} html_ = json.dumps(d) + Logger.info("[WebRender] Converting message chain...") try: pic = await download(webrender('element_screenshot', use_local=use_local), status_code=200, @@ -100,17 +101,21 @@ async def msgchain2image(message_chain: Union[List, MessageChain], timeout=30, request_private_ip=True ) - except aiohttp.ClientConnectorError: + except Exception: if use_local: - pic = await download(webrender('element_screenshot', use_local=False), - status_code=200, - method='POST', - headers={'Content-Type': 'application/json'}, - post_data=html_, - request_private_ip=True - ) + try: + pic = await download(webrender('element_screenshot', use_local=False), + status_code=200, + method='POST', + headers={'Content-Type': 'application/json'}, + post_data=html_, + request_private_ip=True + ) + except Exception: + Logger.error('[WebRender] Generation Failed.') + return False else: - Logger.info('[WebRender] Generation Failed.') + Logger.error('[WebRender] Generation Failed.') return False with open(pic) as read: diff --git a/core/utils/image_table.py b/core/utils/image_table.py index 4c60f945a1..d84036e0c6 100644 --- a/core/utils/image_table.py +++ b/core/utils/image_table.py @@ -1,5 +1,6 @@ import base64 import re +import traceback from html import escape from io import BytesIO from typing import Any, List, Union @@ -100,19 +101,27 @@ async def image_table_render( "Content-Type": "application/json", }, ) - except aiohttp.ClientConnectorError: + except Exception: if use_local: - pic = await download( - webrender(use_local=False), - method="POST", - post_data=json.dumps(html), - request_private_ip=True, - headers={ - "Content-Type": "application/json", - }, - ) + try: + pic = await download( + webrender(use_local=False), + method="POST", + post_data=json.dumps(html), + request_private_ip=True, + headers={ + "Content-Type": "application/json", + }, + ) + except Exception: + Logger.error("Generation failed.") + return False + else: + Logger.error("Generation failed.") + return False except Exception: - Logger.exception("Error at image_table_render.") + Logger.error(traceback.format_exc()) + return False with open(pic) as read: load_img = json.loads(read.read()) img_lst = [] diff --git a/modules/bugtracker/bugtracker.py b/modules/bugtracker/bugtracker.py index a31c917e24..cc6b81164a 100644 --- a/modules/bugtracker/bugtracker.py +++ b/modules/bugtracker/bugtracker.py @@ -43,14 +43,12 @@ async def make_screenshot(page_link, use_local=True): bimg = PILImage.open(bio) img_lst.append(bimg) return img_lst - Logger.info("[WebRender] Generation Failed.") + Logger.error("[WebRender] Generation Failed.") return False - except aiohttp.ClientConnectorError: + except Exception: if use_local: return await make_screenshot(page_link, use_local=False) - return False - except ValueError: - Logger.info("[WebRender] Generation Failed.") + Logger.error("[WebRender] Generation Failed.") return False diff --git a/modules/core/help.py b/modules/core/help.py index 43fb08baa4..af3cb99e89 100644 --- a/modules/core/help.py +++ b/modules/core/help.py @@ -137,7 +137,7 @@ async def _(msg: Bot.MessageSession, module: str): d = {'content': html_content, 'element': '.botbox'} html_ = json.dumps(d) - + Logger.info("[WebRender] Generating help document...") try: pic = await download(webrender('element_screenshot', use_local=use_local), status_code=200, @@ -148,7 +148,7 @@ async def _(msg: Bot.MessageSession, module: str): timeout=30, request_private_ip=True ) - except aiohttp.ClientConnectorError: + except Exception as e: if use_local: try: pic = await download(webrender('element_screenshot', use_local=False), @@ -158,12 +158,12 @@ async def _(msg: Bot.MessageSession, module: str): post_data=html_, request_private_ip=True ) - except aiohttp.ClientConnectorError: - Logger.info('[WebRender] Generation Failed.') - raise + except Exception as e: + Logger.error('[WebRender] Generation Failed.') + raise e else: - Logger.info('[WebRender] Generation Failed.') - raise + Logger.error('[WebRender] Generation Failed.') + raise e with open(pic) as read: load_img = json.loads(read.read()) img_lst = [] @@ -363,7 +363,7 @@ async def help_generator(msg: Bot.MessageSession, d = {'content': html_content, 'element': '.botbox'} html_ = json.dumps(d) - + Logger.info("[WebRender] Generating module list...") try: pic = await download(webrender('element_screenshot', use_local=use_local), status_code=200, @@ -374,17 +374,21 @@ async def help_generator(msg: Bot.MessageSession, timeout=30, request_private_ip=True ) - except aiohttp.ClientConnectorError: + except Exception: if use_local: - pic = await download(webrender('element_screenshot', use_local=False), - status_code=200, - method='POST', - headers={'Content-Type': 'application/json'}, - post_data=html_, - request_private_ip=True - ) + try: + pic = await download(webrender('element_screenshot', use_local=False), + status_code=200, + method='POST', + headers={'Content-Type': 'application/json'}, + post_data=html_, + request_private_ip=True + ) + except Exception: + Logger.error('[WebRender] Generation Failed.') + return False else: - Logger.info('[WebRender] Generation Failed.') + Logger.error('[WebRender] Generation Failed.') return False with open(pic) as read: load_img = json.loads(read.read()) diff --git a/modules/wiki/utils/screenshot_image.py b/modules/wiki/utils/screenshot_image.py index 2f321275b7..aa8a98701a 100644 --- a/modules/wiki/utils/screenshot_image.py +++ b/modules/wiki/utils/screenshot_image.py @@ -52,7 +52,7 @@ async def generate_screenshot_v2( timeout=30, request_private_ip=True, ) - except aiohttp.ClientConnectorError: + except Exception: if use_local: return await generate_screenshot_v2( page_link, @@ -61,9 +61,7 @@ async def generate_screenshot_v2( content_mode, use_local=False, ) - return False - except ValueError: - Logger.info("[WebRender] Generation Failed.") + Logger.error("[WebRender] Generation Failed.") return False else: Logger.info("[WebRender] Generating section screenshot...") @@ -79,7 +77,7 @@ async def generate_screenshot_v2( timeout=30, request_private_ip=True, ) - except aiohttp.ClientConnectorError: + except Exception: if use_local: return await generate_screenshot_v2( page_link, @@ -88,9 +86,7 @@ async def generate_screenshot_v2( content_mode, use_local=False, ) - return False - except ValueError: - Logger.info("[WebRender] Generation Failed.") + Logger.error("[WebRender] Generation Failed.") return False with open(img) as read: load_img = json.loads(read.read()) @@ -119,7 +115,7 @@ async def generate_screenshot_v1( page_link, timeout=aiohttp.ClientTimeout(total=20) ) as req: html = await req.read() - except BaseException: + except Exception: Logger.error(traceback.format_exc()) return False soup = BeautifulSoup(html, "html.parser")