2021-11-08 16:09:06 +00:00
|
|
|
import os
|
2021-11-12 14:25:53 +00:00
|
|
|
import re
|
2021-11-08 16:09:06 +00:00
|
|
|
import traceback
|
2022-01-07 15:50:43 +00:00
|
|
|
from html import escape
|
2021-11-08 16:09:06 +00:00
|
|
|
from typing import List, Union
|
|
|
|
|
|
|
|
import aiohttp
|
2021-11-12 14:25:53 +00:00
|
|
|
import ujson as json
|
2021-11-08 16:09:06 +00:00
|
|
|
from tabulate import tabulate
|
|
|
|
|
|
|
|
from config import Config
|
2022-01-20 13:31:50 +00:00
|
|
|
from core.logger import Logger
|
2023-02-07 17:55:45 +00:00
|
|
|
from .http import download_to_cache, get_url, post_url
|
2022-06-12 07:07:53 +00:00
|
|
|
from .cache import random_cache_path
|
2021-11-08 16:09:06 +00:00
|
|
|
|
2023-02-07 17:55:45 +00:00
|
|
|
|
|
|
|
web_render = Config('web_render')
|
|
|
|
web_render_local = Config('web_render_local')
|
2021-11-08 16:09:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ImageTable:
|
|
|
|
def __init__(self, data, headers):
|
|
|
|
self.data = data
|
|
|
|
self.headers = headers
|
|
|
|
|
|
|
|
|
2023-02-07 17:55:45 +00:00
|
|
|
async def image_table_render(table: Union[ImageTable, List[ImageTable]], save_source=True, use_local=True):
|
|
|
|
if not web_render_local:
|
|
|
|
if not web_render:
|
|
|
|
Logger.warn('[Webrender] Webrender is not configured.')
|
|
|
|
return False
|
|
|
|
use_local = False
|
2021-11-08 16:09:06 +00:00
|
|
|
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:
|
2022-01-07 16:27:57 +00:00
|
|
|
cs.append(re.sub(r'\n', '<br>', escape(c)))
|
2021-11-08 16:09:06 +00:00
|
|
|
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>"""
|
2023-01-19 14:31:02 +00:00
|
|
|
html = {'content': tblst + css, 'width': w, 'mw': False}
|
2022-04-23 14:22:56 +00:00
|
|
|
if save_source:
|
2022-06-12 07:07:53 +00:00
|
|
|
fname = random_cache_path() + '.html'
|
2022-04-23 14:22:56 +00:00
|
|
|
with open(fname, 'w') as fi:
|
|
|
|
fi.write(tblst + css)
|
2023-02-07 17:55:45 +00:00
|
|
|
|
|
|
|
pic = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
pic = await download_to_cache(web_render_local if use_local else web_render, headers={
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
}, post_data=json.dumps(html))
|
|
|
|
except aiohttp.ClientConnectorError:
|
|
|
|
pic = await download_to_cache(web_render, headers={
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
}, post_data=json.dumps(html))
|
|
|
|
return pic
|
2021-11-08 16:09:06 +00:00
|
|
|
except Exception:
|
2022-01-20 13:31:50 +00:00
|
|
|
Logger.error(traceback.format_exc())
|
2021-11-08 16:09:06 +00:00
|
|
|
return False
|
2022-06-12 07:07:53 +00:00
|
|
|
|
|
|
|
|
2023-02-07 17:55:45 +00:00
|
|
|
__all__ = ['ImageTable', 'image_table_render']
|