From 16dfd5021eb99d54de81224067d056221d07cae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sat, 7 Jan 2023 16:00:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Use=20`ujson`=20to=20Convert=20D?= =?UTF-8?q?ata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/genshin/gacha/gacha_log.py | 11 ++++++++--- plugins/genshin/hilichurls.py | 9 +++++++-- plugins/system/errorhandler.py | 9 +++++++-- plugins/system/update.py | 9 +++++++-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/plugins/genshin/gacha/gacha_log.py b/plugins/genshin/gacha/gacha_log.py index 11297bfa..5b99f18f 100644 --- a/plugins/genshin/gacha/gacha_log.py +++ b/plugins/genshin/gacha/gacha_log.py @@ -1,4 +1,3 @@ -import json from io import BytesIO import genshin @@ -39,6 +38,12 @@ from utils.helpers import get_genshin_client from utils.log import logger from utils.models.base import RegionEnum +try: + import ujson as jsonlib + +except ImportError: + import json as jsonlib + INPUT_URL, INPUT_FILE, CONFIRM_DELETE = range(10100, 10103) @@ -62,7 +67,7 @@ class GachaLogPlugin(Plugin.Conversation, BasePlugin.Conversation): async def __async_init__(self): await update_paimon_moe_zh(False) async with async_open(GACHA_LOG_PAIMON_MOE_PATH, "r", encoding="utf-8") as load_f: - self.zh_dict = json.loads(await load_f.read()) + self.zh_dict = jsonlib.loads(await load_f.read()) async def _refresh_user_data( self, user: User, data: dict = None, authkey: str = None, verify_uid: bool = True @@ -117,7 +122,7 @@ class GachaLogPlugin(Plugin.Conversation, BasePlugin.Conversation): await (await document.get_file()).download_to_memory(out=out) if file_type == "json": # bytesio to json - data = json.loads(out.getvalue().decode("utf-8")) + data = jsonlib.loads(out.getvalue().decode("utf-8")) elif file_type == "xlsx": data = self.gacha_log.convert_xlsx_to_uigf(out, self.zh_dict) else: diff --git a/plugins/genshin/hilichurls.py b/plugins/genshin/hilichurls.py index f7330b35..dadfa82c 100644 --- a/plugins/genshin/hilichurls.py +++ b/plugins/genshin/hilichurls.py @@ -1,4 +1,3 @@ -import json from os import sep from telegram import Update @@ -12,6 +11,12 @@ from utils.decorators.error import error_callable from utils.decorators.restricts import restricts from utils.log import logger +try: + import ujson as jsonlib + +except ImportError: + import json as jsonlib + class HilichurlsPlugin(Plugin, BasePlugin): """丘丘语字典.""" @@ -19,7 +24,7 @@ class HilichurlsPlugin(Plugin, BasePlugin): def __init__(self): """加载数据文件.数据整理自 https://wiki.biligame.com/ys By @zhxycn.""" with open(f"resources{sep}json{sep}hilichurls_dictionary.json", "r", encoding="utf8") as f: - self.hilichurls_dictionary = json.load(f) + self.hilichurls_dictionary = jsonlib.load(f) @handler(CommandHandler, command="hilichurls", block=False) @restricts() diff --git a/plugins/system/errorhandler.py b/plugins/system/errorhandler.py index 4c623879..d4a50de9 100644 --- a/plugins/system/errorhandler.py +++ b/plugins/system/errorhandler.py @@ -1,4 +1,3 @@ -import json import os import time import traceback @@ -15,6 +14,12 @@ from core.plugin import Plugin, error_handler from modules.errorpush import PbClient, SentryClient, PbClientException, SentryClientException from utils.log import logger +try: + import ujson as jsonlib + +except ImportError: + import json as jsonlib + notice_chat_id = bot.config.error.notification_chat_id current_dir = os.getcwd() logs_dir = os.path.join(current_dir, "logs") @@ -52,7 +57,7 @@ class ErrorHandler(Plugin): error_text = ( f"-----Exception while handling an update-----\n" - f"update = {json.dumps(update_str, indent=2, ensure_ascii=False)}\n" + f"update = {jsonlib.dumps(update_str, indent=2, ensure_ascii=False)}\n" f"context.chat_data = {str(context.chat_data)}\n" f"context.user_data = {str(context.user_data)}\n" "\n" diff --git a/plugins/system/update.py b/plugins/system/update.py index 88dd8468..8e58d60f 100644 --- a/plugins/system/update.py +++ b/plugins/system/update.py @@ -1,5 +1,4 @@ import asyncio -import json import os from sys import executable @@ -15,6 +14,12 @@ from utils.decorators.admins import bot_admins_rights_check from utils.helpers import execute from utils.log import logger +try: + import ujson as jsonlib + +except ImportError: + import json as jsonlib + current_dir = os.getcwd() UPDATE_DATA = os.path.join(current_dir, "data", "update.json") @@ -28,7 +33,7 @@ class UpdatePlugin(Plugin): async def __async_init__(): if os.path.exists(UPDATE_DATA): async with async_open(UPDATE_DATA) as file: - data = json.loads(await file.read()) + data = jsonlib.loads(await file.read()) try: reply_text = Message.de_json(data, bot.app.bot) await reply_text.edit_text("重启成功")