Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e4dd0fd

Browse filesBrowse files
committed
feat: 新增压缩保存response.content工具类
1 parent 0e1ae1b commit e4dd0fd
Copy full SHA for e4dd0fd

2 files changed

+112Lines changed: 112 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
# @FileName: compress_response_content.py
3+
4+
import zlib
5+
from hashlib import sha1
6+
import os
7+
8+
9+
def default_generate_save_path_method(url: str, base_dir: str) -> str:
10+
"""
11+
默认生成存贮文件路径函数(采用对URL取sha1, 然后分别取[9, 19], [29, 39]位作为路径分散存储, 目的是防止单个路径下存储过多文件)
12+
"""
13+
url_hash = sha1(url.encode('utf-8')).hexdigest()
14+
hash_dir_path = os.path.join('%s%s' % (url_hash[9], url_hash[19]), '%s%s' % (url_hash[29], url_hash[39]))
15+
dir_path = (os.path.join(base_dir, hash_dir_path))
16+
# 如果这个路径不存在,创建新的
17+
if not os.path.exists(dir_path):
18+
os.makedirs(dir_path)
19+
filename = url_hash + '.zlib'
20+
return os.path.join(dir_path, filename)
21+
22+
23+
def default_compress_algorithm(content: bytes) -> bytes:
24+
"""
25+
默认response压缩算法(zlib)
26+
"""
27+
return zlib.compress(content)
28+
29+
30+
def compress_response(url: str, content: bytes, base_dir: str, get_path=None, compress_algorithm=None) -> str:
31+
"""
32+
压缩并存储返回内容, 返回保存的路径
33+
get_path: 获取存储的路径(入参为请求的URL), 如需自定义, 必须为callable
34+
compress_algorithm: 压缩算法(入参为response.content), 如需自定义, 必须为callable
35+
"""
36+
if get_path is None:
37+
get_path = default_generate_save_path_method
38+
# 默认采用zlib压缩
39+
if compress_algorithm is None:
40+
compress_algorithm = default_compress_algorithm
41+
# 检测callable
42+
assert hasattr(get_path, '__call__')
43+
assert hasattr(compress_algorithm, '__call__')
44+
45+
save_path = get_path(url, base_dir)
46+
compress_data = compress_algorithm(content)
47+
with open(save_path, 'wb') as f:
48+
f.write(compress_data)
49+
return save_path
Collapse file
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
# @FileName: test_compress_response_content.py
3+
4+
import os
5+
6+
from feapder.utils.compress_response_content import compress_response
7+
8+
test_url = 'https://www.baidu.com'
9+
10+
test_content = b"""
11+
<!DOCTYPE html>
12+
<html lang="en">
13+
<head>
14+
<meta charset="UTF-8">
15+
<title>TITLE</title>
16+
</head>
17+
<body>
18+
19+
</body>
20+
</html
21+
"""
22+
23+
24+
def test_compress_response_content_success():
25+
saved_path = compress_response(test_url, test_content, './')
26+
assert os.path.exists(saved_path)
27+
# 清理测试保存的文件
28+
os.remove(saved_path)
29+
os.rmdir('./c3/41')
30+
os.rmdir('./c3')
31+
32+
33+
def no_compress(content: bytes) -> bytes:
34+
return content
35+
36+
37+
def test_compress_response_content_with_custom_compress_alg_success():
38+
saved_path = compress_response(test_url, test_content, './', compress_algorithm=no_compress)
39+
assert os.path.exists(saved_path)
40+
with open(saved_path, 'rb') as f:
41+
compressed_content = f.read()
42+
assert compressed_content == test_content
43+
# 清理测试保存的文件
44+
os.remove(saved_path)
45+
os.rmdir('./c3/41')
46+
os.rmdir('./c3')
47+
48+
49+
def custom_get_path_method(api: str, base_dir: str) -> str:
50+
return './test.html'
51+
52+
53+
def test_compress_content_with_custom_path_success():
54+
target_path = './test.html'
55+
saved_path = compress_response(test_url, test_content, './', get_path=custom_get_path_method)
56+
assert target_path == saved_path
57+
os.remove(saved_path)
58+
59+
60+
if __name__ == '__main__':
61+
test_compress_response_content_success()
62+
test_compress_response_content_with_custom_compress_alg_success()
63+
test_compress_content_with_custom_path_success()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.