a69225ff2b
- 使用 DrissionPage 替代 Playwright - 实现 Cookie 管理和反爬绕过 - 支持回答/文章/问题三种内容类型 - 灵活配置(文件/环境变量/配置文件) - 更新文档和配置文件
319 lines
7.6 KiB
Markdown
319 lines
7.6 KiB
Markdown
# Obsidian KB Skill
|
||
|
||
多源内容知识库维护系统,整合了 bilibili-extractor,支持 Bilibili、YouTube、知乎等平台的内容聚合,智能分类,以及长视频分段分析。
|
||
|
||
## 功能特性
|
||
|
||
- **多源内容聚合**: 支持 Bilibili、YouTube、知乎等平台
|
||
- **增强型 Bilibili 提取**: 使用 Playwright 绕过反爬检测,支持字幕和弹幕提取
|
||
- **知乎内容提取**: 使用 DrissionPage + Cookie,支持回答、文章和问题,完整 Markdown 格式保留
|
||
- **智能收件箱**: AI 质量评分,自动分类
|
||
- **长视频分段分析**: 30分钟+ 视频自动分段处理
|
||
- **无字幕处理**: 智能处理无字幕视频,基于元数据生成内容
|
||
- **主题组织**: 基于标签的自动主题归类
|
||
- **Git 自动同步**: 自动同步到 Obsidian 知识库
|
||
- **CLI 命令**: 完整的命令行接口
|
||
|
||
## 快速开始
|
||
|
||
### 安装
|
||
|
||
```bash
|
||
# 克隆项目
|
||
git clone <repository-url>
|
||
cd obsidian-kb
|
||
|
||
# 安装依赖
|
||
pip install -r requirements.txt
|
||
|
||
# 配置
|
||
cp config.example.yaml config.yaml
|
||
# 编辑 config.yaml,设置 API key 等
|
||
```
|
||
|
||
### 配置
|
||
|
||
创建 `config.yaml`:
|
||
|
||
```yaml
|
||
# AI 配置
|
||
ai:
|
||
provider: openai
|
||
model: gpt-4
|
||
api_key: your-api-key-here # 或设置 OPENAI_API_KEY 环境变量
|
||
|
||
# 存储配置
|
||
storage:
|
||
inbox_dir: inbox
|
||
topics_dir: topics
|
||
data_dir: data
|
||
|
||
# 输出配置
|
||
output:
|
||
format: markdown # 输出格式
|
||
encoding: utf-8
|
||
include_timestamp: true # 包含时间戳
|
||
include_source: true # 包含源链接
|
||
|
||
# Git 同步配置
|
||
git:
|
||
enabled: true
|
||
vault_path: ~/ObsidianVault # Obsidian 知识库路径
|
||
remote: origin
|
||
branch: main
|
||
auto_commit: true
|
||
|
||
# 分段配置
|
||
chunking:
|
||
enabled: true
|
||
chunk_duration_seconds: 900 # 15 分钟分段
|
||
long_video_threshold: 1800 # 30 分钟以上视为长视频
|
||
|
||
# 数据源配置
|
||
sources:
|
||
bilibili:
|
||
enabled: true
|
||
cookie: your-bilibili-cookie # 可选,用于获取字幕
|
||
|
||
# Bilibili 提取器配置
|
||
bilibili_extraction:
|
||
use_playwright: true # 使用 Playwright 绕过检测
|
||
headless: true # 无头模式
|
||
cookie_file: '' # Cookie 文件路径 (可选)
|
||
enable_subtitle: true # 启用字幕提取
|
||
subtitle_language: zh-CN # 字幕语言
|
||
enable_danmaku: false # 启用弹幕提取
|
||
timeout_seconds: 30 # 超时时间
|
||
max_retries: 3 # 最大重试次数
|
||
```
|
||
|
||
### 首次使用
|
||
|
||
1. **获取 Bilibili Cookie (可选但推荐)**
|
||
|
||
为了更好的字幕提取体验,建议设置 Bilibili Cookie:
|
||
|
||
- 在浏览器中登录 Bilibili
|
||
- 打开开发者工具 (F12)
|
||
- 切换到 Network 标签
|
||
- 刷新页面,找到任意请求
|
||
- 复制 `Cookie` 头的值
|
||
- 将其粘贴到配置文件的 `bilibili.cookie` 字段
|
||
|
||
2. **初始化 Playwright (首次使用需要)**
|
||
|
||
```bash
|
||
# 安装 Playwright 浏览器
|
||
playwright install chromium
|
||
```
|
||
|
||
3. **测试提取器**
|
||
|
||
```bash
|
||
# 测试 Bilibili 提取器
|
||
python -c "from obsidian_kb.extractors.bilibili import BilibiliExtractor; e = BilibiliExtractor(); print(e.extract_video('https://www.bilibili.com/video/BV1xx411c7mD'))"
|
||
```
|
||
|
||
### 基本使用
|
||
|
||
```bash
|
||
# 订阅 Bilibili 收藏夹
|
||
/kb-subscribe bilibili:fav:12345678
|
||
|
||
# 查看收件箱
|
||
/kb-inbox
|
||
|
||
# 处理所有内容
|
||
/kb-process --all
|
||
|
||
# 处理指定内容
|
||
/kb-process BV1xx411c7mD
|
||
|
||
# 跳过内容
|
||
/kb-skip BV1xx411c7mD
|
||
|
||
# 查看状态
|
||
/kb-status
|
||
|
||
# 搜索知识库
|
||
/kb-search python tutorial
|
||
```
|
||
|
||
### 提取器特性
|
||
|
||
**无字幕处理:**
|
||
|
||
系统会自动处理没有字幕的视频:
|
||
- 使用视频标题、描述和标签生成内容
|
||
- AI 分析视频元数据生成摘要
|
||
- 在笔记中标注"无字幕"状态
|
||
|
||
**字幕提取:**
|
||
|
||
支持多种字幕源:
|
||
- CC 字幕 (官方字幕)
|
||
- AI 字幕 (自动生成)
|
||
- 自动选择最佳字幕源
|
||
|
||
**反爬检测:**
|
||
|
||
使用 Playwright 绕过 Bilibili 的反爬机制:
|
||
- 无头模式运行
|
||
- 自动重试机制
|
||
- 可配置超时时间
|
||
|
||
## 项目结构
|
||
|
||
```
|
||
obsidian-kb/
|
||
├── src/
|
||
│ ├── __init__.py
|
||
│ ├── models.py # 数据模型
|
||
│ ├── config.py # 配置管理
|
||
│ ├── storage.py # 存储管理
|
||
│ ├── template_renderer.py # 模板渲染
|
||
│ ├── content_chunker.py # 内容分块
|
||
│ ├── pipeline.py # 处理管道
|
||
│ ├── cli.py # 命令行接口
|
||
│ ├── git_sync.py # Git 同步
|
||
│ └── sources/
|
||
│ ├── __init__.py
|
||
│ ├── base.py # 适配器基类
|
||
│ └── bilibili.py # Bilibili 适配器
|
||
├── templates/
|
||
│ ├── light_video.md # 轻量视频模板
|
||
│ ├── tutorial_video.md # 教程视频模板
|
||
│ ├── article.md # 文章模板
|
||
│ └── topic_index.md # 主题索引模板
|
||
├── tests/ # 测试
|
||
├── config.yaml # 配置文件
|
||
└── README.md
|
||
```
|
||
|
||
## 模板系统
|
||
|
||
根据内容类型和时长自动选择模板:
|
||
|
||
- **轻量视频** (`< 30分钟`): `light_video.md`
|
||
- **教程视频** (`≥ 30分钟`): `tutorial_video.md` - 包含详细分段、前置知识、常见坑点
|
||
- **文章**: `article.md`
|
||
|
||
## 知乎提取
|
||
|
||
支持知乎回答、文章和问题的提取。
|
||
|
||
**支持的内容类型:**
|
||
- ✅ 知乎回答:`https://www.zhihu.com/question/xxx/answer/yyy`
|
||
- ✅ 知乎文章:`https://zhuanlan.zhihu.com/p/xxx`
|
||
- ✅ 知乎问题:`https://www.zhihu.com/question/xxx`
|
||
|
||
**技术方案:**
|
||
- 使用 DrissionPage 浏览器自动化工具
|
||
- 通过 Cookie 验证登录状态
|
||
- 完整 Markdown 格式输出
|
||
|
||
**优势:**
|
||
- 稳定可靠,绕过反爬虫保护
|
||
- 支持无头模式,适合服务器环境
|
||
- Cookie 管理灵活(文件/环境变量/配置文件)
|
||
- 内存占用低,每次提取独立启动浏览器
|
||
|
||
### 输出格式
|
||
|
||
提取的内容包含以下信息:
|
||
|
||
```python
|
||
{
|
||
'type': 'answer' | 'article' | 'question',
|
||
'title': '标题',
|
||
'author': '作者',
|
||
'content': 'HTML 内容',
|
||
'content_markdown': 'Markdown 内容', # 完整格式
|
||
'publish_time': '发布时间',
|
||
'vote_up': 点赞数,
|
||
'url': '原始 URL',
|
||
'content_id': '内容 ID'
|
||
}
|
||
```
|
||
|
||
### Markdown 格式保留
|
||
|
||
知乎提取器保留完整的 Markdown 格式:
|
||
|
||
- ✅ 标题(H1-H6)
|
||
- ✅ 代码块(支持语法高亮)
|
||
- ✅ 表格
|
||
- ✅ 引用块
|
||
- ✅ 列表(有序/无序)
|
||
- ✅ 粗体、斜体
|
||
- ✅ 链接
|
||
|
||
### 配置
|
||
|
||
在 `config.yaml` 中配置知乎提取:
|
||
|
||
```yaml
|
||
zhihu:
|
||
enabled: true
|
||
use_drissionpage: true # 使用 DrissionPage(推荐)
|
||
cookie_file: ~/.obsidian-kb/zhihu_cookies.json # Cookie 文件路径
|
||
cookie_str: "" # 可选:直接在配置中填入 Cookie JSON
|
||
timeout: 30
|
||
headless: true
|
||
```
|
||
|
||
**Cookie 获取方式:**
|
||
1. 在有浏览器的机器上登录知乎
|
||
2. 打开开发者工具 (F12)
|
||
3. 进入 Application → Cookies → https://www.zhihu.com
|
||
4. 导出所有 Cookie 为 JSON 文件
|
||
5. 上传到服务器指定路径
|
||
|
||
**Cookie 文件格式:**
|
||
```json
|
||
[
|
||
{
|
||
"name": "z_c0",
|
||
"value": "xxx",
|
||
"domain": ".zhihu.com",
|
||
"path": "/",
|
||
"expiry": 1234567890,
|
||
"secure": true,
|
||
"httpOnly": false
|
||
}
|
||
]
|
||
```
|
||
|
||
**Cookie 加载优先级:**
|
||
1. 配置文件中的 `cookie_str`(最高优先级)
|
||
2. 环境变量 `ZHIHU_COOKIE`
|
||
3. Cookie 文件(最低优先级)
|
||
|
||
---
|
||
|
||
## 长视频分段策略
|
||
|
||
| 时长 | 策略 | 分段数 |
|
||
|------|------|--------|
|
||
| < 10分钟 | 直接处理 | 1 |
|
||
| 10-30分钟 | 截断处理 | 1 |
|
||
| 30-60分钟 | 两段式 | 2 |
|
||
| 60分钟+ | 多段式 | 每15分钟一段 |
|
||
|
||
## 开发
|
||
|
||
```bash
|
||
# 运行测试
|
||
pytest tests/
|
||
|
||
# 运行特定测试
|
||
pytest tests/test_models.py
|
||
|
||
# 覆盖率报告
|
||
pytest --cov=src tests/
|
||
```
|
||
|
||
## 许可证
|
||
|
||
MIT License
|