@@ -11,20 +11,19 @@ class AbstractJmClient(
1111
1212 def __init__ (self ,
1313 postman : Postman ,
14- retry_times : int ,
15- domain = None ,
16- fallback_domain_list = None ,
14+ domain_list : List [str ],
15+ retry_times = 0 ,
1716 ):
17+ """
18+ 创建JM客户端
19+
20+ :param postman: 负责实现HTTP请求的对象,持有cookies、headers、proxies等信息
21+ :param domain_list: 禁漫域名
22+ :param retry_times: 重试次数
23+ """
1824 super ().__init__ (postman )
1925 self .retry_times = retry_times
20-
21- if fallback_domain_list is None :
22- fallback_domain_list = []
23-
24- if domain is not None :
25- fallback_domain_list .insert (0 , domain )
26-
27- self .domain_list = fallback_domain_list
26+ self .domain_list = domain_list
2827 self .CLIENT_CACHE = None
2928 self .enable_cache ()
3029 self .after_init ()
@@ -98,6 +97,9 @@ def request_with_retry(self,
9897 resp = request (url , ** kwargs )
9998 return judge (resp )
10099 except Exception as e :
100+ if self .retry_times == 0 :
101+ raise e
102+
101103 self .before_retry (e , kwargs , retry_count , url )
102104
103105 if retry_count < self .retry_times :
@@ -190,7 +192,7 @@ def append_params_to_url(self, url, params):
190192
191193 # noinspection PyMethodMayBeStatic
192194 def decode (self , url : str ):
193- if not JmModuleConfig .decode_url_when_logging or '/search/' not in url :
195+ if not JmModuleConfig .flag_decode_url_when_logging or '/search/' not in url :
194196 return url
195197
196198 from urllib .parse import unquote
@@ -371,7 +373,7 @@ def album_comment(self,
371373 status = 'true' ,
372374 comment_id = None ,
373375 ** kwargs ,
374- ) -> JmAcResp :
376+ ) -> JmAlbumCommentResp :
375377 data = {
376378 'video_id' : video_id ,
377379 'comment' : comment ,
@@ -396,7 +398,7 @@ def album_comment(self,
396398 data = data ,
397399 )
398400
399- ret = JmAcResp (resp )
401+ ret = JmAlbumCommentResp (resp )
400402 jm_log ('album.comment' , f'{ video_id } : [{ comment } ] ← ({ ret .model ().cid } )' )
401403
402404 return ret
@@ -754,15 +756,15 @@ def decide_headers_and_ts(self, kwargs, url):
754756 ts = time_stamp ()
755757 token , tokenparam = JmCryptoTool .token_and_tokenparam (ts , secret = JmMagicConstants .APP_TOKEN_SECRET_2 )
756758
757- elif JmModuleConfig .use_fix_timestamp :
759+ elif JmModuleConfig .flag_use_fix_timestamp :
758760 ts , token , tokenparam = JmModuleConfig .get_fix_ts_token_tokenparam ()
759761
760762 else :
761763 ts = time_stamp ()
762764 token , tokenparam = JmCryptoTool .token_and_tokenparam (ts )
763765
764- # 计算token,tokenparam
765- headers = kwargs .get ('headers' , JmMagicConstants .APP_HEADERS_TEMPLATE .copy () )
766+ # 设置headers
767+ headers = kwargs .get ('headers' , None ) or JmMagicConstants .APP_HEADERS_TEMPLATE .copy ()
766768 headers .update ({
767769 'token' : token ,
768770 'tokenparam' : tokenparam ,
@@ -786,7 +788,7 @@ def require_resp_success(cls, resp: JmApiResp, orig_req_url: str):
786788
787789 def after_init (self ):
788790 # 保证拥有cookies,因为移动端要求必须携带cookies,否则会直接跳转同一本子【禁漫娘】
789- if JmModuleConfig .api_client_require_cookies :
791+ if JmModuleConfig .flag_api_client_require_cookies :
790792 self .ensure_have_cookies ()
791793
792794 from threading import Lock
@@ -800,7 +802,13 @@ def ensure_have_cookies(self):
800802 if self .get_meta_data ('cookies' ):
801803 return
802804
803- self ['cookies' ] = JmModuleConfig .get_cookies (self )
805+ self ['cookies' ] = self .get_cookies ()
806+
807+ @field_cache ("APP_COOKIES" , obj = JmModuleConfig )
808+ def get_cookies (self ):
809+ resp = self .setting ()
810+ cookies = dict (resp .resp .cookies )
811+ return cookies
804812
805813
806814class FutureClientProxy (JmcomicClient ):
@@ -823,19 +831,21 @@ class FutureClientProxy(JmcomicClient):
823831 'set_cache_dict' , 'get_cache_dict' , 'set_domain_list' , ]
824832
825833 class FutureWrapper :
826- def __init__ (self , future ):
834+ def __init__ (self , future , after_done_callback ):
827835 from concurrent .futures import Future
828836 future : Future
829837 self .future = future
830838 self .done = False
831839 self ._result = None
840+ self .after_done_callback = after_done_callback
832841
833842 def result (self ):
834843 if not self .done :
835844 result = self .future .result ()
836845 self ._result = result
837846 self .done = True
838847 self .future = None # help gc
848+ self .after_done_callback ()
839849
840850 return self ._result
841851
@@ -871,7 +881,9 @@ def get_future(self, cache_key, task):
871881 if cache_key in self .future_dict :
872882 return self .future_dict [cache_key ]
873883
874- future = self .FutureWrapper (self .executors .submit (task ))
884+ future = self .FutureWrapper (self .executors .submit (task ),
885+ after_done_callback = lambda : self .future_dict .pop (cache_key , None )
886+ )
875887 self .future_dict [cache_key ] = future
876888 return future
877889
0 commit comments