Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(web): 采集检查的裸 403 不再让用户去登录

longmeidao135b056bfebf
diff --git a/src/peach/metadata.py b/src/peach/metadata.py
index 538606302..1bb960eb1 100644
--- a/src/peach/metadata.py
+++ b/src/peach/metadata.py
@@ -118,6 +118,16 @@ def auth_wall_reason(*, status_code: int = 0, final_url: str = "",
return ""
+def reason_blames_credentials(reason: str) -> bool:
+ """这条理由有没有把成因锁死在凭据上。
+
+ 消费方要按这个分叉给出不同的下一步(去官网登录,还是先看站点要不要登录再决定
+ 换凭据还是等),判据就不能是各自到 `reason` 里找字眼——措辞改一次,找字眼的那
+ 几处会一起静默失准。
+ """
+ return bool(reason) and CREDENTIAL_ADVICE in reason
+
+
def auth_error(source: str, reason: str, *, status_code: int = 0) -> MetadataProviderError:
"""把一次鉴权失败包成 `kind="auth"` 的错误。措辞只有这一处。"""
return MetadataProviderError(
diff --git a/src/peach/web_scraping.py b/src/peach/web_scraping.py
index 6aa3d1f0a..e2032f67a 100644
--- a/src/peach/web_scraping.py
+++ b/src/peach/web_scraping.py
@@ -134,7 +134,7 @@ def w_scraping_settings(contract, body):
def w_scraping_check(contract, body):
- from .metadata import auth_wall_reason
+ from .metadata import auth_wall_reason, reason_blames_credentials
source = str(body.get("source", ""))
if source not in SOURCES:
raise ValueError("未知采集来源")
@@ -162,8 +162,13 @@ def w_scraping_check(contract, body):
with Image.open(io.BytesIO(response.body)) as image:
result["width"], result["height"] = image.size
if not result["ok"]:
- result["message"] = ("来源要求登录或验证,请在官网完成后重试。"
- if reason else "来源暂不可用,请稍后重试。")
+ # 成因锁死在凭据上才让人去登录。只剩状态码可看的 403 直接把
+ # `auth_wall_reason` 那句原样给用户:出口 IP 被封时登录是白做,
+ # 页面上再手写一份「请去登录」等于把人引去做错事。
+ if reason_blames_credentials(reason):
+ result["message"] = "来源要求登录或验证,请在官网完成登录后重试。"
+ else:
+ result["message"] = f"{reason}。" if reason else "来源暂不可用,请稍后重试。"
except Exception as exc:
result.update(ok=False, kind="unavailable",
message="连接未取得;此来源可能需要代理,请检查来源连接方式。",
diff --git a/tests/test_scraping_access.py b/tests/test_scraping_access.py
index 115c7014d..c6ddaf31e 100644
--- a/tests/test_scraping_access.py
+++ b/tests/test_scraping_access.py
@@ -91,25 +91,29 @@ class ScrapingAccessTests(unittest.TestCase):
self.assertFalse(result["results"][0]["ok"])
def test_the_check_tells_a_login_wall_apart_from_an_unreachable_source(self):
- """「要登录」和「站点挂了」在页面上要分得开:前者要人去官网,后者只要等。
+ """页面上要分得开三种结果,因为用户的下一步各不相同。
- 登录墙不一定回 403。跟完重定向落在登录页的那次响应是 200,旧判据只看状态码,
- 于是把一次「其实什么都没取到」报成可连接。
+ 登录墙不一定回 403。跟完重定向落在登录页的那次响应是 200,只看状态码的判据
+ 会把一次「其实什么都没取到」报成可连接。反过来,只剩状态码可看的 403 也不许
+ 报成登录墙:`docs/SOURCING.md` 记着那些 403 多半是出口 IP 被封,去官网登录
+ 白做一场,正确动作是等或换出口。措辞与元数据来源同源,页面不另写一份。
"""
contract = SimpleNamespace(follow_secrets_root=self.root)
login = "https://fc2cmadb.com/"
- cases = ((HttpResponse(403, {}, b"", login), "auth", False),
+ cases = ((HttpResponse(403, {}, b"", login), "auth", False, "出口 IP", "请在官网"),
(HttpResponse(200, {}, b"<html>login</html>",
- "https://fc2cmadb.com/users/sign_in"), "auth", False),
- (HttpResponse(503, {}, b"", login), "unavailable", False),
- (HttpResponse(200, {}, b"<html>ok</html>", login), "ok", True))
- for response, kind, ok in cases:
+ "https://fc2cmadb.com/users/sign_in"), "auth", False,
+ "请在官网完成登录后重试", "出口 IP"),
+ (HttpResponse(503, {}, b"", login), "unavailable", False, "稍后重试", "登录"),
+ (HttpResponse(200, {}, b"<html>ok</html>", login), "ok", True, "", "登录"))
+ for response, kind, ok, says, avoids in cases:
with self.subTest(status=response.status, url=response.url), \
patch("peach.web_scraping.SourceTransport") as factory:
factory.return_value.return_value = response
result = w_scraping_check(contract, {"source": "fc2cmadb"})["results"][0]
self.assertEqual((result["kind"], result["ok"]), (kind, ok))
- self.assertEqual("登录" in result.get("message", ""), kind == "auth")
+ self.assertIn(says, result.get("message", ""))
+ self.assertNotIn(avoids, result.get("message", ""))
def test_transport_ownership_is_explicit(self):
for owned in (True, False):