diff --git a/pipeline_opportunity/opp_common.py b/pipeline_opportunity/opp_common.py index eb12e44..a4f5689 100644 --- a/pipeline_opportunity/opp_common.py +++ b/pipeline_opportunity/opp_common.py @@ -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: