jmcomic 2.7.1__tar.gz → 2.7.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {jmcomic-2.7.1/src/jmcomic.egg-info → jmcomic-2.7.2}/PKG-INFO +1 -1
  2. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/__init__.py +1 -1
  3. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/api.py +24 -5
  4. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_async_client.py +67 -30
  5. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_async_downloader.py +12 -1
  6. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_client_interface.py +25 -0
  7. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_config.py +16 -32
  8. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_option.py +13 -2
  9. {jmcomic-2.7.1 → jmcomic-2.7.2/src/jmcomic.egg-info}/PKG-INFO +1 -1
  10. {jmcomic-2.7.1 → jmcomic-2.7.2}/LICENSE +0 -0
  11. {jmcomic-2.7.1 → jmcomic-2.7.2}/README.md +0 -0
  12. {jmcomic-2.7.1 → jmcomic-2.7.2}/pyproject.toml +0 -0
  13. {jmcomic-2.7.1 → jmcomic-2.7.2}/setup.cfg +0 -0
  14. {jmcomic-2.7.1 → jmcomic-2.7.2}/setup.py +0 -0
  15. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/cl.py +0 -0
  16. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/cli.py +0 -0
  17. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_client_impl.py +0 -0
  18. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_downloader.py +0 -0
  19. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_entity.py +0 -0
  20. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_exception.py +0 -0
  21. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_feature.py +0 -0
  22. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_plugin.py +0 -0
  23. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic/jm_toolkit.py +0 -0
  24. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic.egg-info/SOURCES.txt +0 -0
  25. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic.egg-info/dependency_links.txt +0 -0
  26. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic.egg-info/entry_points.txt +0 -0
  27. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic.egg-info/requires.txt +0 -0
  28. {jmcomic-2.7.1 → jmcomic-2.7.2}/src/jmcomic.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jmcomic
3
- Version: 2.7.1
3
+ Version: 2.7.2
4
4
  Summary: Python API For JMComic (禁漫天堂)
5
5
  Home-page: https://github.com/hect0x7/JMComic-Crawler-Python
6
6
  Author: hect0x7
@@ -2,7 +2,7 @@
2
2
  # 被依赖方 <--- 使用方
3
3
  # config <--- entity <--- toolkit <--- client <--- option <--- downloader
4
4
 
5
- __version__ = '2.7.1'
5
+ __version__ = '2.7.2'
6
6
 
7
7
  from .api import *
8
8
  from .jm_plugin import *
@@ -1,10 +1,28 @@
1
1
  import asyncio
2
+ import inspect
2
3
 
3
4
  from .jm_downloader import *
4
5
 
5
6
  __DOWNLOAD_API_RET = DownloadResult
6
7
 
7
8
 
