Initial commit

This commit is contained in:
Alex committed 2026-11-02 22:54:14 +08:00
commit 57fc3525eb
7311 files changed
+1291398

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
"""
app/core/extractors - 内容提取器模块
提供不同的页面内容提取策略。
"""
from app.core.extractors.base import BaseExtractor
from app.core.extractors.registry import ExtractorRegistry
from app.core.extractors.deep_mode import DeepBrowserExtractor
from app.core.extractors.dom_mode import DOMDirectExtractor
from app.core.extractors.hybrid_mode import HybridExtractor
from app.core.extractors.image_extractor import ImageExtractor, image_extractor # 🆕 新增
from app.core.extractors.media_extractor import MediaExtractor, media_extractor
# 自动注册所有提取器
ExtractorRegistry.register_class(DeepBrowserExtractor)
ExtractorRegistry.register_class(DOMDirectExtractor)
ExtractorRegistry.register_class(HybridExtractor)
__all__ = [
'BaseExtractor',
'ExtractorRegistry',
'DeepBrowserExtractor',
'DOMDirectExtractor',
'HybridExtractor',
'ImageExtractor',
'image_extractor',
'MediaExtractor',
'media_extractor',
]
Binary file not shown.
+99
View File
@@ -0,0 +1,99 @@
"""
app/core/extractors/base.py - 提取器抽象基类
定义内容提取的标准接口,支持策略模式扩展不同的提取算法。
"""
from abc import ABC, abstractmethod
from typing import Any
class BaseExtractor(ABC):
"""
内容提取器抽象基类
定义从页面元素中提取内容的标准接口。
不同的提取策略(深度提取、简单提取、API提取等)
可以通过继承此类来实现。
"""
# ============ 抽象方法(子类必须实现)============
@abstractmethod
def extract_text(self, element) -> str:
"""
从元素中提取纯文本
Args:
element: 页面元素对象
Returns:
提取的文本内容
"""
pass
@abstractmethod
def get_anchor(self, element) -> str:
"""
获取元素的唯一标识(锚点)
用于在页面变化时追踪同一个元素。
Args:
element: 页面元素对象
Returns:
锚点字符串,用于唯一标识该元素
"""
pass
@abstractmethod
def find_content_node(self, element) -> Any:
"""
定位内容子节点
从容器元素中找到实际包含内容的子节点,
避免读取到按钮、头像等无关元素。
Args:
element: 父元素对象
Returns:
内容子节点,如果未找到则返回原元素
"""
pass
# ============ 元数据接口(子类可选覆盖)============
@classmethod
def get_id(cls) -> str:
"""
获取提取器唯一标识符
Returns:
提取器 ID(默认使用类名转小写)
"""
return cls.__name__.lower()
@classmethod
def get_name(cls) -> str:
"""
获取提取器显示名称
Returns:
用于 UI 展示的名称
"""
return cls.__name__
@classmethod
def get_description(cls) -> str:
"""
获取提取器描述信息
Returns:
算法描述(用于帮助用户选择)
"""
return "未提供描述"
__all__ = ['BaseExtractor']
+534
View File
@@ -0,0 +1,534 @@
"""
app/core/extractors/deep_mode.py - 深度内容提取器 (v2.2)
v2.2 修复:
- find_content_node 正确识别容器元素,不再误匹配段落
- 支持 DeepSeek ds-markdown 结构
"""
from typing import Any, Optional, Dict, List
from app.core.extractors.base import BaseExtractor
from app.core.extractors.image_extractor import image_extractor
from app.core.driver import as_element
class DeepBrowserExtractor(BaseExtractor):
"""深度浏览器内容提取器"""
@classmethod
def get_id(cls) -> str:
return "deep_mode_v1"
@classmethod
def get_name(cls) -> str:
return "深度模式 (JS注入)"
@classmethod
def get_description(cls) -> str:
return "通过 JavaScript 注入深度提取内容,支持 LaTeX、代码块、Shadow DOM"
DEEP_EXTRACT_JS = r"""
return (function () {
function normNewline(s){ return (s||'').replace(/\r\n/g,'\n').replace(/\r/g,'\n'); }
function isEl(n){ return n && n.nodeType === 1; }
function rstripNewlines(s){ return normNewline(s).replace(/\n+$/g, ''); }
function hasAncestorTag(el, tagNameLower){
var p = el;
while (p) {
if (p.nodeType === 1 && (p.tagName||'').toLowerCase() === tagNameLower) return true;
p = p.parentElement;
}
return false;
}
function hasAncestorClass(el, classPattern){
var p = el;
while (p) {
if (p.nodeType === 1) {
var cls = (p.className && typeof p.className === 'string') ? p.className : '';
if (cls.indexOf(classPattern) >= 0) return true;
}
p = p.parentElement;
}
return false;
}
function ignorableEl(el){
if (!isEl(el)) return false;
var tag = (el.tagName||'').toLowerCase();
if (tag === 'button' || tag === 'svg') return true;
if (tag === 'mat-expansion-panel-header') return true;
var cls = (el.className && typeof el.className === 'string') ? el.className : '';
// DeepSeek: 代码块横幅区域
if (cls.indexOf('md-code-block-banner-wrap') >= 0) return true;
if (cls.indexOf('code-info-button-text') >= 0) return true;
// GPT: 代码块工具栏
if (cls.indexOf('sticky') >= 0 && el.querySelector && el.querySelector('button')) return true;
if (cls.indexOf('bg-token-sidebar-surface-primary') >= 0 && el.querySelector && el.querySelector('button')) return true;
// 通用 UI 元素
if (cls.indexOf('actions-container') >= 0) return true;
if (cls.indexOf('projected-actions-wrapper') >= 0) return true;
if (cls.indexOf('turn-footer') >= 0) return true;
if (cls.indexOf('mat-expansion-indicator') >= 0) return true;
if (cls.indexOf('material-symbols') >= 0) return true;
if (cls.indexOf('material-icons') >= 0) return true;
if (cls.indexOf('mat-icon') >= 0) return true;
// Gemini: 代码块按钮区
if (cls.indexOf('buttons') >= 0 && el.closest && el.closest('code-block')) return true;
var aria = (el.getAttribute && (el.getAttribute('aria-label')||'')) || '';
if (/download|copy|expand|collapse|edit|rerun|open options|good response|bad response|复制/i.test(aria)) return true;
return false;
}
// ========== LaTeX 提取 ==========
function extractKatexAnnotation(el){
try {
var ann = el.querySelector && el.querySelector('annotation[encoding="application/x-tex"]');
if (ann && ann.textContent) return ann.textContent;
} catch(e) {}
return '';
}
function extractDataMath(el){
try {
var dm = el.getAttribute && el.getAttribute('data-math');
if (dm) return dm;
} catch(e) {}
return '';
}
// ========== 代码块提取 ==========
function extractDeepSeekCodeBlock(el){
var lang = '';
var code = '';
try {
var langSpan = el.querySelector('.d813de27');
if (langSpan) lang = (langSpan.textContent || '').trim();
var pre = el.querySelector('pre');
if (pre) code = normNewline(pre.textContent || '');
} catch(e) {}
return { lang: lang, code: code };
}
function extractGeminiCodeBlock(el){
var lang = '';
var code = '';
try {
var langEl = el.querySelector('.code-block-decoration span');
if (langEl) lang = (langEl.textContent || '').trim();
var codeEl = el.querySelector('code[data-test-id="code-content"]');
if (codeEl) code = normNewline(codeEl.textContent || '');
} catch(e) {}
return { lang: lang, code: code };
}
function extractGPTCodeBlock(el){
var lang = '';
var code = '';
try {
var codeEl = el.querySelector('code[class*="language-"]');
if (codeEl) {
var clsStr = codeEl.className || '';
var match = clsStr.match(/language-(\S+)/);
if (match) lang = match[1];
code = normNewline(codeEl.textContent || '');
} else {
codeEl = el.querySelector('code');
if (codeEl) code = normNewline(codeEl.textContent || '');
}
if (!lang) {
var headerDiv = el.previousElementSibling;
if (headerDiv && headerDiv.textContent) {
var t = (headerDiv.textContent || '').trim();
if (t && t.length < 30 && !/复制|copy/i.test(t)) lang = t;
}
}
} catch(e) {}
return { lang: lang, code: code };
}
function extractMsCodeBlock(el){
var best = '';
try {
var nodes = el.querySelectorAll ? el.querySelectorAll('pre code') : [];
for (var i = 0; i < nodes.length; i++) {
var c = nodes[i];
if (!c) continue;
if (hasAncestorTag(c, 'ms-katex')) continue;
var ccls = (c.className && typeof c.className === 'string') ? c.className : '';
if (ccls.indexOf('rendered') >= 0) continue;
try {
if (c.querySelector && c.querySelector('.katex')) continue;
} catch(e2){}
var t = normNewline(c.textContent || '');
if (t && t.replace(/\s+/g,'').length > best.replace(/\s+/g,'').length) best = t;
}
} catch(e) {}
return best;
}
// ========== 主遍历逻辑 ==========
function walk(node, out){
if (!node) return;
if (isEl(node) && node.shadowRoot) {
walk(node.shadowRoot, out);
}
if (node.nodeType === 11) {
var kidsF = node.childNodes ? Array.prototype.slice.call(node.childNodes) : [];
for (var iF = 0; iF < kidsF.length; iF++) walk(kidsF[iF], out);
return;
}
if (node.nodeType === 3) {
out.push(node.nodeValue || '');
return;
}
if (!isEl(node)) return;
var el = node;
if (ignorableEl(el)) return;
var tag = (el.tagName||'').toLowerCase();
var cls = (el.className && typeof el.className === 'string') ? el.className : '';
// ===== DeepSeek 代码块 =====
if (cls.indexOf('md-code-block') >= 0 && tag === 'div') {
var ds = extractDeepSeekCodeBlock(el);
if (ds.code && ds.code.replace(/\s+/g,'').length > 0) {
out.push('\n```' + (ds.lang || '') + '\n');
out.push(rstripNewlines(ds.code));
out.push('\n```\n');
}
return;
}
// ===== Gemini 代码块 =====
if (tag === 'code-block') {
var gm = extractGeminiCodeBlock(el);
if (gm.code && gm.code.replace(/\s+/g,'').length > 0) {
out.push('\n```' + (gm.lang ? gm.lang.toLowerCase() : '') + '\n');
out.push(rstripNewlines(gm.code));
out.push('\n```\n');
}
return;
}
// ===== GPT 代码块 =====
if (tag === 'pre' && el.querySelector && el.querySelector('code[class*="language-"]')) {
var gpt = extractGPTCodeBlock(el);
if (gpt.code && gpt.code.replace(/\s+/g,'').length > 0) {
out.push('\n```' + (gpt.lang || '') + '\n');
out.push(rstripNewlines(gpt.code));
out.push('\n```\n');
}
return;
}
// ===== ms-katex (LaTeX) =====
if (tag === 'ms-katex') {
var tex = extractKatexAnnotation(el);
if (tex) {
var inline = (cls.indexOf('inline') >= 0);
out.push(inline ? (' $' + tex + '$ ') : ('\n$$\n' + tex + '\n$$\n'));
}
return;
}
// ===== Gemini LaTeX (span[data-math]) =====
var dataMath = extractDataMath(el);
if (dataMath) {
var isBlockMath = cls.indexOf('math-block') >= 0 || cls.indexOf('math-display') >= 0;
out.push(isBlockMath ? ('\n$$\n' + dataMath + '\n$$\n') : (' $' + dataMath + '$ '));
return;
}
// ===== GPT/通用 .katex =====
if (cls.indexOf('katex') >= 0) {
var tex2 = extractKatexAnnotation(el);
if (tex2) out.push(' $' + tex2 + '$ ');
return;
}
// ===== ms-code-block =====
if (tag === 'ms-code-block') {
var msCode = extractMsCodeBlock(el);
if (msCode && msCode.replace(/\s+/g,'').length > 0) {
out.push('\n```\n');
out.push(rstripNewlines(msCode));
out.push('\n```\n');
}
return;
}
// ===== 通用 pre/code 回退 =====
if (tag === 'pre' && !hasAncestorTag(el, 'ms-katex') && !hasAncestorClass(el, 'md-code-block')) {
var tcode = normNewline(el.textContent || '');
if (tcode && tcode.replace(/\s+/g,'').length > 0) {
out.push('\n```\n');
out.push(rstripNewlines(tcode));
out.push('\n```\n');
}
return;
}
// ===== Markdown 标题 h1-h6 =====
if (/^h[1-6]$/.test(tag)) {
var level = parseInt(tag.charAt(1), 10);
var hashes = '';
for (var h = 0; h < level; h++) hashes += '#';
out.push('\n' + hashes + ' ');
var hKids = el.childNodes ? Array.prototype.slice.call(el.childNodes) : [];
for (var hk = 0; hk < hKids.length; hk++) walk(hKids[hk], out);
out.push('\n');
return;
}
// ===== Markdown 加粗 strong/b =====
if (tag === 'strong' || tag === 'b') {
out.push('**');
var bKids = el.childNodes ? Array.prototype.slice.call(el.childNodes) : [];
for (var bk = 0; bk < bKids.length; bk++) walk(bKids[bk], out);
out.push('**');
return;
}
// ===== Markdown 斜体 em/i =====
if (tag === 'em' || tag === 'i') {
out.push('*');
var iKids = el.childNodes ? Array.prototype.slice.call(el.childNodes) : [];
for (var ik = 0; ik < iKids.length; ik++) walk(iKids[ik], out);
out.push('*');
return;
}
// ===== Markdown 水平线 hr =====
if (tag === 'hr') {
out.push('\n---\n');
return;
}
// ===== 通用块级元素 =====
var kids = el.childNodes ? Array.prototype.slice.call(el.childNodes) : [];
if (kids.length) {
var isBlock = ('p div section article li ul ol table tr'.indexOf(tag) >= 0);
if (isBlock) out.push('\n');
for (var k = 0; k < kids.length; k++) walk(kids[k], out);
if (isBlock) out.push('\n');
}
}
// ========== 入口 ==========
try {
var out = [];
walk(this, out);
var s = normNewline(out.join(''));
s = s.replace(/[ \t]+\n/g, '\n');
s = s.replace(/\n{3,}/g, '\n\n');
return s.trim();
} catch (e) {
try {
return normNewline(this.textContent || this.innerText || '').trim();
} catch (e2) {
return '';
}
}
}).call(this);
"""
# 注意:移除了 'css:[class*="markdown"]' 这种容易误匹配的选择器
CONTENT_CHILD_SELECTORS = [
'css:.markdown-body', # GitHub style
'css:.prose', # Tailwind
'css:.turn-content', # Gemini
'css:.response-content-markdown',
'css:.message-content',
]
CONTENT_CHILD_SELECTOR_COMBINED = (
'css:.markdown-body, .prose, .turn-content, '
'.response-content-markdown, .message-content'
)
def find_content_node(self, element) -> Any:
"""
定位内容子节点
v2.2 修复:正确识别容器元素,不再误匹配段落
"""
if not element:
return element
# ===== 关键修复:检查元素本身是否已经是目标容器 =====
try:
ele_class = element.attr('class') or ""
ele_tag = element.tag.lower() if hasattr(element, 'tag') else ""
# 已知的内容容器类名(直接返回,不再向下查找)
container_patterns = [
'ds-markdown', # DeepSeek
'markdown-body', # GitHub style
'prose', # Tailwind
'message-content', # 通用
'response-content', # 通用
]
for pattern in container_patterns:
if pattern in ele_class:
# 确保不是段落子元素
if 'paragraph' not in ele_class and 'html' not in ele_class:
return element
# 如果是 div/article 且类名包含 markdown/prose,直接返回
if ele_tag in ('div', 'article', 'section'):
if ('markdown' in ele_class or 'prose' in ele_class) and 'paragraph' not in ele_class:
return element
except Exception:
pass
# ===== 向下查找(只在必要时执行)=====
try:
children = as_element(element).find_all(self.CONTENT_CHILD_SELECTOR_COMBINED, timeout=0.08)
if children and not isinstance(children, list):
children = [children]
except Exception:
children = []
for child in (children or [])[:8]:
try:
if not child:
continue
child_class = child.attr('class') or ""
if 'paragraph' in child_class:
continue
child_text = as_element(child).run_js("return this.textContent || this.innerText || ''")
if child_text and len(str(child_text).strip()) > 0:
return child
except Exception:
pass
return element
def extract_text(self, element) -> str:
"""从元素中提取纯文本"""
if not element:
return ""
target_ele = self.find_content_node(element)
try:
text = as_element(target_ele).run_js(self.DEEP_EXTRACT_JS)
if text and str(text).strip():
return str(text).replace('\r\n', '\n').replace('\r', '\n')
except Exception:
pass
try:
t = target_ele.text if hasattr(target_ele, 'text') else ""
return (t or "").replace('\r\n', '\n').replace('\r', '\n')
except Exception:
return ""
def get_anchor(self, element) -> str:
"""获取元素锚点"""
if not element:
return ""
try:
stable_attrs = ['data-message-id', 'data-turn-id', 'data-testid', 'id']
for attr in stable_attrs:
try:
val = element.attr(attr)
if val:
return f"{attr}={val}"
except Exception:
pass
tag = element.tag if hasattr(element, 'tag') else 'unknown'
cls = ""
try:
cls = element.attr('class') or ""
except Exception:
pass
classes = cls.split()[:3]
class_part = f"|cls={'.'.join(classes)}" if classes else ""
index_part = ""
try:
index = as_element(element).run_js("""
const parent = this.parentElement;
if (!parent) return -1;
const siblings = Array.from(parent.children);
return siblings.indexOf(this);
""")
if index is not None and index >= 0:
index_part = f"|idx={index}"
except Exception:
pass
return f"tag:{tag}{class_part}{index_part}"
except Exception:
return ""
def extract_images(
self,
element,
config: Optional[Dict] = None,
container_selector_fallback: Optional[str] = None
) -> List[Dict]:
"""
从元素中提取图片(Phase A 新增)
Args:
element: 页面元素对象
config: 图片提取配置(ImageExtractionConfig)
container_selector_fallback: 容器选择器回退值
Returns:
图片数据列表,每项符合 ImageData 格式
"""
if not element:
return []
return image_extractor.extract(
element,
config=config,
container_selector_fallback=container_selector_fallback
)
def extract_media(
self,
element,
config: Optional[Dict] = None,
container_selector_fallback: Optional[str] = None
) -> List[Dict]:
"""从元素中提取多模态资源。"""
if not element:
return []
from app.core.extractors.media_extractor import media_extractor
return media_extractor.extract(
element,
config=config,
container_selector_fallback=container_selector_fallback
)
__all__ = ['DeepBrowserExtractor']
+181
View File
@@ -0,0 +1,181 @@
"""
app/core/extractors/dom_mode.py - DOM 直接提取器
轻量级提取器,直接读取元素的 textContent/innerText。
适用于结构简单、无复杂格式的页面。
"""
from typing import Any
from app.core.extractors.base import BaseExtractor
from app.core.driver import as_element
class DOMDirectExtractor(BaseExtractor):
"""
DOM 直接提取器
特点:
- 直接读取 textContent/innerText
- 无 JavaScript 注入(性能高)
- 不处理 LaTeX/代码块
- 适合简单页面
"""
# ============ 元数据 ============
@classmethod
def get_id(cls) -> str:
return "dom_direct"
@classmethod
def get_name(cls) -> str:
return "DOM 直接提取"
@classmethod
def get_description(cls) -> str:
return "直接读取 DOM textContent(轻量级,不支持复杂格式)"
CONTENT_CHILD_SELECTOR_COMBINED = 'css:.markdown, .prose, [class*="content"]'
# ============ 提取逻辑 ============
def extract_text(self, element) -> str:
"""
直接提取元素的文本内容
Args:
element: 页面元素对象
Returns:
提取的文本
"""
if not element:
return ""
try:
# 优先使用 .text 属性
if hasattr(element, 'text'):
text = element.text
if text:
return self._normalize_text(text)
# 回退:使用 textContent
text = as_element(element).run_js("return this.textContent || this.innerText || ''")
return self._normalize_text(str(text)) if text else ""
except Exception:
return ""
def get_anchor(self, element) -> str:
"""
获取元素锚点(复用 deep_mode 的逻辑)
Args:
element: 页面元素对象
Returns:
锚点字符串
"""
if not element:
return ""
try:
# 1. 优先使用稳定的 ID 类属性
stable_attrs = ['data-message-id', 'data-turn-id', 'data-testid', 'id']
for attr in stable_attrs:
try:
val = element.attr(attr)
if val:
return f"{attr}={val}"
except Exception:
pass
# 2. 回退:tag + class
tag = element.tag if hasattr(element, 'tag') else 'unknown'
cls = ""
try:
cls = element.attr('class') or ""
except Exception:
pass
classes = cls.split()[:3]
class_part = f"|cls={'.'.join(classes)}" if classes else ""
# 3. 加入 DOM 位置
index_part = ""
try:
index = as_element(element).run_js("""
const parent = this.parentElement;
if (!parent) return -1;
const siblings = Array.from(parent.children);
return siblings.indexOf(this);
""")
if index is not None and index >= 0:
index_part = f"|idx={index}"
except Exception:
pass
return f"tag:{tag}{class_part}{index_part}"
except Exception:
return ""
def find_content_node(self, element) -> Any:
"""
定位内容子节点
简化版:直接使用常见选择器
Args:
element: 父元素
Returns:
内容子节点或原元素
"""
if not element:
return element
try:
children = as_element(element).find_all(self.CONTENT_CHILD_SELECTOR_COMBINED, timeout=0.08)
if children and not isinstance(children, list):
children = [children]
except Exception:
children = []
for child in (children or [])[:8]:
try:
if not child:
continue
text = as_element(child).run_js("return this.textContent || ''")
if text and len(str(text).strip()) > 0:
return child
except Exception:
pass
return element
# ============ 辅助方法 ============
def _normalize_text(self, text: str) -> str:
"""
标准化文本
- 统一换行符
- 去除首尾空白
Args:
text: 原始文本
Returns:
标准化后的文本
"""
if not text:
return ""
text = text.replace('\r\n', '\n').replace('\r', '\n')
return text.strip()
__all__ = ['DOMDirectExtractor']
+396
View File
@@ -0,0 +1,396 @@
"""
app/core/extractors/hybrid_mode.py - 混合智能提取器
结合 DOM 直接提取和深度 JS 注入的优点:
- 先快速检测页面特征(是否有代码块、公式等)
- 简单内容 → 轻量提取(性能优先)
- 复杂内容 → 深度提取(准确优先)
"""
from typing import Any
from app.core.extractors.base import BaseExtractor
from app.core.driver import as_element
class HybridExtractor(BaseExtractor):
"""
混合智能提取器
策略:
1. 快速检测元素是否包含复杂内容(代码块、LaTeX、Shadow DOM)
2. 简单内容:直接读取 textContent(快速,低开销)
3. 复杂内容:JS 深度遍历提取(准确,支持格式化)
优势:
- 兼顾性能与准确性
- 自动适应不同类型的页面
- 失败时自动降级
"""
# ============ 元数据 ============
@classmethod
def get_id(cls) -> str:
return "hybrid_mode"
@classmethod
def get_name(cls) -> str:
return "混合智能模式"
@classmethod
def get_description(cls) -> str:
return "自动检测内容复杂度,智能选择最优提取策略(推荐)"
# ============ 检测脚本 ============
# 轻量级复杂度检测 JS
DETECT_COMPLEXITY_JS = """
return (function() {
var result = {
hasCode: false,
hasLatex: false,
hasShadow: false,
hasTable: false,
textLength: 0
};
try {
// 检测代码块
result.hasCode = !!(
this.querySelector('pre') ||
this.querySelector('code') ||
this.querySelector('ms-code-block')
);
// 检测 LaTeX 公式
result.hasLatex = !!(
this.querySelector('.katex') ||
this.querySelector('ms-katex') ||
this.querySelector('.MathJax') ||
this.querySelector('math')
);
// 检测表格
result.hasTable = !!(this.querySelector('table'));
// 检测 Shadow DOM(需要深度遍历)
var checkShadow = function(el, depth) {
if (depth > 5) return false;
if (el.shadowRoot) return true;
var children = el.children || [];
for (var i = 0; i < Math.min(children.length, 20); i++) {
if (checkShadow(children[i], depth + 1)) return true;
}
return false;
};
result.hasShadow = checkShadow(this, 0);
// 获取文本长度(用于判断是否值得深度提取)
result.textLength = (this.textContent || '').length;
} catch(e) {
result.error = e.message;
}
// 综合判断
result.isComplex = result.hasCode || result.hasLatex || result.hasShadow || result.hasTable;
result.needsDeep = result.isComplex && result.textLength > 50;
return result;
}).call(this);
"""
# 深度提取 JS(来自 deep_mode.py,此处简化展示关键部分)
DEEP_EXTRACT_JS = r"""
return (function () {
function normNewline(s){ return (s||'').replace(/\r\n/g,'\n').replace(/\r/g,'\n'); }
function isEl(n){ return n && n.nodeType === 1; }
function hasAncestorTag(el, tagNameLower){
var p = el;
while (p) {
if (p.nodeType === 1 && (p.tagName||'').toLowerCase() === tagNameLower) return true;
p = p.parentElement;
}
return false;
}
function rstripNewlines(s){
s = normNewline(s);
return s.replace(/\n+$/g, '');
}
function ignorableEl(el){
if (!isEl(el)) return false;
var tag = (el.tagName||'').toLowerCase();
if (tag === 'button' || tag === 'svg') return true;
if (tag === 'mat-expansion-panel-header') return true;
var cls = (el.className && typeof el.className === 'string') ? el.className : '';
if (cls.indexOf('actions-container') >= 0) return true;
if (cls.indexOf('turn-footer') >= 0) return true;
if (cls.indexOf('material-symbols') >= 0) return true;
if (cls.indexOf('material-icons') >= 0) return true;
if (cls.indexOf('mat-icon') >= 0) return true;
var aria = (el.getAttribute && (el.getAttribute('aria-label')||'')) || '';
if (/download|copy|expand|collapse|edit/i.test(aria)) return true;
return false;
}
function extractKatex(el){
try {
var ann = el.querySelector && el.querySelector('annotation[encoding="application/x-tex"]');
if (ann && ann.textContent) return ann.textContent;
} catch(e) {}
return '';
}
function walk(node, out){
if (!node) return;
if (isEl(node) && node.shadowRoot) walk(node.shadowRoot, out);
if (node.nodeType === 11) {
var kidsF = node.childNodes ? Array.prototype.slice.call(node.childNodes) : [];
for (var iF = 0; iF < kidsF.length; iF++) walk(kidsF[iF], out);
return;
}
if (node.nodeType === 3) {
out.push(node.nodeValue || '');
return;
}
if (!isEl(node)) return;
var el = node;
if (ignorableEl(el)) return;
var tag = (el.tagName||'').toLowerCase();
var cls = (el.className && typeof el.className === 'string') ? el.className : '';
// KaTeX 处理
if (tag === 'ms-katex' || cls.indexOf('katex') >= 0) {
var tex = extractKatex(el);
if (tex) {
var inline = (cls.indexOf('inline') >= 0);
out.push(inline ? (' $' + tex + '$ ') : ('\n$$\n' + tex + '\n$$\n'));
}
return;
}
// 代码块处理
if ((tag === 'pre' || tag === 'code') && !hasAncestorTag(el, 'ms-katex')) {
var tcode = normNewline(el.textContent || '');
if (tcode && tcode.replace(/\s+/g,'').length > 0) {
out.push('\n```\n' + rstripNewlines(tcode) + '\n```\n');
}
return;
}
// 递归子节点
var kids = el.childNodes ? Array.prototype.slice.call(el.childNodes) : [];
if (kids.length) {
var isBlock = 'p div section li ul ol table tr h1 h2 h3 h4 h5 h6 br'.indexOf(tag) >= 0;
if (isBlock) out.push('\n');
for (var k = 0; k < kids.length; k++) walk(kids[k], out);
if (isBlock) out.push('\n');
}
}
try {
var out = [];
walk(this, out);
var s = normNewline(out.join(''));
s = s.replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n');
return s.trim();
} catch (e) {
return normNewline(this.textContent || '').trim();
}
}).call(this);
"""
# 内容子节点选择器
CONTENT_SELECTORS = [
'css:.markdown',
'css:.prose',
'css:.response-content-markdown',
'css:.ds-markdown',
'css:.message-content',
'css:[class*="markdown"]',
'css:[class*="content"]',
]
# ============ 核心方法 ============
def extract_text(self, element) -> str:
"""
智能提取文本
流程:
1. 定位内容节点
2. 检测复杂度
3. 选择提取策略
4. 失败时自动降级
"""
if not element:
return ""
# 1. 定位内容子节点
target = self.find_content_node(element)
# 2. 检测复杂度
complexity = self._detect_complexity(target)
# 3. 根据复杂度选择策略
if complexity.get('needsDeep', False):
# 复杂内容:使用深度提取
text = self._deep_extract(target)
if text:
return text
# 简单内容或深度提取失败:使用轻量提取
return self._simple_extract(target)
def get_anchor(self, element) -> str:
"""
获取元素锚点
复用 deep_mode 的稳定锚点策略
"""
if not element:
return ""
try:
# 1. 优先使用稳定的 ID 类属性
stable_attrs = ['data-message-id', 'data-turn-id', 'data-testid', 'id']
for attr in stable_attrs:
try:
val = element.attr(attr)
if val:
return f"{attr}={val}"
except Exception:
pass
# 2. 回退:tag + class + DOM 位置
tag = element.tag if hasattr(element, 'tag') else 'unknown'
cls = ""
try:
cls = element.attr('class') or ""
except Exception:
pass
classes = cls.split()[:3]
class_part = f"|cls={'.'.join(classes)}" if classes else ""
# 3. DOM 位置
index_part = ""
try:
index = as_element(element).run_js("""
const parent = this.parentElement;
if (!parent) return -1;
return Array.from(parent.children).indexOf(this);
""")
if index is not None and index >= 0:
index_part = f"|idx={index}"
except Exception:
pass
return f"tag:{tag}{class_part}{index_part}"
except Exception:
return ""
def find_content_node(self, element) -> Any:
"""
定位内容子节点
尝试找到实际包含内容的子元素,避免读取杂项
"""
if not element:
return element
for selector in self.CONTENT_SELECTORS:
try:
child = as_element(element).find(selector, timeout=0.05)
if child:
# 验证有实际内容
text = as_element(child).run_js("return (this.textContent || '').trim()")
if text and len(str(text)) > 10:
return child
except Exception:
pass
return element
# ============ 私有方法 ============
def _detect_complexity(self, element) -> dict:
"""
检测元素复杂度
Returns:
{
'hasCode': bool,
'hasLatex': bool,
'hasShadow': bool,
'hasTable': bool,
'isComplex': bool,
'needsDeep': bool
}
"""
try:
result = as_element(element).run_js(self.DETECT_COMPLEXITY_JS)
if isinstance(result, dict):
return result
except Exception:
pass
# 检测失败时默认简单模式
return {'isComplex': False, 'needsDeep': False}
def _simple_extract(self, element) -> str:
"""
简单提取(高性能)
直接读取 textContent,不做复杂处理
"""
try:
# 优先使用 .text 属性
if hasattr(element, 'text') and element.text:
return self._normalize(element.text)
# 回退:JS 读取
text = as_element(element).run_js("return this.textContent || this.innerText || ''")
return self._normalize(str(text)) if text else ""
except Exception:
return ""
def _deep_extract(self, element) -> str:
"""
深度提取(高准确度)
使用 JS 注入遍历 DOM 树
"""
try:
text = as_element(element).run_js(self.DEEP_EXTRACT_JS)
if text and str(text).strip():
return self._normalize(str(text))
except Exception:
pass
return ""
def _normalize(self, text: str) -> str:
"""标准化文本"""
if not text:
return ""
text = text.replace('\r\n', '\n').replace('\r', '\n')
return text.strip()
__all__ = ['HybridExtractor']
+779
View File
@@ -0,0 +1,779 @@
"""
app/core/extractors/image_extractor.py - 图片内容提取器 (v1.0)
Phase A 实现:
- 支持 http(s)、data:、blob:、相对路径四种来源
- blob 自动转换为 data_uri(在浏览器上下文中完成)
- 支持大小限制和去重
- 所有异常均被捕获,保证不影响文本提取
安全规范:
- 不记录 data_uri 完整内容到日志
- 仅记录 mime、byte_size、前缀片段
"""
import hashlib
from typing import List, Optional, Any, Dict
from datetime import datetime, timezone
from app.core.config import get_logger
from app.models.schemas import normalize_modalities_config
from app.core.driver import as_element
logger = get_logger("IMG_EXT")
def get_default_image_extraction_config() -> Dict:
"""获取默认的多模态提取配置"""
return {
"enabled": False,
"modalities": normalize_modalities_config({}),
"selector": "img",
"audio_selector": "audio, audio source",
"video_selector": "video, video source",
"container_selector": None,
"final_target_strategy": "container",
"allow_container_fallback": True,
"force_postprocess": False,
"debounce_seconds": 2.0,
"wait_for_load": True,
"load_timeout_seconds": 5.0,
"download_blobs": True,
"max_size_mb": 10,
"canvas_export_mime": "image/jpeg",
"canvas_export_quality": 0.88,
"src_allow_patterns": [],
"mode": "all",
"audio_capture_enabled": True,
"audio_capture_mute_playback": True,
"audio_capture_preload_enabled": True,
"audio_capture_reload_before_workflow": False,
"audio_capture_preserve_graph": True,
"audio_capture_terminal_settle_seconds": 0.35,
"audio_trigger_selector": "",
"audio_trigger_labels": ["朗读", "语音朗读", "收听", "read aloud", "listen", "tts", "voice"],
"audio_capture_max_wait_seconds": 12.0,
"audio_capture_min_wait_seconds": 2.0,
"audio_capture_hard_max_wait_seconds": 45.0,
"audio_capture_estimated_chars_per_second": 4.8,
"audio_capture_wait_padding_seconds": 1.2,
"audio_network_capture": {
"enabled": False,
"timeout_seconds": 2.5,
"transport": "page_websocket_probe",
"url_patterns": ["voicegenie", "speech", "audio", "tts"],
"extractor": "voicegenie_ogg_pages",
"settle_seconds": 0.35,
},
"audio_capture_poll_seconds": 0.25,
"audio_capture_silence_seconds": 1.2,
"audio_capture_activity_threshold": 0.006,
"audio_capture_activity_silence_seconds": 0.65,
}
KNOWN_1X1_PLACEHOLDER_DATA_URIS = {
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==",
"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==",
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
}
class ImageExtractor:
"""
图片提取器
从页面元素中提取图片信息,支持四种来源:
1. http(s) URL:直接返回 kind="url"
2. data: URI:直接返回 kind="data_uri"
3. blob: URL:转换为 data_uri 后返回
4. 相对路径:补全为绝对 URL 后返回
使用方式:
extractor = ImageExtractor()
images = extractor.extract(element, config)
"""
# ============ 核心 JS 代码 ============
# 功能:收集图片 + 可选等待加载 + blob 转 data_uri
# 执行方式:async IIFE,使用 .call(this, opts)
# 返回格式:{ images: [...], warnings: [...] }
EXTRACT_IMAGES_JS = r"""
return (async function(opts) {
const {
selector = "img",
containerSelector = null,
waitForLoad = true,
loadTimeoutMs = 5000,
downloadBlobs = true,
maxBytes = 10485760,
srcAllowPatterns = [],
mode = "all",
allowContainerFallback = true,
canvasExportMime = "image/jpeg",
canvasExportQuality = 0.88,
requestBaselineToken = "",
requestBaselineProperty = "",
requestBaselineExcludeExistingNodes = false
} = opts || {};
const windowedPlaceholderAttribute = "data-uwa-image-window-placeholder";
const windowedOriginalSourceAttribute = "data-uwa-image-window-original-src";
const knownPlaceholders = new Set([
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==",
"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==",
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
]);
const isKnownPlaceholderSrc = (src) => {
const trimmed = String(src || "").trim();
return knownPlaceholders.has(trimmed);
};
// ===== 1. 确定根元素 =====
// 优先只在传入元素(当前回复节点)内查找;只有查不到时才回退到容器/整页。
const primaryRoots = [];
const fallbackRoots = [];
const pushRoot = (bucket, value) => {
if (!value) return;
const nodeType = Number(value.nodeType || 0);
if (nodeType !== 1 && nodeType !== 9) return;
if (!bucket.includes(value)) {
bucket.push(value);
}
};
if (this && (this.nodeType === 1 || this.nodeType === 9)) {
pushRoot(primaryRoots, this);
}
if (containerSelector) {
try {
const scopedRoots = Array.from(document.querySelectorAll(containerSelector));
for (const scopedRoot of scopedRoots) {
pushRoot(fallbackRoots, scopedRoot);
}
} catch {}
} else {
pushRoot(fallbackRoots, document);
}
if (primaryRoots.length === 0 && fallbackRoots.length === 0) {
return { images: [], warnings: ["container_not_found"] };
}
// ===== 2. 查找所有图片元素 =====
const isUserUploadNode = (el) => {
if (!el || !(el instanceof Element)) return false;
try {
return !!el.closest(
'user-query, user-query-content, user-query-file-preview, user-query-file-carousel, ' +
'[data-message-author="user"], [data-role="user"], [data-testid*="user-message"], ' +
'.user-query-container, .query-file-preview'
);
} catch {
return false;
}
};
const collectNodes = (roots) => {
const scopedNodes = [];
const seenNodes = new Set();
const pushNode = (value) => {
if (!(value instanceof Element)) return;
if (seenNodes.has(value)) return;
if (isUserUploadNode(value)) return;
seenNodes.add(value);
scopedNodes.push(value);
};
for (const root of roots) {
try {
if (root instanceof Element && typeof root.matches === "function" && root.matches(selector)) {
pushNode(root);
}
} catch {}
try {
const rootNodes = root.querySelectorAll ? Array.from(root.querySelectorAll(selector)) : [];
for (const node of rootNodes) {
pushNode(node);
}
} catch {}
}
return scopedNodes;
};
let scopeUsed = "primary";
let nodes = collectNodes(primaryRoots);
if (nodes.length === 0 && allowContainerFallback) {
scopeUsed = "fallback";
nodes = collectNodes(fallbackRoots);
}
if (nodes.length === 0) {
return { images: [], warnings: [], scope: scopeUsed, nodeCount: 0 };
}
// ===== 辅助函数 =====
// 获取图片源(优先 currentSrc)
function pickSrc(img) {
if (!img) return "";
const cs = typeof img.currentSrc === "string" ? img.currentSrc : "";
if (cs && cs.trim()) return cs.trim();
const s = typeof img.src === "string" ? img.src : "";
if (s && s.trim()) return s.trim();
return "";
}
// A request baseline is attached to every image node that existed immediately
// before submit. Upload previews often fill or replace their src asynchronously
// after the click, so a changed src does not make a pre-submit node generated output.
if (requestBaselineToken && requestBaselineProperty) {
nodes = nodes.filter((img) => {
const baseline = img[requestBaselineProperty];
if (!baseline || String(baseline.token || "") !== String(requestBaselineToken)) {
return true;
}
if (requestBaselineExcludeExistingNodes) return false;
return pickSrc(img) !== String(baseline.reference || "");
});
if (nodes.length === 0) {
return { images: [], warnings: [], scope: scopeUsed, nodeCount: 0 };
}
}
// 判断图片是否加载完成
function isLoaded(img) {
return !!(img.complete && img.naturalWidth > 0);
}
// ===== 3. 可选:等待图片加载 =====
if (waitForLoad) {
const deadline = Date.now() + loadTimeoutMs;
while (Date.now() < deadline) {
const allOk = nodes.every(img => {
const s = pickSrc(img);
if (!s) return true; // 无 src 不阻塞
if (s.startsWith("data:")) return true; // data uri 无需加载
return isLoaded(img); // 检查 complete
});
if (allOk) break;
await new Promise(r => setTimeout(r, 100));
}
}
// ===== 4. 收集基础信息 =====
function stripRuntimeFields(item) {
const { _node, ...rest } = item || {};
return rest;
}
function estimateByteSizeFromDataUri(dataUri) {
try {
const base64 = String(dataUri || "").split(",", 2)[1] || "";
const paddingMatch = base64.match(/=*$/);
const padding = paddingMatch ? paddingMatch[0].length : 0;
return Math.max(0, Math.floor(base64.length * 3 / 4) - padding);
} catch {
return null;
}
}
async function blobToDataUri(blob) {
const dataUri = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onerror = () => reject(new Error("read_failed"));
reader.onload = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
return {
dataUri: dataUri,
mime: blob.type || null,
byteSize: Number(blob.size) || null,
source: "blob"
};
}
async function fetchBlobWithLimit(src, limitBytes) {
const response = await fetch(src);
const contentType = String(response && response.headers && response.headers.get("content-type") || "");
const contentLength = Number(response && response.headers && response.headers.get("content-length") || 0) || 0;
if (limitBytes && contentLength && contentLength > limitBytes) {
throw new Error("blob_too_large:" + contentLength);
}
if (response && response.body && typeof response.body.getReader === "function") {
const reader = response.body.getReader();
const chunks = [];
let total = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (!value) continue;
total += Number(value.byteLength || value.length || 0) || 0;
if (limitBytes && total > limitBytes) {
try { await reader.cancel(); } catch {}
throw new Error("blob_too_large:" + total);
}
chunks.push(value);
}
return new Blob(chunks, { type: contentType });
}
const blob = await response.blob();
if (limitBytes && blob.size > limitBytes) {
throw new Error("blob_too_large:" + blob.size);
}
return blob;
}
async function imageElementToDataUri(img) {
if (!img) {
throw new Error("img_missing");
}
const isPlaceholderNode = isKnownPlaceholderSrc(pickSrc(img)) ||
img.getAttribute(windowedPlaceholderAttribute) === "1";
if (isPlaceholderNode) {
throw new Error("img_is_placeholder");
}
if (typeof img.decode === "function") {
try {
await img.decode();
} catch {}
}
const width = img.naturalWidth || img.width || 0;
const height = img.naturalHeight || img.height || 0;
if (!img.complete || width <= 0 || height <= 0) {
throw new Error("img_not_ready");
}
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
if (!ctx) {
throw new Error("canvas_ctx_unavailable");
}
ctx.drawImage(img, 0, 0, width, height);
let dataUri;
const exportMime = String(canvasExportMime || "image/jpeg");
const exportQuality = Number(canvasExportQuality || 0.88);
try {
dataUri = canvas.toDataURL(exportMime, exportQuality);
} catch (e) {
throw new Error("canvas_export_failed:" + String(e).slice(0, 80));
}
const byteSize = estimateByteSizeFromDataUri(dataUri);
if (maxBytes && byteSize && byteSize > maxBytes) {
throw new Error("blob_too_large:" + byteSize);
}
return {
dataUri: dataUri,
mime: exportMime,
byteSize: byteSize,
source: "blob_canvas"
};
}
const warnings = [];
const isMicroIcon = (img) => {
if (!img) return false;
try {
const nw = Number(img.naturalWidth || 0);
const nh = Number(img.naturalHeight || 0);
if (nw > 0 && nh > 0 && (nw < 48 && nh < 48)) {
const rect = img.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0 && (rect.width < 48 && rect.height < 48)) {
return true;
}
}
} catch {}
return false;
};
const isHiddenNode = (img) => {
if (!img) return false;
try {
const style = window.getComputedStyle(img);
if (style.display === "none" || style.visibility === "hidden" || Number(style.opacity || "1") <= 0.01) {
return true;
}
} catch {}
return false;
};
let items = nodes.map((img, i) => {
const rawSrc = pickSrc(img);
const isPlaceholderSrc = isKnownPlaceholderSrc(rawSrc);
const windowedPlaceholder = img.getAttribute(windowedPlaceholderAttribute) === "1" || isPlaceholderSrc;
const rawOriginalSource = windowedPlaceholder
? String(img.getAttribute(windowedOriginalSourceAttribute) || "").trim()
: "";
const windowedOriginalSource = isKnownPlaceholderSrc(rawOriginalSource) ? "" : rawOriginalSource;
const src = windowedOriginalSource || rawSrc;
const microIcon = isMicroIcon(img);
const hidden = isHiddenNode(img);
return {
_node: img,
index: i,
src: src,
alt: img.getAttribute("alt") || "",
width: img.naturalWidth || img.width || null,
height: img.naturalHeight || img.height || null,
complete: !!img.complete,
naturalWidth: img.naturalWidth || 0,
_windowed_placeholder: windowedPlaceholder && !windowedOriginalSource,
_is_micro_icon: microIcon,
_is_hidden: hidden,
_source: windowedOriginalSource ? "image_window_original_src" : ""
};
}).filter(x => x.src); // 过滤无 src 的
const windowedPlaceholderCount = items.filter(item => item._windowed_placeholder).length;
if (windowedPlaceholderCount > 0) {
warnings.push("windowed_placeholders_skipped:" + windowedPlaceholderCount);
items = items.filter(item => !item._windowed_placeholder);
}
// 过滤微小图标与隐藏占位节点(若有有效大图候选时)
const validGenerations = items.filter(item => !item._is_micro_icon && !item._is_hidden);
if (validGenerations.length > 0) {
items = validGenerations;
}
const beforeAllowFilterCount = items.length;
const beforeAllowFilterSamples = items.slice(0, 5).map((item) => {
const s = String(item.src || "");
return s.length > 80 ? (s.slice(0, 80) + "...") : s;
});
// ===== 4.5 可选:按 src 白名单过滤 =====
const allowRegexes = Array.isArray(srcAllowPatterns)
? srcAllowPatterns
.map((pattern) => {
try {
const text = String(pattern || "").trim();
if (!text) return null;
return new RegExp(text, "i");
} catch {
return null;
}
})
.filter(Boolean)
: [];
if (allowRegexes.length > 0) {
items = items.filter((item) => allowRegexes.some((regex) => regex.test(item.src)));
}
if (allowRegexes.length > 0 && beforeAllowFilterCount > 0 && items.length === 0) {
warnings.push(
"all_filtered_by_src_allow_patterns:" + JSON.stringify({
count: beforeAllowFilterCount,
sample_srcs: beforeAllowFilterSamples,
patterns: allowRegexes.map((regex) => String(regex))
})
);
}
// ===== 5. 按模式筛选 =====
if (mode === "first") items = items.slice(0, 1);
if (mode === "last") items = items.slice(-1);
// ===== 6. 相对路径补全 =====
items = items.map(x => {
const s = x.src;
if (s.startsWith("http://") || s.startsWith("https://") ||
s.startsWith("data:") || s.startsWith("blob:")) {
return x;
}
// 尝试补全相对路径
try {
const abs = new URL(s, document.baseURI).href;
return { ...x, src: abs, _source: x._source || "relative" };
} catch {
return { ...x, _bad: true };
}
}).filter(x => !x._bad);
const out = [];
// ===== 7. 处理 blob URL(保持原有 DOM 顺序) =====
if (downloadBlobs) {
for (const x of items) {
if (!x.src.startsWith("blob:")) {
out.push(stripRuntimeFields(x));
continue;
}
let converted = null;
let fetchError = null;
try {
const blob = await fetchBlobWithLimit(x.src, maxBytes);
// 校验类型
if (!blob.type || !blob.type.startsWith("image/")) {
warnings.push("blob_not_image:" + (blob.type || "unknown"));
} else {
converted = await blobToDataUri(blob);
}
} catch (e) {
if (String(e || "").includes("blob_too_large:")) {
warnings.push(String(e).replace(/^Error:\s*/, ""));
continue;
}
fetchError = e;
}
if (!converted) {
try {
converted = await imageElementToDataUri(x._node);
} catch (canvasError) {
const fetchMsg = fetchError ? String(fetchError).slice(0, 60) : "n/a";
const canvasMsg = String(canvasError).slice(0, 60);
warnings.push("blob_convert_failed:fetch=" + fetchMsg + ";canvas=" + canvasMsg);
continue;
}
}
out.push({
...stripRuntimeFields(x),
data_uri: converted.dataUri,
mime: converted.mime,
byte_size: converted.byteSize,
_source: converted.source
});
}
} else {
for (const x of items) {
out.push(stripRuntimeFields(x));
}
}
return { images: out, warnings: warnings, scope: scopeUsed, nodeCount: nodes.length };
}).call(this, arguments[0]);
"""
def __init__(self):
self._log_prefix = "[ImageExtractor]"
def extract(
self,
element: Any,
config: Optional[Dict] = None,
container_selector_fallback: Optional[str] = None
) -> List[Dict]:
"""
从页面元素提取图片
Args:
element: 页面元素对象(需支持 run_js 方法)
config: 图片提取配置(ImageExtractionConfig 格式)
container_selector_fallback: 容器选择器回退值(当 config 中未指定时使用)
Returns:
图片数据列表,每项符合 ImageData 格式
任何异常都返回空列表,不抛出异常
Example:
>>> extractor = ImageExtractor()
>>> images = extractor.extract(element, {"enabled": True})
>>> for img in images:
... print(img["kind"], img.get("url") or "data_uri")
"""
# 合并默认配置
final_config = get_default_image_extraction_config()
if config:
final_config.update(config)
# 检查是否启用
if not final_config.get("enabled", False):
logger.debug(f" 图片提取未启用,跳过")
return []
if not element:
logger.debug(f" 元素为空,跳过")
return []
# 构建 JS 参数
container_selector = final_config.get("container_selector") or container_selector_fallback
js_opts = {
"selector": final_config.get("selector", "img"),
"containerSelector": container_selector,
"waitForLoad": final_config.get("wait_for_load", True),
"loadTimeoutMs": int((final_config.get("load_timeout_seconds") or 5) * 1000),
"downloadBlobs": final_config.get("download_blobs", True),
"maxBytes": int((final_config.get("max_size_mb") or 10) * 1024 * 1024),
"srcAllowPatterns": final_config.get("src_allow_patterns", []) or [],
"mode": final_config.get("mode", "all"),
"allowContainerFallback": bool(final_config.get("allow_container_fallback", True)),
"canvasExportMime": final_config.get("canvas_export_mime", "image/jpeg"),
"canvasExportQuality": float(final_config.get("canvas_export_quality", 0.88) or 0.88),
"requestBaselineToken": str(final_config.get("request_baseline_token") or ""),
"requestBaselineProperty": str(final_config.get("request_baseline_property") or ""),
"requestBaselineExcludeExistingNodes": bool(
final_config.get("request_baseline_exclude_existing_nodes")
),
}
try:
# 执行 JS
result = as_element(element).run_js(self.EXTRACT_IMAGES_JS, js_opts)
if not result:
logger.debug(f" JS 返回空结果")
return []
raw_images = result.get("images", [])
warnings = result.get("warnings", [])
scope = result.get("scope", "-")
node_count = result.get("nodeCount", "-")
# 记录警告(不中断流程)
for w in warnings:
logger.warning(f" {w}")
# 规范化 + 去重
images = self._normalize_and_dedupe(raw_images)
# 日志摘要
is_quiet = bool(final_config.get("quiet", False))
if not is_quiet or len(images) > 0:
logger.debug(
f"图片查找完成: {len(images)} 张图片 (selector={js_opts['selector']}, "
f"scope={scope}, nodes={node_count})"
)
for img in images[:5]: # 最多记录前 5 张
self._log_image_summary(img)
if len(images) > 5:
logger.debug(f" ... 还有 {len(images) - 5} 张")
return images
except Exception as e:
# 🔴 关键:图片提取失败不能影响主流程
logger.error(f" 提取失败(已降级为空列表): {e}")
return []
def _normalize_and_dedupe(self, raw_images: List[Dict]) -> List[Dict]:
"""
规范化并去重
处理逻辑:
1. 过滤窗口占位图与已知 1x1 空白占位图
2. 确定 kind (url/data_uri)
3. 提取 source 类型
4. 按 key 去重(url 用完整 URL,data_uri 用前 200 字符)
"""
seen_keys = set()
result = []
now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
for i, img in enumerate(raw_images):
if img.get("_windowed_placeholder") is True:
logger.debug(" 跳过图片窗口脚本替换的占位图")
continue
src = img.get("src", "")
data_uri = img.get("data_uri")
# 过滤已知的 1x1 占位图
if (src and src.strip() in KNOWN_1X1_PLACEHOLDER_DATA_URIS) or (
data_uri and data_uri.strip() in KNOWN_1X1_PLACEHOLDER_DATA_URIS
):
logger.debug(" 跳过已知的 1x1 空白占位图片")
continue
# 确定 kind 和去重键
if data_uri:
kind = "data_uri"
key = f"data_uri:{hashlib.sha256(data_uri.encode('utf-8')).hexdigest()}"
elif src.startswith("data:"):
kind = "data_uri"
data_uri = src
key = f"data_uri:{hashlib.sha256(src.encode('utf-8')).hexdigest()}"
else:
kind = "url"
key = f"url:{src}"
# 去重检查
if key in seen_keys:
logger.debug(f" 跳过重复: {key[:50]}...")
continue
seen_keys.add(key)
# 检测来源类型
source = img.get("_source")
if not source:
source = self._detect_source(src)
# 构建标准化结构(符合 ImageData schema)
image_data = {
"kind": kind,
"url": src if kind == "url" else None,
"data_uri": data_uri if kind == "data_uri" else None,
"mime": img.get("mime"),
"byte_size": img.get("byte_size"),
"alt": img.get("alt"),
"width": img.get("width"),
"height": img.get("height"),
"index": i,
"detected_at": now,
"source": source
}
result.append(image_data)
return result
def _detect_source(self, src: str) -> str:
"""检测图片来源类型"""
if not src:
return "unknown"
if src.startswith("data:"):
return "data_uri"
if src.startswith("blob:"):
return "blob"
if src.startswith("http://") or src.startswith("https://"):
return "currentSrc"
return "relative"
def _log_image_summary(self, img: Dict):
"""
记录图片摘要信息(安全日志)
⚠️ 绝不记录 data_uri 完整内容
"""
kind = img.get("kind")
source = img.get("source", "unknown")
index = img.get("index", 0)
if kind == "url":
url = img.get("url", "")
# 截断长 URL
url_display = (url[:80] + "...") if len(url) > 80 else url
logger.debug(f" [{index}] {kind}/{source}: {url_display}")
else:
# data_uri 只记录元信息
mime = img.get("mime", "unknown")
size = img.get("byte_size")
size_str = f"{size} bytes" if size else "unknown size"
logger.debug(f" [{index}] {kind}/{source}: mime={mime}, {size_str}")
# ============ 单例实例 ============
image_extractor = ImageExtractor()
__all__ = ['ImageExtractor', 'image_extractor', 'get_default_image_extraction_config']
File diff suppressed because it is too large. Load diff
+210
View File
@@ -0,0 +1,210 @@
"""
app/core/extractors/registry.py - 提取器注册中心
提供提取器的动态注册、查找、实例化功能。
支持从配置文件动态加载提取器类。
"""
import importlib
import logging
from typing import Dict, Type, Optional, List
from app.core.extractors.base import BaseExtractor
logger = logging.getLogger(__name__)
class ExtractorRegistry:
"""
提取器注册中心
职责:
1. 管理所有可用的提取器类
2. 提供装饰器注册机制
3. 根据 ID 获取提取器实例
4. 支持动态导入(从配置文件加载)
"""
_extractors: Dict[str, Type[BaseExtractor]] = {}
_default_extractor_id: str = "deep_mode_v1"
@classmethod
def register(cls, extractor_id: Optional[str] = None):
"""
装饰器:注册提取器类
用法:
@ExtractorRegistry.register("custom_v1")
class CustomExtractor(BaseExtractor):
pass
Args:
extractor_id: 提取器 ID(可选,默认使用类的 get_id())
"""
def decorator(extractor_class: Type[BaseExtractor]):
# 确定 ID
eid = extractor_id
if eid is None:
if hasattr(extractor_class, 'get_id'):
eid = extractor_class.get_id()
else:
eid = extractor_class.__name__.lower()
# 注册
cls._extractors[eid] = extractor_class
logger.debug(f"已注册提取器: {eid} -> {extractor_class.__name__}")
return extractor_class
return decorator
@classmethod
def register_class(cls, extractor_class: Type[BaseExtractor], extractor_id: Optional[str] = None):
"""
直接注册提取器类(非装饰器方式)
Args:
extractor_class: 提取器类
extractor_id: 提取器 ID(可选)
"""
eid = extractor_id
if eid is None:
if hasattr(extractor_class, 'get_id'):
eid = extractor_class.get_id()
else:
eid = extractor_class.__name__.lower()
cls._extractors[eid] = extractor_class
logger.info(f"已注册提取器: {eid} -> {extractor_class.__name__}")
@classmethod
def get(cls, extractor_id: Optional[str] = None) -> BaseExtractor:
"""
获取提取器实例
Args:
extractor_id: 提取器 ID(None 则返回默认提取器)
Returns:
提取器实例
Raises:
ValueError: 提取器 ID 不存在
"""
eid = extractor_id or cls._default_extractor_id
if eid not in cls._extractors:
raise ValueError(
f"未知的提取器 ID: {eid},可用的提取器: {list(cls._extractors.keys())}"
)
extractor_class = cls._extractors[eid]
return extractor_class()
@classmethod
def get_class(cls, extractor_id: str) -> Type[BaseExtractor]:
"""
获取提取器类(不实例化)
Args:
extractor_id: 提取器 ID
Returns:
提取器类
Raises:
ValueError: 提取器 ID 不存在
"""
if extractor_id not in cls._extractors:
raise ValueError(f"未知的提取器: {extractor_id}")
return cls._extractors[extractor_id]
@classmethod
def list_all(cls) -> List[Dict[str, str]]:
"""
列出所有已注册的提取器
Returns:
提取器信息列表,每项包含 id/name/description
"""
result = []
for eid, extractor_class in cls._extractors.items():
info = {
"id": eid,
"name": extractor_class.get_name() if hasattr(extractor_class, 'get_name') else extractor_class.__name__,
"description": extractor_class.get_description() if hasattr(extractor_class, 'get_description') else "",
}
result.append(info)
return result
@classmethod
def exists(cls, extractor_id: str) -> bool:
"""
检查提取器是否存在
Args:
extractor_id: 提取器 ID
Returns:
是否存在
"""
return extractor_id in cls._extractors
@classmethod
def set_default(cls, extractor_id: str):
"""
设置默认提取器
Args:
extractor_id: 提取器 ID
Raises:
ValueError: 提取器不存在
"""
if not cls.exists(extractor_id):
raise ValueError(f"无法设置默认提取器:{extractor_id} 不存在")
cls._default_extractor_id = extractor_id
logger.info(f"默认提取器已设置为: {extractor_id}")
@classmethod
def get_default_id(cls) -> str:
"""获取默认提取器 ID"""
return cls._default_extractor_id
@classmethod
def load_from_module(cls, module_path: str, class_name: str, extractor_id: str):
"""
动态加载并注册提取器类
用于从配置文件加载自定义提取器
Args:
module_path: 模块路径(如 "app.core.extractors.custom")
class_name: 类名(如 "CustomExtractor")
extractor_id: 注册的 ID
Raises:
ImportError: 模块不存在
AttributeError: 类不存在
"""
try:
module = importlib.import_module(module_path)
extractor_class = getattr(module, class_name)
# 验证是否为 BaseExtractor 子类
if not issubclass(extractor_class, BaseExtractor):
raise TypeError(f"{class_name} 不是 BaseExtractor 的子类")
cls.register_class(extractor_class, extractor_id)
logger.info(f"动态加载提取器成功: {module_path}.{class_name} -> {extractor_id}")
except Exception as e:
logger.error(f"动态加载提取器失败: {module_path}.{class_name} - {e}")
raise
__all__ = ['ExtractorRegistry']