fix(opp): infer source name from source_url domain - crawler records carry no source code, only source_url; registry domain suffix match (search.ccgp.gov.cn -> ccgp)

This commit is contained in:
yumoqing 2026-09-08 18:59:52 +08:00
parent 8c032d7be0
commit 1040eb0bd9

View File

@ -52,14 +52,40 @@ def source_display(code):
return (code or "", "")
# 域名 → 来源 code从 source_url 反推来源名称用;爬虫平台记录不带 source code
# 只有 source_url故按域名匹配。主域匹配去 www. 后按注册表主页域名后缀比对)
def _registry_hosts():
hosts = {}
for code, (_name, home) in SOURCE_REGISTRY.items():
h = home.split("//", 1)[-1].split("/", 1)[0].lower()
h = h[4:] if h.startswith("www.") else h
hosts[h] = code
return hosts
def infer_source_code(source_url):
"""source_url → 来源 code域名后缀匹配注册表主页。匹配不到返回 ''"""
url = (source_url or "").strip()
if not url or "//" not in url:
return ""
host = url.split("//", 1)[-1].split("/", 1)[0].split(":", 1)[0].lower()
host = host[4:] if host.startswith("www.") else host
for rh, code in _registry_hosts().items():
if host == rh or host.endswith("." + rh):
return code
return ""
def source_button_widget(code, source_url=""):
"""来源名称按钮:显示来源名称,点击新窗口直跳来源 url。
名称解析顺序记录 source code source_url 域名反推 空则采集来源页
跳转目标优先记录级 source_url采集来源页最精确缺失回退来源主页
两者皆无返回 None不渲染按钮code 缺失但有 source_url 时名称显示
采集来源页
两者皆无返回 None不渲染按钮
"""
code = (code or "").strip()
if not code:
code = infer_source_code(source_url)
name, home = source_display(code)
target = (source_url or "").strip() or home
if not target: