@@ -15,118 +15,103 @@ def download_album(jm_album_id, option=None):
1515 option , jm_client = build_client (option )
1616 album : JmAlbumDetail = jm_client .get_album_detail (jm_album_id )
1717
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 )
6923 )
24+ option .after_album (album )
7025
7126
7227def download_photo (jm_photo_id , option = None ):
7328 """
7429 下载一个本子的一章,入口api
7530 """
7631 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 )
7934
8035
81- def download_by_photo_detail (photo_detail : JmPhotoDetail ,
82- option = None ,
83- ):
36+ def download_by_photo_detail (photo : JmPhotoDetail , option = None ):
8437 """
85- 下载一个本子的一章,根据 photo_detail
86- @param photo_detail : 本子章节信息
38+ 下载一个本子的一章,根据 photo
39+ @param photo : 本子章节信息
8740 @param option: 选项
8841 """
8942 option , jm_client = build_client (option )
9043
9144 # 下载准备
9245 use_cache = option .download_cache
9346 decode_image = option .download_image_decode
94- jm_client .check_photo (photo_detail )
47+ jm_client .check_photo (photo )
9548
9649 # 下载每个图片的函数
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 )
10052
10153 # 已下载过,缓存命中
10254 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
10656 return
10757
108- # 开始下载
58+ option . before_image ( image , img_save_path )
10959 jm_client .download_by_image_detail (
11060 image ,
11161 img_save_path ,
11262 decode_image = decode_image ,
11363 )
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 )
11473
115- jm_debug (debug_topic ,
116- f'图片下载完成: { debug_tag } , [{ image .img_url } ] → [{ img_save_path } ]'
117- )
11874
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+ # 一图一线程
121105 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 ,
124108 )
125109 else :
110+ # 创建batch个线程的线程池,当图片数>batch时要等待。
126111 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 ,
130115 )
131116
132117
0 commit comments