@@ -317,10 +317,10 @@ def create_from_file(cls, filepath: str) -> 'JmOption':
317317 @classmethod
318318 def default_client_config (cls ):
319319 return {
320- 'domain' : JmModuleConfig .DOMAIN ,
320+ 'domain' : JmModuleConfig .domain () ,
321321 'meta_data' : {
322322 'cookies' : None ,
323- 'headers' : JmModuleConfig .default_headers (),
323+ 'headers' : JmModuleConfig .headers (),
324324 'allow_redirects' : True ,
325325 },
326326 'postman_type_list' : [
@@ -364,9 +364,13 @@ def build_jm_client(self) -> JmcomicClient:
364364
365365 def new_jm_client (self ) -> JmcomicClient :
366366 meta_data = self .client_config ['meta_data' ]
367+ postman_clazz = Postmans .get_impl_clazz (self .client_config .get ('postman_type' , 'cffi' ))
368+ proxies = None
369+ domain = None
370+ postman : Optional [Postman ] = None
367371
368- # 处理代理
369- def handle_proxies ( key = ' proxies' ):
372+ def decide_proxies ( key = 'proxies' ):
373+ nonlocal proxies
370374 proxies = meta_data .get (key , None )
371375
372376 # 无代理,或代理已配置好好的
@@ -381,25 +385,46 @@ def handle_proxies(key='proxies'):
381385
382386 meta_data [key ] = proxies
383387
384- # 处理 headers
388+ def decide_domain (key = 'domain' ) -> str :
389+ nonlocal domain
390+ domain = self .client_config .get (key , None )
391+ if domain is None :
392+ temp_postman = postman_clazz .create (
393+ headers = JmModuleConfig .headers (JmModuleConfig .JM_REDIRECT_URL ),
394+ proxies = proxies ,
395+ )
396+ domain = JmModuleConfig .domain (temp_postman )
397+
398+ domain = JmcomicText .parse_to_jm_domain (domain )
399+ self .client_config [key ] = domain
400+ return domain
401+
385402 def handle_headers (key = 'headers' ):
386403 headers = meta_data .get (key , None )
387404 if headers is None or (not isinstance (headers , dict )) or len (headers ) == 0 :
388- meta_data [key ] = JmModuleConfig .default_headers ()
405+ # 未配置headers,使用默认headers
406+ headers = JmModuleConfig .headers ()
389407
390- # 处理【特殊配置项】
391- handle_proxies ()
392- handle_headers ()
408+ meta_data [key ] = headers
393409
394- # 决定Postman的实现类,根据配置项 client_config.postman
395- postman_clazz = Postmans .get_impl_clazz (self .client_config .get ('postman_type' , 'cffi' ))
396- # 决定域名
397- domain = self .client_config .get ('domain' , JmModuleConfig .DOMAIN )
410+ def handle_postman ():
411+ nonlocal postman
412+ postman = postman_clazz (meta_data )
413+
414+ # 1. 决定 代理
415+ decide_proxies ()
416+ # 2. 指定 JM域名
417+ decide_domain ()
418+ # 3. 处理 headers
419+ handle_headers ()
420+ # 4. 创建 postman
421+ handle_postman ()
398422
399423 jm_debug ('创建JmcomicClient' , f'使用域名: { domain } ,使用Postman实现: { postman_clazz } ' )
400- # 创建 JmcomicClient 实例
424+
425+ # 创建 JmcomicClient 对象
401426 client = JmcomicClient (
402- postman = postman_clazz ( meta_data ) ,
427+ postman = postman ,
403428 domain = domain ,
404429 retry_times = self .client_config .get ('retry_times' , None )
405430 )
@@ -413,7 +438,7 @@ def handle_headers(key='headers'):
413438 def build_cdn_option (self , use_multi_thread_strategy = True ):
414439
415440 return CdnConfig .create (
416- cdn_domain = self .client_config .get ('domain' , JmModuleConfig .DOMAIN ),
441+ cdn_domain = self .client_config .get ('domain' , JmModuleConfig .domain () ),
417442 fetch_strategy = MultiThreadFetch if use_multi_thread_strategy else InOrderFetch ,
418443 cdn_image_suffix = None ,
419444 use_cache = self .download_use_disk_cache ,
0 commit comments