Skip to content

Commit 367ba05

Browse files
committed
refactor: 优化xhs签名js编译一次
1 parent 0098935 commit 367ba05

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

service/xhs/logic/common.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Optional
12
from lib.logger import logger
23
from lib import requests
34
import execjs
@@ -25,8 +26,13 @@
2526
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
2627
}
2728

29+
# 在程序启动时编译 JavaScript 代码
30+
with open('lib/js/xhs.js', encoding='utf-8') as f:
31+
xhs_sign_obj = execjs.compile(f.read())
2832

29-
async def common_request(uri: str, params: dict, headers: dict, need_sign: bool = True, post: bool = True) -> tuple[dict, bool]:
33+
34+
async def common_request(uri: str, params: dict, headers: dict, need_sign: bool = True, post: bool = True) -> tuple[
35+
dict, bool]:
3036
"""
3137
请求 xhs
3238
:param uri: 请求路径
@@ -36,13 +42,8 @@ async def common_request(uri: str, params: dict, headers: dict, need_sign: bool
3642
"""
3743
url = f'{API_HOST}{uri}'
3844
headers.update(COMMON_HEADERS)
39-
4045
if post:
41-
if need_sign:
42-
xhs_sign_obj = execjs.compile(open('lib/js/xhs.js', encoding='utf-8').read())
43-
sign_header = xhs_sign_obj.call('sign', uri, params, headers.get('cookie', ''))
44-
headers.update(sign_header)
45-
46+
sign_request(uri, params, headers, need_sign)
4647
logger.info(f'url: {url}, request {url}, params={params}, headers={headers}')
4748
body = json.dumps(params, separators=(',', ':'), ensure_ascii=False)
4849
response = await requests.post(url, data=body, headers=headers)
@@ -53,11 +54,7 @@ async def common_request(uri: str, params: dict, headers: dict, need_sign: bool
5354
uri = f'{uri}?{params_str}'
5455
url = f'{url}?{params_str}'
5556

56-
if need_sign:
57-
xhs_sign_obj = execjs.compile(open('lib/js/xhs.js', encoding='utf-8').read())
58-
sign_header = xhs_sign_obj.call('sign', uri, None, headers.get('cookie', ''))
59-
headers.update(sign_header)
60-
57+
sign_request(uri, None, headers, need_sign)
6158
logger.info(f'url: {url}, request {url}, params={params}, headers={headers}')
6259
response = await requests.get(url, headers=headers)
6360

@@ -68,9 +65,24 @@ async def common_request(uri: str, params: dict, headers: dict, need_sign: bool
6865
logger.error(
6966
f'url: {url}, params: {params}, request error, code: {response.status_code}, body: {response.text}')
7067
return {}, False
68+
7169
if response.json().get('code', 0) != 0:
7270
logger.error(
7371
f'url: {url}, params: {params}, request error, code: {response.status_code}, body: {response.text}')
7472
return response.json(), False
7573

76-
return response.json(), True
74+
return response.json(), True
75+
76+
77+
def sign_request(uri: str, params: Optional[dict], headers: dict, need_sign: bool) -> None:
78+
"""
79+
为请求添加签名
80+
:param uri:
81+
:param params:
82+
:param headers:
83+
:param need_sign:
84+
:return:
85+
"""
86+
if need_sign:
87+
sign_header = xhs_sign_obj.call('sign', uri, params, headers.get('cookie', ''))
88+
headers.update(sign_header)

0 commit comments

Comments
 (0)