@@ -15,118 +15,103 @@ def download_album(jm_album_id, option=None):
15
15
option , jm_client = build_client (option )
16
16
album : JmAlbumDetail = jm_client .get_album_detail (jm_album_id )
17
17
18
- jm_debug ('album' ,
19
- f'本子获取成功: [{ album .id } ], '
20
- f'作者: [{ album .author } ], '
21
- f'章节数: [{ len (album )} ], '
22
- f'标题: [{ album .title } ], '
23
- )
24
-
25
- def download_photo (photo : JmPhotoDetail ,
26
- debug_topic = 'photo' ,
27
- ):
28
- jm_client .check_photo (photo )
29
-
30
- jm_debug (debug_topic ,
31
- f'开始下载章节: { photo .id } ({ photo .album_id } [{ photo .index } /{ len (album )} ]), '
32
- f'标题: [{ photo .title } ], '
33
- f'图片数为[{ len (photo )} ]'
34
- )
35
-
36
- download_by_photo_detail (photo , option )
37
-
38
- jm_debug (debug_topic ,
39
- f'章节下载完成: { photo .id } ({ photo .album_id } [{ photo .index } /{ len (album )} ])'
40
- )
41
-
42
- thread_pool_executor (
43
- iter_objs = album ,
44
- apply_each_obj_func = download_photo ,
45
- )
46
-
47
- jm_debug ('album' , f'本子下载完成: [{ album .id } ]' )
48
-
49
-
50
- def download_album_batch (jm_album_id_iter : Union [Iterable , Generator ],
51
- option = None ,
52
- wait_finish = True ,
53
- ) -> List [Thread ]:
54
- """
55
- 批量下载album,每个album一个线程,使用的是同一个option。
56
-
57
- @param jm_album_id_iter: album_id的可迭代对象
58
- @param option: 下载选项,为空默认是 JmOption.default()
59
- @param wait_finish: 是否要等待这些下载线程全部完成
60
- @return 返回值是List[Thread],里面是每个下载漫画的线程。
61
- """
62
- if option is None :
63
- option = JmOption .default ()
64
-
65
- return thread_pool_executor (
66
- iter_objs = ((album_id , option ) for album_id in jm_album_id_iter ),
67
- apply_each_obj_func = download_album ,
68
- wait_finish = wait_finish ,
18
+ option .before_album (album )
19
+ execute_by_condition (
20
+ iter_obj = album ,
21
+ apply = lambda photo : download_by_photo_detail (photo , option ),
22
+ count_batch = option .decide_photo_batch_count (album )
69
23
)
24
+ option .after_album (album )
70
25
71
26
72
27
def download_photo (jm_photo_id , option = None ):
73
28
"""
74
29
下载一个本子的一章,入口api
75
30
"""
76
31
option , jm_client = build_client (option )
77
- photo_detail = jm_client .get_photo_detail (jm_photo_id )
78
- download_by_photo_detail (photo_detail , option )
32
+ photo = jm_client .get_photo_detail (jm_photo_id )
33
+ download_by_photo_detail (photo , option )
79
34
80
35
81
- def download_by_photo_detail (photo_detail : JmPhotoDetail ,
82
- option = None ,
83
- ):
36
+ def download_by_photo_detail (photo : JmPhotoDetail , option = None ):
84
37
"""
85
- 下载一个本子的一章,根据 photo_detail
86
- @param photo_detail : 本子章节信息
38
+ 下载一个本子的一章,根据 photo
39
+ @param photo : 本子章节信息
87
40
@param option: 选项
88
41
"""
89
42
option , jm_client = build_client (option )
90
43
91
44
# 下载准备
92
45
use_cache = option .download_cache
93
46
decode_image = option .download_image_decode
94
- jm_client .check_photo (photo_detail )
47
+ jm_client .check_photo (photo )
95
48
96
49
# 下载每个图片的函数
97
- def download_image (index , image : JmImageDetail , debug_topic = 'image' ):
98
- img_save_path = option .decide_image_filepath (photo_detail , index )
99
- debug_tag = f'{ image .aid } /{ image .filename } [{ index + 1 } /{ len (photo_detail )} ]'
50
+ def download_image (image : JmImageDetail ):
51
+ img_save_path = option .decide_image_filepath (image )
100
52
101
53
# 已下载过,缓存命中
102
54
if use_cache is True and file_exists (img_save_path ):
103
- jm_debug (debug_topic ,
104
- f'图片已存在: { debug_tag } ← [{ img_save_path } ]'
105
- )
55
+ image .is_exists = True
106
56
return
107
57
108
- # 开始下载
58
+ option . before_image ( image , img_save_path )
109
59
jm_client .download_by_image_detail (
110
60
image ,
111
61
img_save_path ,
112
62
decode_image = decode_image ,
113
63
)
64
+ option .after_image (image , img_save_path )
65
+
66
+ option .before_photo (photo )
67
+ execute_by_condition (
68
+ iter_obj = photo ,
69
+ apply = download_image ,
70
+ count_batch = option .decide_image_batch_count (photo )
71
+ )
72
+ option .before_photo (photo )
114
73
115
- jm_debug (debug_topic ,
116
- f'图片下载完成: { debug_tag } , [{ image .img_url } ] → [{ img_save_path } ]'
117
- )
118
74
119
- batch = option .download_threading_batch_count
120
- if batch <= 0 :
75
+ def download_album_batch (jm_album_id_iter : Union [Iterable , Generator ],
76
+ option = None ,
77
+ wait_finish = True ,
78
+ ) -> List [Thread ]:
79
+ """
80
+ 批量下载album,每个album一个线程,使用的是同一个option。
81
+
82
+ @param jm_album_id_iter: album_id的可迭代对象
83
+ @param option: 下载选项,为空默认是 JmOption.default()
84
+ @param wait_finish: 是否要等待这些下载线程全部完成
85
+ @return 返回值是List[Thread],里面是每个下载漫画的线程。
86
+ """
87
+ if option is None :
88
+ option = JmOption .default ()
89
+
90
+ return thread_pool_executor (
91
+ iter_objs = ((album_id , option ) for album_id in jm_album_id_iter ),
92
+ apply_each_obj_func = download_album ,
93
+ wait_finish = wait_finish ,
94
+ )
95
+
96
+
97
+ def execute_by_condition (iter_obj , apply : Callable , count_batch : int ):
98
+ """
99
+ 章节/图片的下载调度逻辑
100
+ """
101
+ count_real = len (iter_obj )
102
+
103
+ if count_batch >= count_real :
104
+ # 一图一线程
121
105
multi_thread_launcher (
122
- iter_objs = enumerate ( photo_detail ) ,
123
- apply_each_obj_func = download_image ,
106
+ iter_objs = iter_obj ,
107
+ apply_each_obj_func = apply ,
124
108
)
125
109
else :
110
+ # 创建batch个线程的线程池,当图片数>batch时要等待。
126
111
thread_pool_executor (
127
- iter_objs = enumerate ( photo_detail ) ,
128
- apply_each_obj_func = download_image ,
129
- max_workers = batch ,
112
+ iter_objs = iter_obj ,
113
+ apply_each_obj_func = apply ,
114
+ max_workers = count_batch ,
130
115
)
131
116
132
117
0 commit comments