|
| 1 | +import zipfile |
| 2 | +import pyzipper |
| 3 | +from pathlib import Path |
| 4 | +from base64 import b64encode |
| 5 | + |
| 6 | +from creart import create |
| 7 | +from graia.ariadne.message.element import Image |
| 8 | + |
| 9 | +from .pica import pica |
| 10 | +from shared.utils.text2img import md2img |
| 11 | +from shared.models.config import GlobalConfig |
| 12 | + |
| 13 | + |
| 14 | +config = create(GlobalConfig) |
| 15 | +DOWNLOAD_CACHE = config.functions["pica"]["download_cache"] |
| 16 | +SEARCH_CACHE = config.functions["pica"]["search_cache"] |
| 17 | + |
| 18 | +BASE_PATH = Path(__file__).parent |
| 19 | +SEARCH_CACHE_PATH = BASE_PATH / "cache" / "search" |
| 20 | + |
| 21 | + |
| 22 | +async def pica_t2i(comic_info, is_search: bool = False, rank: int | None = None): |
| 23 | + thumb = await (await get_thumb(comic_info)).get_bytes() |
| 24 | + md = f"<img src='data:image/png;base64, {b64encode(thumb).decode()}'/><br>" |
| 25 | + if rank: |
| 26 | + md += f"排名:{rank}<br>" |
| 27 | + md += f"名称:{comic_info['title']}<br>" |
| 28 | + md += f"作者:{comic_info['author']}<br>" |
| 29 | + if is_search: |
| 30 | + md += f"描述:{comic_info['description']}<br>" |
| 31 | + md += f"分类:{'、'.join(comic_info['categories'])}<br>" |
| 32 | + if is_search: |
| 33 | + md += f"标签:{'、'.join(comic_info['tags'])}<br>", |
| 34 | + md += f"页数:{comic_info['pagesCount']}<br>" |
| 35 | + md += f"章节数:{comic_info['epsCount']}<br>" |
| 36 | + md += f"完结状态:{'已完结' if comic_info['finished'] else '未完结'}<br>" |
| 37 | + md += f"喜欢: {comic_info['totalLikes']}<br>" |
| 38 | + md += f"浏览次数: {comic_info['totalViews']}" |
| 39 | + return Image(data_bytes=await md2img(md)) |
| 40 | + |
| 41 | + |
| 42 | +def zip_directory(path: Path, zip_name, pwd: str = "i_luv_sagiri") -> Path: |
| 43 | + zip_file = path.parent / f"{zip_name}.zip" |
| 44 | + encrypt_zip_file = path.parent / f"{zip_name}_密码{pwd}.zip" |
| 45 | + with zipfile.ZipFile(zip_file, mode="w") as zip_w: |
| 46 | + for entry in path.rglob("*"): |
| 47 | + zip_w.write(entry, entry.relative_to(path)) |
| 48 | + |
| 49 | + with pyzipper.AESZipFile( |
| 50 | + encrypt_zip_file, |
| 51 | + "w", |
| 52 | + compression=pyzipper.ZIP_LZMA, |
| 53 | + encryption=pyzipper.WZ_AES, |
| 54 | + ) as zf: |
| 55 | + zf.setpassword(pwd.encode()) |
| 56 | + zf.setencryption(pyzipper.WZ_AES, nbits=128) |
| 57 | + zf.write(zip_file, encrypt_zip_file.name) |
| 58 | + |
| 59 | + zip_file.unlink() |
| 60 | + return encrypt_zip_file |
| 61 | + |
| 62 | + |
| 63 | +async def get_thumb(comic_info: dict) -> Image: |
| 64 | + thumb = SEARCH_CACHE_PATH / f"{comic_info['_id']}.jpg" |
| 65 | + if thumb.exists(): |
| 66 | + return Image(path=thumb) |
| 67 | + else: |
| 68 | + return Image( |
| 69 | + data_bytes=await pica.download_image( |
| 70 | + url=f"{comic_info['thumb']['fileServer']}/static/{comic_info['thumb']['path']}", |
| 71 | + path=thumb if SEARCH_CACHE else None, |
| 72 | + ) |
| 73 | + ) |
0 commit comments