🎨 Use ujson to Convert Data

This commit is contained in:
洛水居室 2023-01-07 16:00:32 +08:00
parent 24ce0dac92
commit 16dfd5021e
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
4 changed files with 29 additions and 9 deletions

View File

@ -1,4 +1,3 @@
import json
from io import BytesIO from io import BytesIO
import genshin import genshin
@ -39,6 +38,12 @@ from utils.helpers import get_genshin_client
from utils.log import logger from utils.log import logger
from utils.models.base import RegionEnum 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) INPUT_URL, INPUT_FILE, CONFIRM_DELETE = range(10100, 10103)
@ -62,7 +67,7 @@ class GachaLogPlugin(Plugin.Conversation, BasePlugin.Conversation):
async def __async_init__(self): async def __async_init__(self):
await update_paimon_moe_zh(False) await update_paimon_moe_zh(False)
async with async_open(GACHA_LOG_PAIMON_MOE_PATH, "r", encoding="utf-8") as load_f: 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( async def _refresh_user_data(
self, user: User, data: dict = None, authkey: str = None, verify_uid: bool = True 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) await (await document.get_file()).download_to_memory(out=out)
if file_type == "json": if file_type == "json":
# bytesio to json # bytesio to json
data = json.loads(out.getvalue().decode("utf-8")) data = jsonlib.loads(out.getvalue().decode("utf-8"))
elif file_type == "xlsx": elif file_type == "xlsx":
data = self.gacha_log.convert_xlsx_to_uigf(out, self.zh_dict) data = self.gacha_log.convert_xlsx_to_uigf(out, self.zh_dict)
else: else:

View File

@ -1,4 +1,3 @@
import json
from os import sep from os import sep
from telegram import Update from telegram import Update
@ -12,6 +11,12 @@ from utils.decorators.error import error_callable
from utils.decorators.restricts import restricts from utils.decorators.restricts import restricts
from utils.log import logger from utils.log import logger
try:
import ujson as jsonlib
except ImportError:
import json as jsonlib
class HilichurlsPlugin(Plugin, BasePlugin): class HilichurlsPlugin(Plugin, BasePlugin):
"""丘丘语字典.""" """丘丘语字典."""
@ -19,7 +24,7 @@ class HilichurlsPlugin(Plugin, BasePlugin):
def __init__(self): def __init__(self):
"""加载数据文件.数据整理自 https://wiki.biligame.com/ys By @zhxycn.""" """加载数据文件.数据整理自 https://wiki.biligame.com/ys By @zhxycn."""
with open(f"resources{sep}json{sep}hilichurls_dictionary.json", "r", encoding="utf8") as f: 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) @handler(CommandHandler, command="hilichurls", block=False)
@restricts() @restricts()

View File

@ -1,4 +1,3 @@
import json
import os import os
import time import time
import traceback import traceback
@ -15,6 +14,12 @@ from core.plugin import Plugin, error_handler
from modules.errorpush import PbClient, SentryClient, PbClientException, SentryClientException from modules.errorpush import PbClient, SentryClient, PbClientException, SentryClientException
from utils.log import logger 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 notice_chat_id = bot.config.error.notification_chat_id
current_dir = os.getcwd() current_dir = os.getcwd()
logs_dir = os.path.join(current_dir, "logs") logs_dir = os.path.join(current_dir, "logs")
@ -52,7 +57,7 @@ class ErrorHandler(Plugin):
error_text = ( error_text = (
f"-----Exception while handling an update-----\n" 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.chat_data = {str(context.chat_data)}\n"
f"context.user_data = {str(context.user_data)}\n" f"context.user_data = {str(context.user_data)}\n"
"\n" "\n"

View File

@ -1,5 +1,4 @@
import asyncio import asyncio
import json
import os import os
from sys import executable from sys import executable
@ -15,6 +14,12 @@ from utils.decorators.admins import bot_admins_rights_check
from utils.helpers import execute from utils.helpers import execute
from utils.log import logger from utils.log import logger
try:
import ujson as jsonlib
except ImportError:
import json as jsonlib
current_dir = os.getcwd() current_dir = os.getcwd()
UPDATE_DATA = os.path.join(current_dir, "data", "update.json") UPDATE_DATA = os.path.join(current_dir, "data", "update.json")
@ -28,7 +33,7 @@ class UpdatePlugin(Plugin):
async def __async_init__(): async def __async_init__():
if os.path.exists(UPDATE_DATA): if os.path.exists(UPDATE_DATA):
async with async_open(UPDATE_DATA) as file: async with async_open(UPDATE_DATA) as file:
data = json.loads(await file.read()) data = jsonlib.loads(await file.read())
try: try:
reply_text = Message.de_json(data, bot.app.bot) reply_text = Message.de_json(data, bot.app.bot)
await reply_text.edit_text("重启成功") await reply_text.edit_text("重启成功")