From 0c8b3f4cb2545a2ab5803d0407214918fd2c7f49 Mon Sep 17 00:00:00 2001 From: hect0x7 <93357912+hect0x7@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:38:03 +0800 Subject: [PATCH] =?UTF-8?q?v1.8.0:=20=E5=A2=9E=E5=8A=A0=20album=5Fmissing?= =?UTF-8?q?=20=E6=83=85=E5=86=B5=E7=9A=84=E5=8F=8B=E5=A5=BD=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/action_workflow.yml | 2 +- .github/workflows/action_workflow_local.yml | 2 +- src/jmcomic/__init__.py | 2 +- src/jmcomic/jm_client.py | 28 +++++++++++++++++++-- src/jmcomic/jm_config.py | 9 ------- tests/test_jmcomic/test_jm_client.py | 7 ++++++ 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.github/workflows/action_workflow.yml b/.github/workflows/action_workflow.yml index 3809565..a072d5e 100644 --- a/.github/workflows/action_workflow.yml +++ b/.github/workflows/action_workflow.yml @@ -14,7 +14,7 @@ permissions: contents: read jobs: - build: + crawler: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/action_workflow_local.yml b/.github/workflows/action_workflow_local.yml index 4059bdf..7465b4f 100644 --- a/.github/workflows/action_workflow_local.yml +++ b/.github/workflows/action_workflow_local.yml @@ -20,7 +20,7 @@ permissions: contents: read jobs: - build: + crawler: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/src/jmcomic/__init__.py b/src/jmcomic/__init__.py index 36207dd..cb01b0d 100644 --- a/src/jmcomic/__init__.py +++ b/src/jmcomic/__init__.py @@ -2,6 +2,6 @@ # 被依赖方 <--- 使用方 # config <--- entity <--- toolkit <--- client <--- service <--- option -__version__ = '1.7.0' +__version__ = '1.8.0' from .api import * diff --git a/src/jmcomic/jm_client.py b/src/jmcomic/jm_client.py index ab6c650..3b3775a 100644 --- a/src/jmcomic/jm_client.py +++ b/src/jmcomic/jm_client.py @@ -141,9 +141,12 @@ class JmcomicClient(PostmanProxy): (f"响应文本=[{resp.text}]" if len(resp.text) < 50 else f'响应文本过长(len={len(resp.text)}),不打印') ) + # 图片请求,直接返回 + if is_api is False: + return resp - if is_api is True: - JmModuleConfig.check_html(resp.text.strip(), url) + # 检查请求是否成功 + self.require_resp_success_else_raise(resp, url) return resp @@ -189,6 +192,27 @@ class JmcomicClient(PostmanProxy): def get_jmcomic_url_all(self, postman=None): return JmModuleConfig.get_jmcomic_url_all(postman or self) + @classmethod + def require_resp_success_else_raise(cls, resp, url): + # 1. 是否 album_missing + if resp.url.endswith('/error/album_missing'): + raise AssertionError(f'请求的本子不存在!({url})\n' + '原因可能为:\n' + '1. id有误,检查你的本子/章节id\n' + '2. 该漫画只对登录用户可见,请配置你的cookies\n') + + # 2. 是否是特殊html页 + cls.check_special_html(resp.text.strip(), url) + + @classmethod + def check_special_html(cls, html: str, url=None): + html = html.strip() + error_msg = JmModuleConfig.JM_ERROR_RESPONSE_HTML.get(html, None) + if error_msg is None: + return + + raise AssertionError(f'{error_msg}' + f': {url}' if url is not None else '') + # 爬取策略 class FetchStrategy: diff --git a/src/jmcomic/jm_config.py b/src/jmcomic/jm_config.py index ab643a5..7d89879 100644 --- a/src/jmcomic/jm_config.py +++ b/src/jmcomic/jm_config.py @@ -102,15 +102,6 @@ class JmModuleConfig: from .jm_toolkit import JmcomicText return JmcomicText.analyse_jm_pub_html(resp.text) - @classmethod - def check_html(cls, html: str, url=None): - html = html.strip() - error_msg = cls.JM_ERROR_RESPONSE_HTML.get(html, None) - if error_msg is None: - return - - raise AssertionError(f'{error_msg}' + f': {url}' if url is not None else '') - jm_debug = JmModuleConfig.jm_debug disable_jm_debug = JmModuleConfig.disable_jm_debug diff --git a/tests/test_jmcomic/test_jm_client.py b/tests/test_jmcomic/test_jm_client.py index 2958576..fc0d18f 100644 --- a/tests/test_jmcomic/test_jm_client.py +++ b/tests/test_jmcomic/test_jm_client.py @@ -58,3 +58,10 @@ class Test_Client(JmTestConfigurable): image = photo_detail[3000] print(image.img_url) self.client.download_by_image_detail(image, workspace('3000.png')) + + def test_album_missing(self): + self.assertRaises( + AssertionError, + self.client.get_album_detail, + '332583' + )