Merge branch 'main' into arknights

This commit is contained in:
felinae98
2021-02-26 21:44:43 +08:00
19 changed files with 67 additions and 23 deletions
@@ -5,8 +5,10 @@ import nonebot
from nonebot import logger
import base64
from pyppeteer import launch
from pyppeteer.chromium_downloader import check_chromium, download_chromium
from html import escape
from hashlib import sha256
from tempfile import NamedTemporaryFile
from .plugin_config import plugin_config
@@ -19,6 +21,10 @@ class Singleton(type):
supported_target_type = ('weibo', 'bilibili', 'rss', 'arknights')
if not plugin_config.hk_reporter_use_local and not check_chromium():
os.environ['PYPPETEER_DOWNLOAD_HOST'] = 'http://npm.taobao.org/mirrors'
download_chromium()
class Render(metaclass=Singleton):
def __init__(self):
@@ -44,14 +50,14 @@ class Render(metaclass=Singleton):
return str(data)
async def text_to_pic(self, text: str) -> str:
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)
data = await self.render('file:///tmp/text-{}.html'.format(hash_text), target='div')
os.remove('/tmp/text-{}.html'.format(hash_text))
with NamedTemporaryFile('wt', suffix='.html', delete=False) as tmp:
tmp_path = tmp.name
tmp.write(html_text)
data = await self.render('file://{}'.format(tmp_path), target='div')
os.remove(tmp_path)
return data
async def text_to_pic_cqcode(self, text:str) -> str:
@@ -61,10 +67,10 @@ class Render(metaclass=Singleton):
# logger.debug(code)
return code
async def parse_text(text: str):
async def parse_text(text: str) -> str:
'return raw text if don\'t use pic, otherwise return rendered opcode'
if plugin_config.hk_reporter_use_pic:
r = Render()
return await r.text_to_pic_cqcode(text)
render = Render()
return await render.text_to_pic_cqcode(text)
else:
return text