|
1 |
| -from .jm_option import * |
2 |
| - |
3 |
| - |
4 |
| -def download_album(jm_album_id, option=None): |
5 |
| - """ |
6 |
| - 下载一个本子集,入口api |
7 |
| - @param jm_album_id: 禁漫的本子的id,类型可以是str/int/iterable[str]。 |
8 |
| - 如果是iterable[str],则会调用批量下载方法 download_album_batch |
9 |
| - @param option: 下载选项,为空默认是 JmOption.default() |
10 |
| - """ |
11 |
| - |
12 |
| - if not isinstance(jm_album_id, (str, int)): |
13 |
| - return download_album_batch(jm_album_id, option) |
14 |
| - |
15 |
| - option, jm_client = build_client(option) |
16 |
| - album: JmAlbumDetail = jm_client.get_album_detail(jm_album_id) |
17 |
| - |
18 |
| - option.before_album(album) |
19 |
| - execute_by_condition( |
20 |
| - iter_objs=album, |
21 |
| - apply=lambda photo: download_by_photo_detail(photo, option), |
22 |
| - count_batch=option.decide_photo_batch_count(album) |
23 |
| - ) |
24 |
| - option.after_album(album) |
25 |
| - |
26 |
| - |
27 |
| -def download_photo(jm_photo_id, option=None): |
28 |
| - """ |
29 |
| - 下载一个本子的一章,入口api |
30 |
| - """ |
31 |
| - option, jm_client = build_client(option) |
32 |
| - photo = jm_client.get_photo_detail(jm_photo_id) |
33 |
| - download_by_photo_detail(photo, option) |
34 |
| - |
35 |
| - |
36 |
| -def download_by_photo_detail(photo: JmPhotoDetail, option=None): |
37 |
| - """ |
38 |
| - 下载一个本子的一章,根据 photo |
39 |
| - @param photo: 本子章节信息 |
40 |
| - @param option: 选项 |
41 |
| - """ |
42 |
| - option, jm_client = build_client(option) |
43 |
| - |
44 |
| - # 下载准备 |
45 |
| - use_cache = option.download_cache |
46 |
| - decode_image = option.download_image_decode |
47 |
| - jm_client.check_photo(photo) |
48 |
| - |
49 |
| - # 下载每个图片的函数 |
50 |
| - def download_image(image: JmImageDetail): |
51 |
| - img_save_path = option.decide_image_filepath(image) |
52 |
| - image.is_exists = file_exists(img_save_path) |
53 |
| - |
54 |
| - option.before_image(image, img_save_path) |
55 |
| - if use_cache is True and image.is_exists: |
56 |
| - return |
57 |
| - jm_client.download_by_image_detail( |
58 |
| - image, |
59 |
| - img_save_path, |
60 |
| - decode_image=decode_image, |
61 |
| - ) |
62 |
| - option.after_image(image, img_save_path) |
63 |
| - |
64 |
| - option.before_photo(photo) |
65 |
| - execute_by_condition( |
66 |
| - iter_objs=photo, |
67 |
| - apply=download_image, |
68 |
| - count_batch=option.decide_image_batch_count(photo) |
69 |
| - ) |
70 |
| - option.before_photo(photo) |
| 1 | +from .jm_downloader import * |
71 | 2 |
|
72 | 3 |
|
73 | 4 | def download_album_batch(jm_album_id_iter: Union[Iterable, Generator],
|
74 | 5 | option=None,
|
75 |
| - wait_finish=True, |
76 |
| - ) -> List[Thread]: |
| 6 | + ): |
77 | 7 | """
|
78 |
| - 批量下载album,每个album一个线程,使用的是同一个option。 |
| 8 | + 批量下载album. |
| 9 | + 一个album,对应一个线程,对应一个option |
79 | 10 |
|
80 |
| - @param jm_album_id_iter: album_id的可迭代对象 |
| 11 | + @param jm_album_id_iter: album_id的迭代器 |
81 | 12 | @param option: 下载选项,为空默认是 JmOption.default()
|
82 |
| - @param wait_finish: 是否要等待这些下载线程全部完成 |
83 |
| - @return 返回值是List[Thread],里面是每个下载漫画的线程。 |
84 | 13 | """
|
85 |
| - if option is None: |
86 |
| - option = JmOption.default() |
| 14 | + from common import multi_thread_launcher |
87 | 15 |
|
88 |
| - return thread_pool_executor( |
89 |
| - iter_objs=set(JmcomicText.parse_to_album_id(album_id) for album_id in jm_album_id_iter), |
90 |
| - apply_each_obj_func=lambda album_id: download_album(album_id, option), |
91 |
| - wait_finish=wait_finish, |
| 16 | + return multi_thread_launcher( |
| 17 | + iter_objs=set( |
| 18 | + JmcomicText.parse_to_album_id(album_id) |
| 19 | + for album_id in jm_album_id_iter |
| 20 | + ), |
| 21 | + apply_each_obj_func=lambda aid: download_album(aid, option), |
92 | 22 | )
|
93 | 23 |
|
94 | 24 |
|
95 |
| -def execute_by_condition(iter_objs, apply: Callable, count_batch: int): |
| 25 | +def download_album(jm_album_id, option=None): |
96 | 26 | """
|
97 |
| - 章节/图片的下载调度逻辑 |
| 27 | + 下载一个本子 |
| 28 | + @param jm_album_id: 禁漫的本子的id,类型可以是str/int/iterable[str]。 |
| 29 | + 如果是iterable[str],则会调用 download_album_batch |
| 30 | + @param option: 下载选项,为空默认是 JmOption.default() |
98 | 31 | """
|
99 |
| - count_real = len(iter_objs) |
100 | 32 |
|
101 |
| - if count_batch >= count_real: |
102 |
| - # 一个图/章节 对应 一个线程 |
103 |
| - multi_thread_launcher( |
104 |
| - iter_objs=iter_objs, |
105 |
| - apply_each_obj_func=apply, |
106 |
| - ) |
107 |
| - else: |
108 |
| - # 创建batch个线程的线程池 |
109 |
| - thread_pool_executor( |
110 |
| - iter_objs=iter_objs, |
111 |
| - apply_each_obj_func=apply, |
112 |
| - max_workers=count_batch, |
113 |
| - ) |
| 33 | + if not isinstance(jm_album_id, (str, int)): |
| 34 | + return download_album_batch(jm_album_id, option) |
114 | 35 |
|
| 36 | + with new_downloader(option) as dler: |
| 37 | + dler.download_album(jm_album_id) |
115 | 38 |
|
116 |
| -def build_client(option: Optional[JmOption]) -> Tuple[JmOption, JmcomicClient]: |
| 39 | + |
| 40 | +def download_photo(jm_photo_id, option=None): |
117 | 41 | """
|
118 |
| - 处理option的判空,并且创建jm_client |
| 42 | + 下载一个章节 |
119 | 43 | """
|
| 44 | + with new_downloader(option) as dler: |
| 45 | + dler.download_photo(jm_photo_id) |
| 46 | + |
| 47 | + |
| 48 | +def new_downloader(option=None): |
120 | 49 | if option is None:
|
121 |
| - option = JmOption.default() |
| 50 | + option = JmModuleConfig.option_class().default() |
122 | 51 |
|
123 |
| - jm_client = option.build_jm_client() |
124 |
| - return option, jm_client |
| 52 | + return JmModuleConfig.downloader_class()(option) |
125 | 53 |
|
126 | 54 |
|
127 |
| -def create_option(filepath: str) -> JmOption: |
128 |
| - option = JmOption.from_file(filepath) |
129 |
| - return option |
| 55 | +def create_option(filepath): |
| 56 | + return JmModuleConfig.option_class().from_file(filepath) |
0 commit comments