from ATRI import driver from ATRI.service import Service, ServiceTools from ATRI.rule import is_in_service from ATRI.utils import request from ATRI.log import logger as log from ATRI.exceptions import RequestError from .image_dealer import image_dealer TENCENT_AVATER_URL = "https://q1.qlogo.cn/g?b=qq&nk={user_id}&s=640" SOURCE_URL = "https://fastly.jsdelivr.net/gh/Kyomotoi/CDN@master/project/ATRI/" class Polaroid(Service): def __init__(self): Service.__init__(self, "拍立得", "根据头像生成拍立得风格照片!", rule=is_in_service("拍立得")) @classmethod async def _request(cls, user_id: str) -> bytes: try: res = await request.get(TENCENT_AVATER_URL.format(user_id=user_id)) except Exception: raise RequestError("Request failed!") data = res.read() # type: ignore return data @classmethod async def generate(cls, user_id: str): await init_source() user_avater = await cls._request(user_id) result = image_dealer(user_avater, user_id) return f"file:///{result}" from .image_dealer import TEMP_PATH, POLAROID_DIR async def init_source(): files = ["frame-0.PNG", "frame-1.PNG", "font-0.ttf"] try: for i in files: path = POLAROID_DIR / i if not path.is_file(): log.warning("插件 polaroid 缺少所需资源,装载中") url = SOURCE_URL + i data = await request.get(url) with open(path, "wb") as w: w.write(data.read()) # type: ignore except Exception: ServiceTools.service_controller("拍立得", False) log.error(f"插件 polaroid 装载资源失败. 已自动禁用") log.success("插件 polaroid 装载资源完成") driver().on_startup(init_source)