Skip to content

Commit 89c33e6

Browse files
committed
perf(app): 优化 Cookie 处理逻辑
1 parent 54f8017 commit 89c33e6

5 files changed

Lines changed: 24 additions & 16 deletions

File tree

source/application/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(
2020
self.print = manager.print
2121
self.retry = manager.retry
2222
self.client = manager.request_client
23-
self.headers = manager.headers
23+
self.headers = manager.blank_headers
2424
self.timeout = manager.timeout
2525

2626
@retry

source/application/user_posted.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ def __init__(
2020
):
2121
self.url = url
2222
self.params = params
23-
self.headers = manager.headers.copy()
24-
self.update_cookie(cookies)
25-
self.cookies = self.headers["cookie"]
23+
self.headers = manager.blank_headers.copy()
24+
self.client = manager.request_client
25+
self.cookies = self.get_cookie(cookies)
2626
self.print = manager.print
2727
self.retry = manager.retry
28-
self.client = manager.request_client
2928
self.timeout = manager.timeout
3029
self.proxy = proxy
3130

32-
def update_cookie(self, cookies: str = None) -> None:
31+
def get_cookie(self, cookies: str = None) -> dict | str:
3332
if cookies:
3433
self.headers["cookie"] = cookies
34+
return cookies
35+
return dict(self.client.cookies)
3536

3637
def run(
3738
self,

source/module/manager.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from re import compile, sub
33
from shutil import move, rmtree
44
from os import utime
5+
from http.cookies import SimpleCookie
56
from httpx import (
67
AsyncClient,
78
AsyncHTTPTransport,
@@ -85,9 +86,6 @@ def __init__(
8586
self.blank_headers = HEADERS | {
8687
"user-agent": user_agent or USERAGENT,
8788
}
88-
self.headers = self.blank_headers | {
89-
"cookie": cookie,
90-
}
9189
self.retry = retry
9290
self.chunk = chunk
9391
self.name_format = self.__check_name_format(name_format)
@@ -100,10 +98,11 @@ def __init__(
10098
self.print_proxy_tip()
10199
self.timeout = timeout
102100
self.request_client = AsyncClient(
103-
headers=self.headers
101+
headers=self.blank_headers
104102
| {
105103
"referer": "https://www.xiaohongshu.com/",
106104
},
105+
cookies=self.cookie_str_to_dict(cookie),
107106
timeout=timeout,
108107
verify=False,
109108
follow_redirects=True,
@@ -299,3 +298,9 @@ def compatible(
299298
and not self.folder.exists()
300299
):
301300
move(old, self.folder)
301+
302+
@staticmethod
303+
def cookie_str_to_dict(cookie_str: str) -> dict:
304+
cookie = SimpleCookie()
305+
cookie.load(cookie_str)
306+
return {key: morsel.value for key, morsel in cookie.items()}

source/module/tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def logging(log: Callable, text, style=INFO):
5151

5252

5353
async def sleep_time(
54-
min_time: int | float = 2.0,
55-
max_time: int | float = 4.0,
54+
min_time: int | float = 5.0,
55+
max_time: int | float = 10.0,
5656
):
5757
await sleep(uniform(min_time, max_time))

static/Release_Notes.md

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

1214
*****
1315

0 commit comments

Comments
 (0)