mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
use b64
This commit is contained in:
parent
8d02c8f22a
commit
52fed1ad1a
@ -6,6 +6,6 @@ RUN python3 -m pip install poetry && poetry config virtualenvs.create false
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY ./pyproject.toml ./poetry.lock* /app/
|
COPY ./pyproject.toml ./poetry.lock* /app/
|
||||||
RUN poetry install --no-root --no-dev
|
RUN poetry install --no-root --no-dev
|
||||||
RUN PYPPETEER_DOWNLOAD_HOST='http://npm.taobao.org/mirrors' pyppeteer-install
|
# RUN PYPPETEER_DOWNLOAD_HOST='http://npm.taobao.org/mirrors' pyppeteer-install
|
||||||
COPY . /app/
|
COPY . /app/
|
||||||
CMD ["python", "bot.py"]
|
CMD ["python", "bot.py"]
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import nonebot
|
import nonebot
|
||||||
|
import base64
|
||||||
from pyppeteer import launch
|
from pyppeteer import launch
|
||||||
from html import escape
|
from html import escape
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
@ -26,7 +27,7 @@ class Render(metaclass=Singleton):
|
|||||||
browser = await launch(args=['--no-sandbox'])
|
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) -> bytes:
|
||||||
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))
|
||||||
@ -35,13 +36,11 @@ class Render(metaclass=Singleton):
|
|||||||
f.write(html_text)
|
f.write(html_text)
|
||||||
await self.page.goto('file:///tmp/text-{}.html'.format(hash_text))
|
await self.page.goto('file:///tmp/text-{}.html'.format(hash_text))
|
||||||
div = await self.page.querySelector('div')
|
div = await self.page.querySelector('div')
|
||||||
path = '/tmp/img-{}.png'.format(hash_text)
|
return await div.screenshot()
|
||||||
await div.screenshot(path=path)
|
|
||||||
return path
|
|
||||||
|
|
||||||
async def text_to_pic_cqcode(self, text:str) -> str:
|
async def text_to_pic_cqcode(self, text:str) -> str:
|
||||||
path = await self.text_to_pic(text)
|
data = await self.text_to_pic(text)
|
||||||
return '[CQ:image,file=file://{}]'.format(path)
|
return '[CQ:image,file=base64://{}]'.format(base64.b64encode(data).decode())
|
||||||
|
|
||||||
async def _start():
|
async def _start():
|
||||||
r = Render()
|
r = Render()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user