mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
close chromium after screen shot
This commit is contained in:
parent
27dd2482f8
commit
829d741158
@ -1,4 +1,5 @@
|
||||
import os
|
||||
import asyncio
|
||||
import nonebot
|
||||
from nonebot import logger
|
||||
import base64
|
||||
@ -20,17 +21,15 @@ supported_target_type = ('weibo', 'bilibili', 'rss')
|
||||
class Render(metaclass=Singleton):
|
||||
|
||||
def __init__(self):
|
||||
self.browser = None
|
||||
|
||||
async def init(self):
|
||||
if plugin_config.hk_reporter_use_local:
|
||||
self.browser = await launch(executablePath='/usr/bin/chromium', args=['--no-sandbox'])
|
||||
else:
|
||||
self.browser = await launch(args=['--no-sandbox'])
|
||||
# self.page = await browser.newPage()
|
||||
self.lock = asyncio.Lock()
|
||||
|
||||
async def text_to_pic(self, text: str) -> str:
|
||||
page = await self.browser.newPage()
|
||||
async with self.lock:
|
||||
if plugin_config.hk_reporter_use_local:
|
||||
browser = await launch(executablePath='/usr/bin/chromium', args=['--no-sandbox'])
|
||||
else:
|
||||
browser = await launch(args=['--no-sandbox'])
|
||||
page = await browser.newPage()
|
||||
hash_text = sha256(text.encode()).hexdigest()[:20]
|
||||
lines = text.split('\n')
|
||||
parsed_lines = list(map(lambda x: '<p>{}</p>'.format(escape(x)), lines))
|
||||
@ -41,6 +40,7 @@ class Render(metaclass=Singleton):
|
||||
div = await page.querySelector('div')
|
||||
data = await div.screenshot(type='jpeg', encoding='base64')
|
||||
await page.close()
|
||||
await browser.close()
|
||||
os.remove('/tmp/text-{}.html'.format(hash_text))
|
||||
return data
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user