perf(app): 优化 Cookie 处理逻辑

This commit is contained in:
Quan
2026-02-05 10:06:25 +08:00
parent 54f80171ad
commit 89c33e633c
5 changed files with 24 additions and 16 deletions

View File

@@ -20,7 +20,7 @@ class Html:
self.print = manager.print
self.retry = manager.retry
self.client = manager.request_client
self.headers = manager.headers
self.headers = manager.blank_headers
self.timeout = manager.timeout
@retry

View File

@@ -20,18 +20,19 @@ class UserPosted:
):
self.url = url
self.params = params
self.headers = manager.headers.copy()
self.update_cookie(cookies)
self.cookies = self.headers["cookie"]
self.headers = manager.blank_headers.copy()
self.client = manager.request_client
self.cookies = self.get_cookie(cookies)
self.print = manager.print
self.retry = manager.retry
self.client = manager.request_client
self.timeout = manager.timeout
self.proxy = proxy
def update_cookie(self, cookies: str = None) -> None:
def get_cookie(self, cookies: str = None) -> dict | str:
if cookies:
self.headers["cookie"] = cookies
return cookies
return dict(self.client.cookies)
def run(
self,

View File

@@ -2,6 +2,7 @@ from pathlib import Path
from re import compile, sub
from shutil import move, rmtree
from os import utime
from http.cookies import SimpleCookie
from httpx import (
AsyncClient,
AsyncHTTPTransport,
@@ -85,9 +86,6 @@ class Manager:
self.blank_headers = HEADERS | {
"user-agent": user_agent or USERAGENT,
}
self.headers = self.blank_headers | {
"cookie": cookie,
}
self.retry = retry
self.chunk = chunk
self.name_format = self.__check_name_format(name_format)
@@ -100,10 +98,11 @@ class Manager:
self.print_proxy_tip()
self.timeout = timeout
self.request_client = AsyncClient(
headers=self.headers
headers=self.blank_headers
| {
"referer": "https://www.xiaohongshu.com/",
},
cookies=self.cookie_str_to_dict(cookie),
timeout=timeout,
verify=False,
follow_redirects=True,
@@ -299,3 +298,9 @@ class Manager:
and not self.folder.exists()
):
move(old, self.folder)
@staticmethod
def cookie_str_to_dict(cookie_str: str) -> dict:
cookie = SimpleCookie()
cookie.load(cookie_str)
return {key: morsel.value for key, morsel in cookie.items()}

View File

@@ -51,7 +51,7 @@ def logging(log: Callable, text, style=INFO):
async def sleep_time(
min_time: int | float = 2.0,
max_time: int | float = 4.0,
min_time: int | float = 5.0,
max_time: int | float = 10.0,
):
await sleep(uniform(min_time, max_time))

View File

@@ -4,10 +4,12 @@
2. 新增 `script_server` 配置参数
3. 移除 `从浏览器读取 Cookie` 功能
4. 新增连接用户脚本下载作品功能
5. 修复视频作品下载功能
6. 新增作品处理统计功能
7. 重构数据处理逻辑
8. 调整内置延时机制
5. 优化 Cookie 处理逻辑
6. 修复视频作品下载功能
7. 新增视频下载偏好设置
8. 新增作品处理统计功能
9. 重构数据处理逻辑
10. 调整内置延时机制
*****