optimize chrome use, add pic support

This commit is contained in:
felinae98 2021-02-08 14:26:08 +08:00
parent 3317c8f062
commit 6f9c3887d0
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
2 changed files with 26 additions and 13 deletions

View File

@ -1,3 +1,6 @@
from . import plugin_config
from .utils import parse_text
class Post: class Post:
target_type: str target_type: str
@ -6,8 +9,12 @@ class Post:
pics: list[str] pics: list[str]
def generate_messages(self): def generate_messages(self):
first_msg = '来源: {}\n{}\n详情:{}'.format(self.target_type, self.text, self.url) if plugin_config.hk_reporter_use_pic:
res = [first_msg] 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: for pic in self.pics:
res.append("[CQ:image,file={url}]".format(url=pic)) res.append("[CQ:image,file={url}]".format(url=pic))
return res return res

View File

@ -1,3 +1,4 @@
import os
import nonebot import nonebot
from nonebot import logger from nonebot import logger
import base64 import base64
@ -19,36 +20,41 @@ supported_target_type = ('weibo', 'bilibili', 'rss')
class Render(metaclass=Singleton): class Render(metaclass=Singleton):
def __init__(self): def __init__(self):
self.page = None self.browser = None
async def init(self): async def init(self):
if plugin_config.hk_reporter_use_local: 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: else:
browser = await launch(args=['--no-sandbox']) self.browser = await launch(args=['--no-sandbox'])
self.page = await browser.newPage() # self.page = await browser.newPage()
async def text_to_pic(self, text: str) -> str: async def text_to_pic(self, text: str) -> str:
page = await self.browser.newPage()
hash_text = sha256(text.encode()).hexdigest()[:20] hash_text = sha256(text.encode()).hexdigest()[:20]
lines = text.split('\n') lines = text.split('\n')
parsed_lines = list(map(lambda x: '<p>{}</p>'.format(escape(x)), lines)) 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)) html_text = '<div style="width:17em;padding:1em">{}</div>'.format(''.join(parsed_lines))
with open('/tmp/text-{}.html'.format(hash_text), 'w') as f: with open('/tmp/text-{}.html'.format(hash_text), 'w') as f:
f.write(html_text) f.write(html_text)
await self.page.goto('file:///tmp/text-{}.html'.format(hash_text)) await page.goto('file:///tmp/text-{}.html'.format(hash_text))
div = await self.page.querySelector('div') div = await page.querySelector('div')
return await div.screenshot(type='jpeg', encoding='base64') 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: async def text_to_pic_cqcode(self, text:str) -> str:
data = await self.text_to_pic(text) 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) code = '[CQ:image,file=base64://{}]'.format(data)
logger.debug(code) # logger.debug(code)
return code return code
async def _start(): async def _start():
r = Render() if plugin_config.hk_reporter_use_pic:
await r.init() r = Render()
await r.init()
nonebot.get_driver().on_startup(_start) nonebot.get_driver().on_startup(_start)
async def parse_text(text: str): async def parse_text(text: str):