@@ -52,7 +52,7 @@ def request_with_retry(self,
5252 jm_debug (
5353 f'请求重试' ,
5454 ', ' .join ([
55- f'次数: [{ retry_count + 1 } /{ self .retry_times } ]' ,
55+ f'次数: [{ retry_count } /{ self .retry_times } ]' ,
5656 f'域名: [{ domain } ({ domain_index } /{ len (self .domain_list )} )]' ,
5757 f'路径: [{ url } ]' ,
5858 f'参数: [{ kwargs if "login" not in url else "#login_form#" } ]'
@@ -194,56 +194,82 @@ def get_jm_html(self, url, require_200=True, **kwargs):
194194
195195 if require_200 is True and resp .status_code != 200 :
196196 write_text ('./resp.html' , resp .text )
197- self .check_special_http_code (resp .status_code , url )
198- raise AssertionError (f"请求失败,"
199- f"响应状态码为{ resp .status_code } ,"
200- f"URL=[{ resp .url } ],"
201- + (f"响应文本=[{ resp .text } ]" if len (resp .text ) < 50 else
202- f'响应文本过长(len={ len (resp .text )} ),不打印' )
203- )
197+ self .check_special_http_code (resp )
198+ self .raise_request_error (resp )
199+
204200 # 检查请求是否成功
205- self .require_resp_success_else_raise (resp )
201+ self .require_resp_success_else_raise (resp , url )
206202
207203 return resp
208204
205+ @classmethod
206+ def raise_request_error (cls , resp , msg : Optional [str ] = None ):
207+ """
208+ 请求如果失败,统一由该方法抛出异常
209+ """
210+ if msg is None :
211+ msg = f"请求失败," \
212+ f"响应状态码为{ resp .status_code } ," \
213+ f"URL=[{ resp .url } ]," \
214+ + (f"响应文本=[{ resp .text } ]" if len (resp .text ) < 200 else
215+ f'响应文本过长(len={ len (resp .text )} ),不打印'
216+ )
217+ raise AssertionError (msg )
218+
209219 def get_jm_image (self , img_url ) -> JmImageResp :
210220 return JmImageResp (self .get (img_url ))
211221
212222 @classmethod
213- def require_resp_success_else_raise (cls , resp ):
214- # 1. 是否 album_missing
215- resp_url = resp .url
216- if resp_url .endswith ('/error/album_missing' ):
217- raise AssertionError (f'请求的本子不存在!({ resp_url } )\n '
218- '原因可能为:\n '
219- '1. id有误,检查你的本子/章节id\n '
220- '2. 该漫画只对登录用户可见,请配置你的cookies\n ' )
221-
222- # 2. 是否是错误html页
223- cls .check_error_html (resp .text .strip (), resp_url )
223+ def require_resp_success_else_raise (cls , resp , req_url ):
224+ # 1. 检查是否 album_missing
225+ error_album_missing = '/error/album_missing'
226+ if resp .url .endswith (error_album_missing ) and not req_url .endswith (error_album_missing ):
227+ cls .raise_request_error (
228+ resp ,
229+ f'请求的本子不存在!({ req_url } )\n '
230+ '原因可能为:\n '
231+ '1. id有误,检查你的本子/章节id\n '
232+ '2. 该漫画只对登录用户可见,请配置你的cookies\n '
233+ )
234+
235+ # 2. 是否是特殊的内容
236+ cls .check_special_text (resp )
224237
225238 @classmethod
226- def check_error_html (cls , html : str , url = None ):
227- html = html .strip ()
228- error_msg = JmModuleConfig .JM_ERROR_RESPONSE_HTML .get (html , None )
229- if error_msg is None :
239+ def check_special_text (cls , resp ):
240+ html = resp .text
241+ url = resp .url
242+
243+ if len (html ) > 500 :
230244 return
231245
232- write_text ('./resp.html' , html )
233- raise AssertionError (f'{ error_msg } '
234- + (f': { url } ' if url is not None else '' ))
246+ for content , reason in JmModuleConfig .JM_ERROR_RESPONSE_TEXT .items ():
247+ if content not in html :
248+ continue
249+
250+ write_text ('./resp.html' , html )
251+ cls .raise_request_error (
252+ resp ,
253+ f'{ reason } '
254+ + (f': { url } ' if url is not None else '' )
255+ )
235256
236257 @classmethod
237- def check_special_http_code (cls , code , url = None ):
258+ def check_special_http_code (cls , resp ):
259+ code = resp .status_code
260+ url = resp .url
261+
238262 error_msg = JmModuleConfig .JM_ERROR_STATUS_CODE .get (int (code ), None )
239263 if error_msg is None :
240264 return
241265
242- raise AssertionError (f"请求失败,"
243- f"响应状态码为{ code } ,"
244- f'原因为: [{ error_msg } ], '
245- + (f'URL=[{ url } ]' if url is not None else '' )
246- )
266+ cls .raise_request_error (
267+ resp ,
268+ f"请求失败,"
269+ f"响应状态码为{ code } ,"
270+ f'原因为: [{ error_msg } ], '
271+ + (f'URL=[{ url } ]' if url is not None else '' )
272+ )
247273
248274
249275class JmApiClient (AbstractJmClient ):
0 commit comments