9
+ async def _invoke_async_callback(callback, entity, downloader):
10
+ if callback is None:
11
+ return None
12
+
13
+ is_async_callback = (
14
+ inspect.iscoroutinefunction(callback)
15
+ or inspect.iscoroutinefunction(getattr(callback, '__call__', None))
16
+ )
17
+ if is_async_callback:
18
+ return await callback(entity, downloader)
19
+
20
+ result = await asyncio.to_thread(callback, entity, downloader)
21
+ if inspect.isawaitable(result):
22
+ return await result
23
+ return result
24
+
25
+
8
26
  def download_batch(download_api,
9
27
  jm_id_iter: Union[Iterable, Generator],
10
28
  option=None,
@@ -167,7 +185,8 @@ async def download_album_async(jm_album_id,
167
185
  异步下载一个本子(album),包含其所有的章节(photo)。
168
186
 
169
187
  - 支持批量下载(当 jm_album_id 为可迭代对象时)
170
- - 返回 (album, downloader) 元组
188
+ - callback 支持同步函数和异步函数
189
+ - 返回 (album, downloader) 元组,其中 downloader 的网络和线程池资源已关闭,仅用于读取下载结果
171
190
  """
172
191
  if not isinstance(jm_album_id, (str, int)):
173
192
  return await download_batch_async(download_album_async,
@@ -181,8 +200,7 @@ async def download_album_async(jm_album_id,
181
200
  dler.add_features(extra, 'download_album')
182
201
  album = await dler.download_album(jm_album_id)
183
202
 
184
- if callback is not None:
185
- callback(album, dler)
203
+ await _invoke_async_callback(callback, album, dler)
186
204
  if check_exception:
187
205
  dler.raise_if_has_exception()
188
206
 
@@ -198,6 +216,8 @@ async def download_photo_async(jm_photo_id,
198
216
  ):
199
217
  """
200
218
  异步下载一个章节(photo)。
219
+ callback 支持同步函数和异步函数。
220
+ 返回的 downloader 已关闭网络和线程池资源,仅用于读取下载结果。
201
221
  """
202
222
  if not isinstance(jm_photo_id, (str, int)):
203
223
  return await download_batch_async(download_photo_async,
@@ -211,8 +231,7 @@ async def download_photo_async(jm_photo_id,
211
231
  dler.add_features(extra, 'download_photo')
212
232
  photo = await dler.download_photo(jm_photo_id)
213
233
 
214
- if callback is not None:
215
- callback(photo, dler)
234
+ await _invoke_async_callback(callback, photo, dler)
216
235
  if check_exception:
217
236
  dler.raise_if_has_exception()
218
237
 
@@ -7,6 +7,7 @@ from __future__ import annotations
7
7
 
8
8
  import asyncio
9
9
  import json
10
+ from typing import Sequence
10
11
  from urllib.parse import urlencode
11
12
 
12
13
  from curl_cffi.requests import AsyncSession
@@ -51,12 +52,15 @@ class AsyncJmApiClient(AsyncJmcomicClient):
51
52
  _SENTINEL = object()
52
53
 
53
54
  # 类级别初始化标记与锁,防止并发更新域名
54
- _has_setup_domain_and_cookies = False
55
+ _has_setup_domain = False
55
56
  _setup_lock = asyncio.Lock()
56
57
 
57
- def __init__(self, option: JmOption, max_clients=None, **kwargs):
58
+ def __init__(self, option: JmOption, max_clients=None, domain_list=None, **kwargs):
59
+ if 'domain_retry_strategy' in kwargs:
60
+ raise TypeError('Async client does not support domain_retry_strategy')
61
+
58
62
  self.option = option
59
- self._domain_list = self._resolve_domain_list()
63
+ self._domain_list = self._resolve_domain_list(domain_list)
60
64
  retry_times = option.client.get('retry_times')
61
65
  self._retry_times = retry_times if retry_times is not None else 5
62
66
  self._timeout = option.client.get('timeout', 30) or 30
@@ -84,22 +88,41 @@ class AsyncJmApiClient(AsyncJmcomicClient):
84
88
  # 域名管理
85
89
  # ======================================================================
86
90
 
87
- def _resolve_domain_list(self) -> list[str]:
91
+ @staticmethod
92
+ def _normalize_domain_list(domain_list) -> list[str]:
93
+ if isinstance(domain_list, str):
94
+ return [domain.strip() for domain in domain_list.splitlines() if domain.strip()]
95
+
96
+ if isinstance(domain_list, Sequence):
97
+ result = []
98
+ for domain in domain_list:
99
+ if not isinstance(domain, str):
100
+ raise TypeError(f'domain must be str, got {type(domain)}')
101
+ domain = domain.strip()
102
+ if domain:
103
+ result.append(domain)
104
+ return result
105
+
106
+ raise TypeError('domain_list must be str, Sequence[str], or None')
107
+
108
+ def _resolve_domain_list(self, domain_list=None) -> list[str]:
88
109
  """解析并返回可用的 API 域名列表"""
89
- updated = JmModuleConfig.DOMAIN_API_UPDATED_LIST
90
- if updated:
91
- return list(updated)
110
+ if domain_list is not None:
111
+ resolved = self._normalize_domain_list(domain_list)
112
+ if resolved:
113
+ return resolved
114
+
92
115
  domain = self.option.client.domain
93
116
  if hasattr(domain, 'get'):
94
117
  domain_list = domain.get('api', [])
95
- elif isinstance(domain, list):
96
- domain_list = domain
97
- elif isinstance(domain, str):
98
- domain_list = [d.strip() for d in domain.split('\n') if d.strip()]
99
118
  else:
100
- domain_list = []
101
- if domain_list:
102
- return domain_list
119
+ domain_list = domain
120
+
121
+ if domain_list is not None:
122
+ resolved = self._normalize_domain_list(domain_list)
123
+ if resolved:
124
+ return resolved
125
+
103
126
  return list(JmModuleConfig.DOMAIN_API_LIST)
104
127
 
105
128
  def get_domain_list(self) -> list[str]:
@@ -108,6 +131,20 @@ class AsyncJmApiClient(AsyncJmcomicClient):
108
131
  def set_domain_list(self, domain_list: list[str]):
109
132
  self._domain_list = domain_list
110
133
 
134
+ def update_old_api_domain(self, new_server_list: list[str]):
135
+ if not new_server_list:
136
+ return
137
+
138
+ if sorted(self._domain_list) != sorted(JmModuleConfig.DOMAIN_API_LIST):
139
+ return
140
+
141
+ old_server_list = self._domain_list
142
+ self._domain_list = list(new_server_list)
143
+ jm_log(
144
+ 'api.update_domain.replace',
145
+ f'替换异步客户端的内置API域名:(old){old_server_list} ---→ (new){self._domain_list}',
146
+ )
147
+
111
148
  # ======================================================================
112
149
  # 缓存
113
150
  # ======================================================================
@@ -659,8 +696,7 @@ class AsyncJmApiClient(AsyncJmcomicClient):
659
696
  return
660
697
 
661
698
  if JmModuleConfig.DOMAIN_API_UPDATED_LIST is not None:
662
- if JmModuleConfig.DOMAIN_API_UPDATED_LIST:
663
- self._domain_list = list(JmModuleConfig.DOMAIN_API_UPDATED_LIST)
699
+ self.update_old_api_domain(JmModuleConfig.DOMAIN_API_UPDATED_LIST)
664
700
  return
665
701
 
666
702
  # 尝试从域名服务器获取最新域名
@@ -683,8 +719,7 @@ class AsyncJmApiClient(AsyncJmcomicClient):
683
719
  jm_log('api.update_domain.success',
684
720
  f'获取到最新的API域名: {new_server_list}')
685
721
  JmModuleConfig.DOMAIN_API_UPDATED_LIST = new_server_list
686
- if sorted(self._domain_list) == sorted(JmModuleConfig.DOMAIN_API_LIST):
687
- self._domain_list = new_server_list
722
+ self.update_old_api_domain(new_server_list)
688
723
  return
689
724
  except Exception as e:
690
725
  jm_log('api.update_domain.error', f'通过[{url}]自动更新API域名失败: {e}')
@@ -708,20 +743,22 @@ class AsyncJmApiClient(AsyncJmcomicClient):
708
743
 
709
744
  cls = self.__class__
710
745
  async with cls._setup_lock:
711
- if not cls._has_setup_domain_and_cookies:
746
+ if self._has_setup:
747
+ return
748
+
749
+ if not cls._has_setup_domain:
712
750
  await self.auto_update_domain()
713
- if JmModuleConfig.FLAG_API_CLIENT_REQUIRE_COOKIES:
714
- await self.ensure_have_cookies()
715
- cls._has_setup_domain_and_cookies = True
751
+ cls._has_setup_domain = True
716
752
  else:
717
- # 即使已经初始化过域名和 cookie,也需要将已保存的全局 DOMAIN 和 COOKIES 赋值到当前 client
718
- if JmModuleConfig.DOMAIN_API_UPDATED_LIST:
719
- self._domain_list = list(JmModuleConfig.DOMAIN_API_UPDATED_LIST)
720
- if JmModuleConfig.FLAG_API_CLIENT_REQUIRE_COOKIES and JmModuleConfig.APP_COOKIES:
721
- # noinspection PyUnresolvedReferences
722
- self._session.cookies.update(JmModuleConfig.APP_COOKIES)
723
-
724
- self._has_setup = True
753
+ # 即使已经初始化过域名,也需要将已保存的全局 DOMAIN 赋值到当前 client
754
+ if JmModuleConfig.FLAG_API_CLIENT_AUTO_UPDATE_DOMAIN:
755
+ self.update_old_api_domain(JmModuleConfig.DOMAIN_API_UPDATED_LIST)
756
+
757
+ # Cookie 属于 session 状态,每个 client 都需要独立确认。
758
+ if JmModuleConfig.FLAG_API_CLIENT_REQUIRE_COOKIES:
759
+ await self.ensure_have_cookies()
760
+
761
+ self._has_setup = True
725
762
 
726
763
  # ======================================================================
727
764
  # 生命周期
@@ -257,7 +257,18 @@ class JmAsyncDownloader(BaseDownloader):
257
257
  async def __aenter__(self):
258
258
  # 创建并独占一个 async client(含 AsyncSession)。
259
259
  self.client = self.option.new_jm_async_client(max_clients=self._image_concurrency)
260
- await self.client.setup()
260
+ try:
261
+ await self.client.setup()
262
+ except BaseException:
263
+ try:
264
+ await self.client.close()
265
+ except BaseException as cleanup_error:
266
+ jm_log('dler.cleanup.exception',
267
+ f'初始化失败后的资源清理也发生异常: {cleanup_error}', cleanup_error)
268
+ finally:
269
+ self.client = None
270
+ self.shutdown()
271
+ raise
261
272
  return self
262
273
 
263
274
  async def __aexit__(self, exc_type, exc_val, exc_tb):
@@ -514,6 +514,7 @@ class JmcomicClient(
514
514
  return JmModuleConfig.get_html_domain_all(self.get_root_postman())
515
515
 
516
516
  def get_html_domain_all_via_github(self):
517
+ """已废弃,请使用 get_html_domain_all。"""
517
518
  return JmModuleConfig.get_html_domain_all_via_github(self.get_root_postman())
518
519
 
519
520
  # noinspection PyMethodMayBeStatic
@@ -815,6 +816,30 @@ class AsyncJmcomicClient:
815
816
  ) -> JmCategoryPage:
816
817
  raise NotImplementedError
817
818
 
819
+ async def categories_filter_gen(self,
820
+ page: int = 1,
821
+ time: str = JmMagicConstants.TIME_ALL,
822
+ category: str = JmMagicConstants.CATEGORY_ALL,
823
+ order_by: str = JmMagicConstants.ORDER_BY_LATEST,
824
+ sub_category: Optional[str] = None,
825
+ ):
826
+ """异步分类结果生成器,支持通过 asend 动态更新分页参数。"""
827
+ params = {
828
+ 'time': time,
829
+ 'category': category,
830
+ 'order_by': order_by,
831
+ 'sub_category': sub_category,
832
+ }
833
+
834
+ aiter = self.do_page_iter(params, page, self.categories_filter)
835
+ value = None
836
+ while True:
837
+ try:
838
+ page_content = await aiter.asend(value)
839
+ value = yield page_content
840
+ except StopAsyncIteration:
841
+ break
842
+
818
843
  async def month_ranking(self,
819
844
  page: int = 1,
820
845
  category: str = JmMagicConstants.CATEGORY_ALL,
@@ -103,7 +103,7 @@ class JmMagicConstants:
103
103
  APP_TOKEN_SECRET_2 = '18comicAPPContent'
104
104
  APP_DATA_SECRET = '185Hcomic3PAPP7R'
105
105
  API_DOMAIN_SERVER_SECRET = 'diosfjckwpqpdfjkvnqQjsik'
106
- APP_VERSION = '2.0.26'
106
+ APP_VERSION = '2.0.28'
107
107
 
108
108
 
109
109
  # 模块级别共用配置
@@ -154,10 +154,10 @@ class JmModuleConfig:
154
154
 
155
155
  # 移动端API域名
156
156
  DOMAIN_API_LIST = shuffled('''
157
- www.cdnaspa.club
158
- www.cdnaspa.vip
159
- www.cdnplaystation6.cc
160
- www.cdnplaystation6.vip
157
+ www.cdnhjk.net
158
+ www.cdngwc.cc
159
+ www.cdngwc.net
160
+ www.cdngwc.club
161
161
  ''')
162
162
 
163
163
  DOMAIN_API_UPDATED_LIST = None
@@ -370,37 +370,21 @@ class JmModuleConfig:
370
370
  @classmethod
371
371
  def get_html_domain_all_via_github(cls,
372
372
  postman=None,
373
- template='https://jmcmomic.github.io/go/{}.html',
374
- index_range=(300, 309)
373
+ *deprecated_args,
374
+ **deprecated_kwargs,
375
375
  ):
376
376
  """
377
- 通过禁漫官方的github号的repo获取最新的禁漫域名
378
- https://github.com/jmcmomic/jmcmomic.github.io
377
+ 已废弃:原 GitHub 仓库不再提供禁漫域名。
378
+ 为保持兼容,当前转发到 get_html_domain_all;该方法将在未来版本移除。
379
379
  """
380
- postman = postman or cls.new_postman(headers={
381
- 'authority': 'github.com',
382
- 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 '
383
- 'Safari/537.36'
384
- })
385
- domain_set = set()
386
-
387
- def fetch_domain(url):
388
- resp = postman.get(url, allow_redirects=False)
389
- text = resp.text
390
- from .jm_toolkit import JmcomicText
391
- for domain in JmcomicText.analyse_jm_pub_html(text):
392
- if domain.startswith('jm365'):
393
- continue
394
- domain_set.add(domain)
395
-
396
- from common import multi_thread_launcher
397
-
398
- multi_thread_launcher(
399
- iter_objs=[template.format(i) for i in range(*index_range)],
400
- apply_each_obj_func=fetch_domain,
380
+ import warnings
381
+ warnings.warn(
382
+ 'get_html_domain_all_via_github is deprecated because the GitHub repository no longer provides '
383
+ 'JMComic domains; use get_html_domain_all instead.',
384
+ DeprecationWarning,
385
+ stacklevel=2,
401
386
  )
402
-
403
- return domain_set
387
+ return cls.get_html_domain_all(postman)
404
388
 
405
389
  @classmethod
406
390
  def new_html_headers(cls, domain='18comic.vip'):
@@ -1,4 +1,5 @@
1
1
  from .jm_client_impl import *
2
+ from typing import Optional, Sequence, Union
2
3
 
3
4
 
4
5
  class CacheRegistry:
@@ -594,7 +595,11 @@ class JmOption:
594
595
  from .api import download_photo
595
596
  return download_photo(photo_id, self, *args, **kwargs)
596
597
 
597
- def new_jm_async_client(self, cache=None, **kwargs) -> AsyncJmcomicClient:
598
+ def new_jm_async_client(self,
599
+ cache=None,
600
+ domain_list: Optional[Union[str, Sequence[str]]] = None,
601
+ **kwargs,
602
+ ) -> AsyncJmcomicClient:
598
603
  """
599
604
  通过 Option 配置创建异步客户端。
600
605
  从 REGISTRY_ASYNC_CLIENT 注册表查找实现类(配置项: client.async_impl),
@@ -603,9 +608,15 @@ class JmOption:
603
608
  缓存:与同步版本 new_jm_client 相同,依据 client.cache 配置决定是否启用缓存
604
609
  (默认 None 即为不缓存),并通过 CacheRegistry.enable_client_cache_on_condition 下发配置。
605
610
  """
611
+ if 'domain_retry_strategy' in kwargs:
612
+ raise TypeError('Async client does not support domain_retry_strategy')
613
+
606
614
  async_impl = self.client.get('async_impl', 'async_api') or 'async_api'
607
615
  clazz = JmModuleConfig.async_client_impl_class(async_impl)
608
- client = clazz(self, **kwargs)
616
+ if domain_list is None:
617
+ client = clazz(self, **kwargs)
618
+ else:
619
+ client = clazz(self, domain_list=domain_list, **kwargs)
609
620
 
610
621
  # 启用缓存(与同步版本保持一致):默认不缓存,由 client.cache 配置决定
611
622
  cache = cache if cache is not None else self.client.cache
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jmcomic
3
- Version: 2.7.1
3
+ Version: 2.7.2
4
4
  Summary: Python API For JMComic (禁漫天堂)
5
5
  Home-page: https://github.com/hect0x7/JMComic-Crawler-Python
6
6
  Author: hect0x7
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes