全面更换orjson

This commit is contained in:
雅诺狐
2025-08-26 14:20:26 +08:00
parent bfabf896b5
commit 4a44ba9866
45 changed files with 991 additions and 1279 deletions

View File

@@ -3,7 +3,7 @@
Cookie服务模块
负责从多种来源获取、缓存和管理QZone的Cookie。
"""
import json
import orjson
from pathlib import Path
from typing import Callable, Optional, Dict
@@ -33,7 +33,7 @@ class CookieService:
cookie_file_path = self._get_cookie_file_path(qq_account)
try:
with open(cookie_file_path, "w", encoding="utf-8") as f:
json.dump(cookies, f)
f.write(orjson.dumps(cookies, option=orjson.OPT_INDENT_2).decode('utf-8'))
logger.info(f"Cookie已成功缓存至: {cookie_file_path}")
except IOError as e:
logger.error(f"无法写入Cookie文件 {cookie_file_path}: {e}")
@@ -44,8 +44,8 @@ class CookieService:
if cookie_file_path.exists():
try:
with open(cookie_file_path, "r", encoding="utf-8") as f:
return json.load(f)
except (IOError, json.JSONDecodeError) as e:
return orjson.loads(f.read())
except (IOError, orjson.JSONDecodeError) as e:
logger.error(f"无法读取或解析Cookie文件 {cookie_file_path}: {e}")
return None

View File

@@ -5,7 +5,7 @@ QQ空间服务模块
"""
import asyncio
import json
import orjson
import os
import random
import time
@@ -291,14 +291,14 @@ class QZoneService:
cookie_str = cookie_data["cookies"]
parsed_cookies = {k.strip(): v.strip() for k, v in (p.split('=', 1) for p in cookie_str.split('; ') if '=' in p)}
with open(cookie_file_path, "w", encoding="utf-8") as f:
json.dump(parsed_cookies, f)
orjson.dump(parsed_cookies, f)
logger.info(f"Cookie已更新并保存至: {cookie_file_path}")
return parsed_cookies
# 如果HTTP获取失败尝试读取本地文件
if cookie_file_path.exists():
with open(cookie_file_path, "r", encoding="utf-8") as f:
return json.load(f)
return orjson.loads(f)
return None
except Exception as e:
logger.error(f"更新或加载Cookie时发生异常: {e}")
@@ -422,7 +422,7 @@ class QZoneService:
logger.warning("所有图片上传失败,将发布纯文本说说")
res_text = await _request("POST", self.EMOTION_PUBLISH_URL, params={"g_tk": gtk}, data=post_data)
result = json.loads(res_text)
result = orjson.loads(res_text)
tid = result.get("tid", "")
if tid:
@@ -576,7 +576,7 @@ class QZoneService:
}
res_text = await _request("GET", self.LIST_URL, params=params)
json_str = res_text[len("_preloadCallback(") : -2]
json_data = json.loads(json_str)
json_data = orjson.loads(json_str)
if json_data.get("code") != 0:
return []

View File

@@ -3,7 +3,7 @@
历史记录工具模块
提供用于获取QQ空间发送历史的功能。
"""
import json
import orjson
import os
from pathlib import Path
from typing import Dict, Any, Optional, List
@@ -30,7 +30,7 @@ class _CookieManager:
if os.path.exists(cookie_file):
try:
with open(cookie_file, 'r', encoding='utf-8') as f:
return json.load(f)
return orjson.loads(f.read())
except Exception as e:
logger.error(f"加载Cookie文件失败: {e}")
return None
@@ -67,7 +67,7 @@ class _SimpleQZoneAPI:
data = res.text
json_str = data[len('_preloadCallback('):-2] if data.startswith('_preloadCallback(') else data
json_data = json.loads(json_str)
json_data = orjson.loads(json_str)
return json_data.get("msglist", [])
except Exception as e: