🎨 Optimize Import

Use the default json when ujson is not available
This commit is contained in:
洛水居室 2023-11-16 13:58:32 +08:00
parent 58e6144622
commit ac4610a57c
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
6 changed files with 43 additions and 28 deletions

View File

@ -1,9 +1,13 @@
import ujson as json
from core.base_service import BaseService from core.base_service import BaseService
from core.dependence.redisdb import RedisDB from core.dependence.redisdb import RedisDB
from modules.wiki.base import Model from modules.wiki.base import Model
try:
import ujson as jsonlib
except ImportError:
import json as jsonlib
__all__ = ["WikiCache"] __all__ = ["WikiCache"]
@ -17,7 +21,7 @@ class WikiCache(BaseService.Component):
if isinstance(value, Model): if isinstance(value, Model):
value = value.json() value = value.json()
elif isinstance(value, (dict, list)): elif isinstance(value, (dict, list)):
value = json.dumps(value) value = jsonlib.dumps(value)
await self.client.set(qname, value) await self.client.set(qname, value)
async def delete(self, key: str): async def delete(self, key: str):
@ -28,10 +32,10 @@ class WikiCache(BaseService.Component):
qname = f"{self.qname}:{key}" qname = f"{self.qname}:{key}"
# noinspection PyBroadException # noinspection PyBroadException
try: try:
result = json.loads(await self.client.get(qname)) result = jsonlib.loads(await self.client.get(qname))
except Exception: # pylint: disable=W0703 except Exception: # pylint: disable=W0703
result = [] result = []
if isinstance(result, list) and len(result) > 0: if isinstance(result, list) and len(result) > 0:
for num, item in enumerate(result): for num, item in enumerate(result):
result[num] = json.loads(item) result[num] = jsonlib.loads(item)
return result return result

@ -1 +1 @@
Subproject commit 30cc0cf8f700f50ad3063b2f3bca4f325f121bd3 Subproject commit 9dd1acab7842fb2c7cfd5ca507de93778318dc82

View File

@ -5,12 +5,15 @@ from __future__ import annotations
import functools import functools
from typing import Any, Generic, ItemsView, Iterator, KeysView, Optional, TypeVar, ValuesView from typing import Any, Generic, ItemsView, Iterator, KeysView, Optional, TypeVar, ValuesView
import ujson as json
from utils.const import PROJECT_ROOT from utils.const import PROJECT_ROOT
from utils.log import logger from utils.log import logger
from utils.typedefs import StrOrInt from utils.typedefs import StrOrInt
try:
import ujson as jsonlib
except ImportError:
import json as jsonlib
__all__ = ( __all__ = (
"HONEY_DATA", "HONEY_DATA",
"AVATAR_DATA", "AVATAR_DATA",
@ -52,7 +55,7 @@ class Data(dict, Generic[K, V]):
) )
self._dict = {} self._dict = {}
with open(path, encoding="utf-8") as file: with open(path, encoding="utf-8") as file:
self._dict = json.load(file) self._dict = jsonlib.load(file)
_cache.update({self._file_name: self._dict}) _cache.update({self._file_name: self._dict})
return self._dict return self._dict

View File

