Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/utils/image_table.py
2021-11-12 22:25:53 +08:00

68 lines
2 KiB
Python

import os
import re
import traceback
import uuid
from typing import List, Union
import aiohttp
import ujson as json
from tabulate import tabulate
from config import Config
web_render = Config('web_render')
class ImageTable:
def __init__(self, data, headers):
self.data = data
self.headers = headers
async def image_table_render(table: Union[ImageTable, List[ImageTable]]):
if not web_render:
return False
try:
tblst = []
if isinstance(table, ImageTable):
table = [table]
max_width = 500
for tbl in table:
d = []
for row in tbl.data:
cs = []
for c in row:
cs.append(re.sub(r'\n', '<br>', c))
d.append(cs)
w = len(tbl.headers) * 500
if w > max_width:
max_width = w
tblst.append(re.sub(r'<table>|</table>', '', tabulate(d, tbl.headers, tablefmt='unsafehtml')))
tblst = '<table>' + '\n'.join(tblst) + '</table>'
css = """
<style>table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid rgba(0,0,0,0.05);
font-size: 0.8125rem;
font-weight: 500;
}
th, td {
padding: 15px;
text-align: left;
}</style>"""
html = {'content': tblst + css, 'width': w}
picname = os.path.abspath(f'./cache/{str(uuid.uuid4())}.jpg')
if os.path.exists(picname):
os.remove(picname)
async with aiohttp.ClientSession() as session:
async with session.post(web_render, headers={
'Content-Type': 'application/json',
}, data=json.dumps(html)) as resp:
with open(picname, 'wb+') as jpg:
jpg.write(await resp.read())
return picname
except Exception:
traceback.print_exc()
return False