-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
60 lines (51 loc) · 1.59 KB
/
utils.py
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 time
from io import BytesIO
from pathlib import Path
from typing import Any, Optional
import httpx
import ujson
import re
from PIL import Image
def load_json(path: Path, encoding: str = 'utf-8'):
"""
读取本地json文件,返回文件数据。
:param path: 文件路径
:param encoding: 编码,默认为utf-8
:return: 数据
"""
return ujson.load(path.open('r', encoding=encoding)) if path.exists() else {}
def save_json(data: Any, path: Path, encoding: str = 'utf-8'):
"""
保存数据到json文件中
:param data: 要保存的数据
:param path: 保存路径
:param encoding: 编码,默认为utf-8
"""
path.parent.mkdir(parents=True, exist_ok=True)
ujson.dump(data, path.open('w', encoding=encoding), ensure_ascii=False, indent=2)
def get_describe_name(text: str) -> Optional[str]:
"""
获取文本中<color=#FFD780FF>和</color>之间的文本
:param text: 描述文本
:return: 名称
"""
if text := re.search(r'<color=#FFD780FF>(.+?)</color>', text):
return text[1]
return None
def download_img(url: str, path: Path):
"""
从安柏计划下载UI资源。
:param url: 下载链接
:param path: 保存的路径
:return: Image对象
"""
if path.exists():
return Image.open(path)
print(f'下载{url}')
resp = httpx.get(url,timeout=10)
content = resp.content
img = Image.open(BytesIO(content))
with path.open('wb') as f:
f.write(content)
time.sleep(1) # 安柏网有访问频率限制
return img