@ -4,7 +4,6 @@ import asyncio
import re import re
from typing import Dict, List, Optional from typing import Dict, List, Optional
import ujson as json
from aiofiles import open as async_open from aiofiles import open as async_open
from httpx import AsyncClient, HTTPError, Response from httpx import AsyncClient, HTTPError, Response
@ -13,6 +12,11 @@ from utils.const import PROJECT_ROOT
from utils.log import logger from utils.log import logger
from utils.typedefs import StrOrInt from utils.typedefs import StrOrInt
try:
import ujson as jsonlib
except ImportError:
import json as jsonlib
__all__ = [ __all__ = [
"get_avatar_data", "get_avatar_data",
"get_artifact_data", "get_artifact_data",
@ -46,7 +50,7 @@ async def get_avatar_data() -> DATA_TYPE:
url = "https://genshin.honeyhunterworld.com/fam_chars/?lang=CHS" url = "https://genshin.honeyhunterworld.com/fam_chars/?lang=CHS"
response = await request(url) response = await request(url)
chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0] chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0]
json_data = json.loads(chaos_data) # 转为 json json_data = jsonlib.loads(chaos_data) # 转为 json
for data in json_data: for data in json_data:
cid = int("10000" + re.findall(r"\d+", data[1])[0]) cid = int("10000" + re.findall(r"\d+", data[1])[0])
honey_id = re.findall(r"/(.*?)/", data[1])[0] honey_id = re.findall(r"/(.*?)/", data[1])[0]
@ -64,7 +68,7 @@ async def get_weapon_data() -> DATA_TYPE:
for url in urls: for url in urls:
response = await request(url) response = await request(url)
chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0] chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0]
json_data = json.loads(chaos_data) # 转为 json json_data = jsonlib.loads(chaos_data) # 转为 json
for data in json_data: for data in json_data:
name = re.findall(r">(.*)<", data[1])[0] name = re.findall(r">(.*)<", data[1])[0]
if name in ["「一心传」名刀", "石英大剑", "琥珀玥", "黑檀弓"]: # 跳过特殊的武器 if name in ["「一心传」名刀", "石英大剑", "琥珀玥", "黑檀弓"]: # 跳过特殊的武器
@ -85,12 +89,12 @@ async def get_material_data() -> DATA_TYPE:
urls = weapon + talent + namecard urls = weapon + talent + namecard
response = await request("https://api.ambr.top/v2/chs/material") response = await request("https://api.ambr.top/v2/chs/material")
ambr_data = json.loads(response.text)["data"]["items"] ambr_data = jsonlib.loads(response.text)["data"]["items"]
for url in urls: for url in urls:
response = await request(url) response = await request(url)
chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0] chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0]
json_data = json.loads(chaos_data) # 转为 json json_data = jsonlib.loads(chaos_data) # 转为 json
for data in json_data: for data in json_data:
honey_id = re.findall(r"/(.*?)/", data[1])[0] honey_id = re.findall(r"/(.*?)/", data[1])[0]
name = re.findall(r">(.*)<", data[1])[0] name = re.findall(r">(.*)<", data[1])[0]
@ -108,18 +112,18 @@ async def get_artifact_data() -> DATA_TYPE:
async def get_first_id(_link) -> str: async def get_first_id(_link) -> str:
_response = await request(_link) _response = await request(_link)
_chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", _response.text)[0] _chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", _response.text)[0]
_json_data = json.loads(_chaos_data) _json_data = jsonlib.loads(_chaos_data)
return re.findall(r"/(.*?)/", _json_data[-1][1])[0] return re.findall(r"/(.*?)/", _json_data[-1][1])[0]
result = {} result = {}
url = "https://genshin.honeyhunterworld.com/fam_art_set/?lang=CHS" url = "https://genshin.honeyhunterworld.com/fam_art_set/?lang=CHS"
response = await request("https://api.ambr.top/v2/chs/reliquary") response = await request("https://api.ambr.top/v2/chs/reliquary")
ambr_data = json.loads(response.text)["data"]["items"] ambr_data = jsonlib.loads(response.text)["data"]["items"]
response = await request(url) response = await request(url)
chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0] chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0]
json_data = json.loads(chaos_data) # 转为 json json_data = jsonlib.loads(chaos_data) # 转为 json
for data in json_data: for data in json_data:
honey_id = re.findall(r"/(.*?)/", data[1])[0] honey_id = re.findall(r"/(.*?)/", data[1])[0]
name = re.findall(r"alt=\"(.*?)\"", data[0])[0] name = re.findall(r"alt=\"(.*?)\"", data[0])[0]
@ -151,7 +155,7 @@ async def get_namecard_data() -> DATA_TYPE:
response = await request(url) response = await request(url)
chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0] chaos_data = re.findall(r"sortable_data\.push\((.*?)\);\s*sortable_cur_page", response.text)[0]
json_data = json.loads(chaos_data) json_data = jsonlib.loads(chaos_data)
for data in json_data: for data in json_data:
honey_id = re.findall(r"/(.*?)/", data[1])[0] honey_id = re.findall(r"/(.*?)/", data[1])[0]
name = re.findall(r"alt=\"(.*?)\"", data[0])[0] name = re.findall(r"alt=\"(.*?)\"", data[0])[0]
@ -189,5 +193,5 @@ async def update_honey_metadata(overwrite: bool = True) -> FULL_DATA_TYPE | None
} }
path.parent.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
async with async_open(path, mode="w", encoding="utf-8") as file: async with async_open(path, mode="w", encoding="utf-8") as file:
await file.write(json.dumps(result, ensure_ascii=False, indent=4)) await file.write(jsonlib.dumps(result, ensure_ascii=False, indent=4))
return result return result

