Skip to content

Commit

Permalink
fix webrender (#1512)
Browse files Browse the repository at this point in the history
* Update bugtracker.py

* Update help.py

* Update screenshot_image.py

* Update help.py

* Update image.py

* Update image_table.py
  • Loading branch information
DoroWolf authored Dec 29, 2024
1 parent d1249c5 commit 5b3717e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 51 deletions.
23 changes: 14 additions & 9 deletions core/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
31 changes: 20 additions & 11 deletions core/utils/image_table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import re
import traceback
from html import escape
from io import BytesIO
from typing import Any, List, Union
Expand Down Expand Up @@ -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 = []
Expand Down
8 changes: 3 additions & 5 deletions modules/bugtracker/bugtracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
38 changes: 21 additions & 17 deletions modules/core/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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 = []
Expand Down Expand Up @@ -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,
Expand All @@ -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())
Expand Down
14 changes: 5 additions & 9 deletions modules/wiki/utils/screenshot_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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...")
Expand All @@ -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,
Expand All @@ -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())
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 5b3717e

Please sign in to comment.