@@ -24,6 +24,10 @@ def __init__(self,
24
24
fallback_domain_list .insert (0 , domain )
25
25
26
26
self .domain_list = fallback_domain_list
27
+ self .after_init ()
28
+
29
+ def after_init (self ):
30
+ pass
27
31
28
32
def get (self , url , ** kwargs ):
29
33
return self .request_with_retry (self .postman .get , url , ** kwargs )
@@ -104,31 +108,35 @@ def before_retry(self, e, kwargs, retry_count, url):
104
108
jm_debug ('req.error' , str (e ))
105
109
106
110
def enable_cache (self , debug = False ):
111
+ if self .is_cache_enabled ():
112
+ return
113
+
107
114
def wrap_func_cache (func_name , cache_dict_name ):
108
- import common
109
- if common .VERSION > '0.4.8' :
110
- if hasattr (self , cache_dict_name ):
111
- return
115
+ if hasattr (self , cache_dict_name ):
116
+ return
112
117
113
- cache = common .cache
114
- cache_dict = {}
115
- cache_hit_msg = (f'【缓存命中】{ cache_dict_name } ' + '→ [{}]' ) if debug is True else None
116
- cache_miss_msg = (f'【缓存缺失】{ cache_dict_name } ' + '← [{}]' ) if debug is True else None
117
- cache = cache (
118
- cache_dict = cache_dict ,
119
- cache_hit_msg = cache_hit_msg ,
120
- cache_miss_msg = cache_miss_msg ,
121
- )
122
- setattr (self , cache_dict_name , cache_dict )
118
+ if sys .version_info > (3 , 9 ):
119
+ import functools
120
+ cache = functools .cache
123
121
else :
124
- if sys .version_info < (3 , 9 ):
122
+ import common
123
+ if common .VERSION > '0.4.8' :
124
+ cache = common .cache
125
+ cache_dict = {}
126
+ cache_hit_msg = (f'【缓存命中】{ cache_dict_name } ' + '→ [{}]' ) if debug is True else None
127
+ cache_miss_msg = (f'【缓存缺失】{ cache_dict_name } ' + '← [{}]' ) if debug is True else None
128
+ cache = cache (
129
+ cache_dict = cache_dict ,
130
+ cache_hit_msg = cache_hit_msg ,
131
+ cache_miss_msg = cache_miss_msg ,
132
+ )
133
+ setattr (self , cache_dict_name , cache_dict )
134
+ else :
125
135
ExceptionTool .raises ('不支持启用JmcomicClient缓存。\n '
126
136
'请更新python版本到3.9以上,'
127
137
'或更新commonX: `pip install commonX --upgrade`' )
128
- import functools
129
- cache = functools .cache
138
+ return
130
139
131
- # 重载本对象的方法
132
140
func = getattr (self , func_name )
133
141
wrap_func = cache (func )
134
142
@@ -399,14 +407,6 @@ class JmApiClient(AbstractJmClient):
399
407
API_CHAPTER = '/chapter'
400
408
API_SCRAMBLE = '/chapter_view_template'
401
409
402
- def __init__ (self ,
403
- postman : Postman ,
404
- retry_times : int ,
405
- domain = None ,
406
- fallback_domain_list = None ,
407
- ):
408
- super ().__init__ (postman , retry_times , domain , fallback_domain_list )
409
-
410
410
def search (self ,
411
411
search_query : str ,
412
412
page : int ,
@@ -445,19 +445,34 @@ def get_album_detail(self, album_id) -> JmAlbumDetail:
445
445
)
446
446
447
447
def get_photo_detail (self , photo_id , fetch_album = True ) -> JmPhotoDetail :
448
- photo_id = JmcomicText .parse_to_photo_id (photo_id )
449
448
photo : JmPhotoDetail = self .fetch_detail_entity (photo_id ,
450
449
JmModuleConfig .photo_class (),
451
450
)
452
- photo . scramble_id = self .get_scramble_id (photo . photo_id )
451
+ self .fetch_photo_additional_field (photo , fetch_album )
453
452
return photo
454
453
454
+ def get_scramble_id (self , photo_id ):
455
+ """
456
+ 带有缓存的fetch_scramble_id,缓存位于JmModuleConfig.SCRAMBLE_CACHE
457
+ """
458
+ cache = JmModuleConfig .SCRAMBLE_CACHE
459
+ if photo_id in cache :
460
+ return cache [photo_id ]
461
+
462
+ scramble_id = self .fetch_scramble_id (photo_id )
463
+ cache [photo_id ] = scramble_id
464
+ return scramble_id
465
+
455
466
def fetch_detail_entity (self , apid , clazz , ** kwargs ):
467
+ """
468
+ 请求实体类
469
+ """
470
+ apid = JmcomicText .parse_to_album_id (apid )
456
471
url = self .API_ALBUM if issubclass (clazz , JmAlbumDetail ) else self .API_CHAPTER
457
472
resp = self .get_decode (
458
473
url ,
459
474
params = {
460
- 'id' : JmcomicText . parse_to_album_id ( apid ) ,
475
+ 'id' : apid ,
461
476
** kwargs ,
462
477
}
463
478
)
@@ -466,11 +481,11 @@ def fetch_detail_entity(self, apid, clazz, **kwargs):
466
481
467
482
return JmApiAdaptTool .parse_entity (resp .res_data , clazz )
468
483
469
- def get_scramble_id (self , photo_id ):
470
- cache = JmModuleConfig . SCRAMBLE_CACHE
471
- if photo_id in cache :
472
- return cache [ photo_id ]
473
-
484
+ def fetch_scramble_id (self , photo_id ):
485
+ """
486
+ 请求scramble_id
487
+ """
488
+ photo_id : str = JmcomicText . parse_to_photo_id ( photo_id )
474
489
resp = self .get_decode (
475
490
self .API_SCRAMBLE ,
476
491
params = {
@@ -487,11 +502,43 @@ def get_scramble_id(self, photo_id):
487
502
scramble_id = match [1 ]
488
503
else :
489
504
jm_debug ('api.scramble' , '未从响应中匹配到scramble_id,返回默认值220980' )
490
- scramble_id = 220980
505
+ scramble_id = ' 220980'
491
506
492
- cache [photo_id ] = scramble_id
493
507
return scramble_id
494
508
509
+ def fetch_photo_additional_field (self , photo : JmPhotoDetail , fetch_album : bool ):
510
+ """
511
+ 获取章节的额外信息
512
+ 1. scramble_id
513
+ 2. album
514
+
515
+ 这里的难点是,是否要采用异步的方式并发请求。
516
+ """
517
+ aid = photo .album_id
518
+ pid = photo .photo_id
519
+ scramble_cache = JmModuleConfig .SCRAMBLE_CACHE
520
+
521
+ if fetch_album is False and pid in scramble_cache :
522
+ # 不用发请求,直接返回
523
+ photo .scramble_id = scramble_cache [pid ]
524
+ return
525
+
526
+ if fetch_album is True and pid not in scramble_cache :
527
+ # 要发起两个请求,这里实现很简易,直接排队请求
528
+ # todo: 改进实现
529
+ # 1. 直接开两个线程跑
530
+ # 2. 开两个线程,但是开之前检查重复性
531
+ # 3. 线程池,也要检查重复性
532
+ # 23做法要改不止一处地方
533
+ photo .from_album = self .get_scramble_id (pid )
534
+ photo .scramble_id = self .get_album_detail (aid )
535
+ return
536
+
537
+ if fetch_album is True :
538
+ photo .from_album = self .get_album_detail (aid )
539
+ else :
540
+ photo .scramble_id = self .get_scramble_id (pid )
541
+
495
542
def get_decode (self , url , ** kwargs ) -> JmApiResp :
496
543
# set headers
497
544
headers , key_ts = self .headers_key_ts
0 commit comments