View File

@ -1,13 +1,17 @@
from contextlib import contextmanager from contextlib import contextmanager
from typing import Iterator, Dict from typing import Iterator, Dict
import ujson as json
from aiofiles import open as async_open from aiofiles import open as async_open
from httpx import URL, AsyncClient, RemoteProtocolError, Response from httpx import URL, AsyncClient, RemoteProtocolError, Response
from utils.const import AMBR_HOST, PROJECT_ROOT from utils.const import AMBR_HOST, PROJECT_ROOT
from utils.log import logger from utils.log import logger
try:
import ujson as jsonlib
except ImportError:
import json as jsonlib
__all__ = [ __all__ = [
"update_metadata_from_ambr", "update_metadata_from_ambr",
"update_metadata_from_github", "update_metadata_from_github",
@ -32,7 +36,7 @@ async def fix_metadata_from_ambr(json_data: Dict[str, Dict], data_type: str):
for wid in need_append_ids: for wid in need_append_ids:
url = AMBR_HOST.join(f"v2/chs/{data_type}/{wid}") url = AMBR_HOST.join(f"v2/chs/{data_type}/{wid}")
response = await client.get(url) response = await client.get(url)
json_data_ = json.loads(response.text)["data"] json_data_ = jsonlib.loads(response.text)["data"]
json_data[str(json_data_["id"])] = {k: json_data_[k] for k in need_attr} json_data[str(json_data_["id"])] = {k: json_data_[k] for k in need_attr}
@ -46,10 +50,10 @@ async def update_metadata_from_ambr(overwrite: bool = True):
url = AMBR_HOST.join(f"v2/chs/{target}") url = AMBR_HOST.join(f"v2/chs/{target}")
path.parent.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
response = await client.get(url) response = await client.get(url)
json_data = json.loads(response.text)["data"]["items"] json_data = jsonlib.loads(response.text)["data"]["items"]
await fix_metadata_from_ambr(json_data, target) await fix_metadata_from_ambr(json_data, target)
async with async_open(path, mode="w", encoding="utf-8") as file: async with async_open(path, mode="w", encoding="utf-8") as file:
data = json.dumps(json_data, ensure_ascii=False, indent=4) data = jsonlib.dumps(json_data, ensure_ascii=False, indent=4)
await file.write(data) await file.write(data)
result.append(json_data) result.append(json_data)
return result return result
@ -88,7 +92,7 @@ async def update_metadata_from_github(overwrite: bool = True):
if line in [" },", " }"]: if line in [" },", " }"]:
started = False started = False
if any("MATERIAL_NAMECARD" in x for x in cell): if any("MATERIAL_NAMECARD" in x for x in cell):
material_json_data.append(json.loads("{" + "".join(cell) + "}")) material_json_data.append(jsonlib.loads("{" + "".join(cell) + "}"))
cell = [] cell = []
continue continue
if started: if started:
@ -136,7 +140,7 @@ async def update_metadata_from_github(overwrite: bool = True):
} }
) )
async with async_open(path, mode="w", encoding="utf-8") as file: async with async_open(path, mode="w", encoding="utf-8") as file:
data = json.dumps(data, ensure_ascii=False, indent=4) data = jsonlib.dumps(data, ensure_ascii=False, indent=4)
await file.write(data) await file.write(data)
return data return data
except RemoteProtocolError as exc: except RemoteProtocolError as exc:

View File

@ -24,10 +24,10 @@ from utils.log._style import DEFAULT_STYLE
from utils.log._traceback import Traceback from utils.log._traceback import Traceback
try: try:
import ujson as json import ujson as jsonlib
from ujson import JSONDecodeError from ujson import JSONDecodeError
except ImportError: except ImportError:
import json import json as jsonlib
from json import JSONDecodeError from json import JSONDecodeError
if TYPE_CHECKING: if TYPE_CHECKING:
@ -250,7 +250,7 @@ class Handler(DefaultRichHandler):
if message is not None: if message is not None:
try: try:
if self.auto_load_json: if self.auto_load_json:
message_renderable = self.render_message(record, json.loads(message)) message_renderable = self.render_message(record, jsonlib.loads(message))
except JSONDecodeError: except JSONDecodeError:
pass pass
finally: finally: