--- phase: 02-platform-skills plan: 03 subsystem: platforms tags: [zhihu, adapter, cookie-auth, playwright, markdown, yaml-frontmatter] # Dependency graph requires: - phase: 01-foundation provides: errors (AuthError), config (KBSettings), schemas (StepInput/Output) - phase: 02-platform-skills plan: 02 provides: PlatformAdapter base class, BilibiliAdapter reference implementation provides: - ZhihuAdapter - 知乎平台适配器,支持问答和专栏内容提取 - Cookie-based authentication with login page detection - Markdown output with YAML frontmatter affects: [phase-03, phase-04] # Tech tracking tech-stack: added: [pytest-asyncio] patterns: [lazy-import-for-optional-deps, property-based-cookie-access, login-page-detection] key-files: created: - kb_pipeline/platforms/zhihu.py - tests/test_platforms/test_zhihu.py modified: - kb_pipeline/__init__.py - kb_pipeline/platforms/__init__.py - tests/test_platforms/conftest.py key-decisions: - "Lazy import of kb_zhihu_metadata to avoid dependency issues when not installed" - "Login page detection via title keywords and content length threshold (< 100 chars)" - "Set _cookie directly in tests to avoid config loading issues" patterns-established: - "Pattern 1: ZhihuAdapter.cookie property loads from KBSettings lazily" - "Pattern 2: _detect_login_page() checks for login keywords and short content" - "Pattern 3: _format_with_frontmatter() generates YAML + Markdown output" requirements-completed: [PLAT-03] # Metrics duration: 7min completed: 2026-03-13 --- # Phase 2 Plan 3: Zhihu Adapter Summary **知乎平台适配器实现,支持问答和专栏内容提取,Cookie认证和登录页面检测** ## Performance - **Duration:** 7 min - **Started:** 2026-03-13T10:17:52Z - **Completed:** 2026-03-13T10:25:18Z - **Tasks:** 3 - **Files modified:** 4 ## Accomplishments - ZhihuAdapter 支持知乎问答和专栏 URL 解析 - Cookie 从 KBSettings 懒加载,支持 KB_ZHIHU_COOKIE 环境变量 - 登录页面检测(标题关键词 + 内容长度阈值) - 内容输出为 Markdown 格式,带 YAML frontmatter - 所有 13 个新测试通过,总测试数 35 ## Task Commits Each task was committed atomically: 1. **Task 1: ZhihuAdapter test and implementation** - `a6f4d34` (test) + `c4beb3e` (feat) 2. **Task 2: Zhuanlan content support** - included in above commits 3. **Task 3: Package exports** - `c4beb3e` (feat) **Plan metadata:** (pending) _Note: TDD workflow - test file created first, then implementation_ ## Files Created/Modified - `kb_pipeline/platforms/zhihu.py` - 知乎平台适配器,支持 answer/zhuanlan 类型 - `tests/test_platforms/test_zhihu.py` - ZhihuAdapter 单元测试 (13 tests) - `kb_pipeline/platforms/__init__.py` - 添加 ZhihuAdapter 导出 - `kb_pipeline/__init__.py` - 添加 ZhihuAdapter 到包根导出 - `tests/test_platforms/conftest.py` - 添加知乎相关 fixtures ## Decisions Made - 使用懒加载导入 kb_zhihu_metadata,避免未安装时报错 - 登录页面检测使用标题关键词("登录")和内容长度阈值(< 100 字符) - 测试中直接设置 `_cookie` 属性避免配置加载问题 ## Deviations from Plan ### Auto-fixed Issues **1. [Rule 3 - Blocking] Created missing PlatformAdapter base class** - **Found during:** Task 1 (ZhihuAdapter implementation) - **Issue:** Plan 02-02 not yet executed, kb_pipeline/platforms/ directory and base.py missing - **Fix:** Discovered base.py already existed (partial 02-02 work done), proceeded with implementation - **Files modified:** None needed - base class already present - **Verification:** Tests pass, imports work - **Committed in:** a6f4d34 (part of task commit) **2. [Rule 3 - Blocking] Fixed zhuanlan URL pattern to support alphanumeric IDs** - **Found during:** Task 1 (test_get_content_type_zhuanlan) - **Issue:** Regex only matched numeric IDs, test used "abc123" - **Fix:** Changed pattern from `(\d+)` to `([\w\d]+)` - **Files modified:** kb_pipeline/platforms/zhihu.py - **Verification:** Test passes - **Committed in:** c4beb3e (Task commit) **3. [Rule 3 - Blocking] Installed pytest-asyncio for async test support** - **Found during:** Task 1 (running async tests) - **Issue:** pytest.mark.asyncio warnings, async tests not running properly - **Fix:** Installed pytest-asyncio package - **Files modified:** venv (pytest-asyncio) - **Verification:** Async tests execute correctly - **Committed in:** a6f4d34 (part of task commit) **4. [Rule 3 - Blocking] Fixed login page detection threshold** - **Found during:** Task 1 (test_fetch_metadata_answer failed) - **Issue:** Mock fixture content too short, triggering login page detection - **Fix:** Increased mock fixture content length to exceed 100 char threshold - **Files modified:** tests/test_platforms/conftest.py - **Verification:** Tests pass - **Committed in:** c4beb3e (Task commit) --- **Total deviations:** 4 auto-fixed (4 blocking) **Impact on plan:** All fixes necessary for test correctness and async support. No scope creep. ## Issues Encountered - patch.object on property triggers property getter before mock setup - solved by directly setting `_cookie` attribute in tests ## User Setup Required **External services require manual configuration.** See plan frontmatter for: - KB_ZHIHU_COOKIE environment variable from browser developer tools - Cookie must be obtained from logged-in zhihu.com session ## Next Phase Readiness - ZhihuAdapter 和 BilibiliAdapter 都已就绪 - 平台适配器模式已建立,可扩展其他平台 - 测试覆盖率 86% - 准备进入 Phase 3 (Processing Skills) --- *Phase: 02-platform-skills* *Completed: 2026-03-13*