blob: d4df4fe25c6cf81dfe2f5c70715e4b7a7a6fa1c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import asyncio
from ATRI.service import Service
from ATRI.rule import is_in_service
from ATRI.utils import request
from ATRI.log import logger as log
from ATRI.exceptions import RequestError, WriteFileError
from .image_dealer import image_dealer
TENCENT_AVATER_URL = "https://q1.qlogo.cn/g?b=qq&nk={user_id}&s=640"
SOURCE_URL = "https://cdn.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 RequestError:
raise RequestError("Request failed!")
data = res.read()
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"]
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)
try:
with open(path, "wb") as w:
w.write(data.read())
log.info("所需资源装载完成")
except WriteFileError:
raise WriteFileError("装载资源失败")
loop = asyncio.get_event_loop()
loop.create_task(init_source())
|