mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-02 09:26:12 +08:00
optimize chrome use, add pic support
This commit is contained in:
parent
3317c8f062
commit
6f9c3887d0
@ -1,3 +1,6 @@
|
||||
from . import plugin_config
|
||||
from .utils import parse_text
|
||||
|
||||
class Post:
|
||||
|
||||
target_type: str
|
||||
@ -6,8 +9,12 @@ class Post:
|
||||
pics: list[str]
|
||||
|
||||
def generate_messages(self):
|
||||
first_msg = '来源: {}\n{}\n详情:{}'.format(self.target_type, self.text, self.url)
|
||||
res = [first_msg]
|
||||
if plugin_config.hk_reporter_use_pic:
|
||||
text_msg = '来源: {}\n{}'.format(self.target_type, self.text)
|
||||
res = [await parse_text(text_msg), self.url]
|
||||
else:
|
||||
first_msg = '来源: {}\n{}\n详情:{}'.format(self.target_type, self.text, self.url)
|
||||
res = [first_msg]
|
||||
for pic in self.pics:
|
||||
res.append("[CQ:image,file={url}]".format(url=pic))
|
||||
return res
|
||||
|
@ -1,3 +1,4 @@
|
||||
import os
|
||||
import nonebot
|
||||
from nonebot import logger
|
||||
import base64
|
||||
@ -19,36 +20,41 @@ supported_target_type = ('weibo', 'bilibili', 'rss')
|
||||
class Render(metaclass=Singleton):
|
||||
|
||||
def __init__(self):
|
||||
self.page = None
|
||||
self.browser = None
|
||||
|
||||
async def init(self):
|
||||
if plugin_config.hk_reporter_use_local:
|
||||
browser = await launch(executablePath='/usr/bin/chromium', args=['--no-sandbox'])
|
||||
self.browser = await launch(executablePath='/usr/bin/chromium', args=['--no-sandbox'])
|
||||
else:
|
||||
browser = await launch(args=['--no-sandbox'])
|
||||
self.page = await browser.newPage()
|
||||
self.browser = await launch(args=['--no-sandbox'])
|
||||
# self.page = await browser.newPage()
|
||||
|
||||
async def text_to_pic(self, text: str) -> str:
|
||||
page = await self.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))
|
||||
html_text = '<div style="width:17em;padding:1em">{}</div>'.format(''.join(parsed_lines))
|
||||
with open('/tmp/text-{}.html'.format(hash_text), 'w') as f:
|
||||
f.write(html_text)
|
||||
await self.page.goto('file:///tmp/text-{}.html'.format(hash_text))
|
||||
div = await self.page.querySelector('div')
|
||||
return await div.screenshot(type='jpeg', encoding='base64')
|
||||
await page.goto('file:///tmp/text-{}.html'.format(hash_text))
|
||||
div = await page.querySelector('div')
|
||||
data = await div.screenshot(type='jpeg', encoding='base64')
|
||||
await page.close()
|
||||
os.remove('/tmp/text-{}.html'.format(hash_text))
|
||||
return data
|
||||
|
||||
async def text_to_pic_cqcode(self, text:str) -> str:
|
||||
data = await self.text_to_pic(text)
|
||||
logger.debug('file size: {}'.format(len(data)))
|
||||
# logger.debug('file size: {}'.format(len(data)))
|
||||
code = '[CQ:image,file=base64://{}]'.format(data)
|
||||
logger.debug(code)
|
||||
# logger.debug(code)
|
||||
return code
|
||||
|
||||
async def _start():
|
||||
r = Render()
|
||||
await r.init()
|
||||
if plugin_config.hk_reporter_use_pic:
|
||||
r = Render()
|
||||
await r.init()
|
||||
|
||||
nonebot.get_driver().on_startup(_start)
|
||||
async def parse_text(text: str):
|
||||
|
Loading…
x
Reference in New Issue
Block a user