自动同步: 2026-03-17 02:10
This commit is contained in:
@@ -141,14 +141,26 @@ class DeepAnalyzer:
|
||||
|
||||
# 标准化元数据
|
||||
metadata = self._normalize_metadata(metadata)
|
||||
|
||||
|
||||
title = metadata.get('title', '未知')
|
||||
duration = metadata.get('duration', 0)
|
||||
author = metadata.get('author', '')
|
||||
pubtime = metadata.get('pubtime', '')
|
||||
url = metadata.get('url', '')
|
||||
bvid = metadata.get('bvid', '')
|
||||
platform = metadata.get('platform', 'bilibili')
|
||||
|
||||
# 推断 platform:优先使用元数据中的值,否则根据 URL 或 content_type 推断
|
||||
platform = metadata.get('platform', '')
|
||||
if not platform:
|
||||
if 'zhihu.com' in url or content_type in ('answer', 'zhuanlan', 'question'):
|
||||
platform = 'zhihu'
|
||||
elif 'bilibili.com' in url or bvid:
|
||||
platform = 'bilibili'
|
||||
else:
|
||||
platform = 'bilibili' # 默认值
|
||||
|
||||
# 清理 author 字段:移除零宽度空格和前后空白
|
||||
author = self._clean_author(author)
|
||||
|
||||
# 获取内容 - 支持多种字段名
|
||||
text = content.get('text', '') or content.get('subtitle', '') or content.get('subtitles', '') or content.get('description', '')
|
||||
@@ -186,11 +198,11 @@ class DeepAnalyzer:
|
||||
|
||||
return result
|
||||
|
||||
def _build_prompt(self, title: str, duration: int, duration_str: str, author: str,
|
||||
pubtime: str, url: str, bvid: str, platform: str, text: str,
|
||||
def _build_prompt(self, title: str, duration: int, duration_str: str, author: str,
|
||||
pubtime: str, url: str, bvid: str, platform: str, text: str,
|
||||
content_type: str = "video") -> str:
|
||||
"""构建分析 prompt,根据内容类型选择不同模板"""
|
||||
if content_type == "article":
|
||||
if content_type in ("article", "answer", "zhuanlan"):
|
||||
return self._build_article_prompt(title, author, pubtime, url, platform, text)
|
||||
else:
|
||||
return self._build_video_prompt(title, duration, duration_str, author, pubtime, url, bvid, platform, text)
|
||||
@@ -422,6 +434,22 @@ URL:{url}
|
||||
return f"{h}:{m:02d}:{s:02d}"
|
||||
return f"{m}:{s:02d}"
|
||||
|
||||
def _clean_author(self, author: str) -> str:
|
||||
"""清理 author 字段:移除零宽度空格和前后空白"""
|
||||
if not author:
|
||||
return ''
|
||||
# 移除零宽度空格 (U+200B) 和其他不可见字符
|
||||
invisible_chars = ['\u200b', '\u200c', '\u200d', '\ufeff', '\u00ad']
|
||||
cleaned = author
|
||||
for char in invisible_chars:
|
||||
cleaned = cleaned.replace(char, '')
|
||||
# 移除前后空白和换行
|
||||
cleaned = cleaned.strip()
|
||||
# 如果包含换行符,只取第一行
|
||||
if '\n' in cleaned:
|
||||
cleaned = cleaned.split('\n')[0].strip()
|
||||
return cleaned
|
||||
|
||||
|
||||
# ============== 工具函数 ==============
|
||||
|
||||
@@ -437,19 +465,34 @@ def generate_frontmatter(metadata: Dict, result: AnalysisResult, content_type: s
|
||||
"""生成 Markdown frontmatter"""
|
||||
# 提取标签
|
||||
tags = result.recommended_tags or []
|
||||
|
||||
|
||||
# 格式化日期
|
||||
pubtime = metadata.get('pubtime', '')
|
||||
if pubtime:
|
||||
date_str = str(pubtime)[:10]
|
||||
else:
|
||||
date_str = datetime.now().strftime('%Y-%m-%d')
|
||||
|
||||
fm = ["---",
|
||||
|
||||
# 清理 author 字段
|
||||
author = metadata.get('author', '未知')
|
||||
author = _clean_string(author)
|
||||
|
||||
# 推断 platform
|
||||
url = metadata.get('url', '')
|
||||
platform = metadata.get('platform', '')
|
||||
if not platform:
|
||||
if 'zhihu.com' in url or content_type in ('answer', 'zhuanlan', 'question'):
|
||||
platform = 'zhihu'
|
||||
elif 'bilibili.com' in url or metadata.get('bvid'):
|
||||
platform = 'bilibili'
|
||||
else:
|
||||
platform = 'bilibili'
|
||||
|
||||
fm = ["---",
|
||||
f"title: \"{metadata.get('title', '未命名')}\"",
|
||||
f"date: {date_str}",
|
||||
f"author: {metadata.get('author', '未知')}",
|
||||
f"platform: {metadata.get('platform', 'bilibili')}"]
|
||||
f"author: {author}",
|
||||
f"platform: {platform}"]
|
||||
|
||||
# 视频类型添加 duration 和 bvid
|
||||
if content_type == "video":
|
||||
@@ -470,6 +513,23 @@ def generate_frontmatter(metadata: Dict, result: AnalysisResult, content_type: s
|
||||
return '\n'.join(fm)
|
||||
|
||||
|
||||
def _clean_string(text: str) -> str:
|
||||
"""清理字符串:移除零宽度空格和前后空白"""
|
||||
if not text:
|
||||
return ''
|
||||
# 移除零宽度空格 (U+200B) 和其他不可见字符
|
||||
invisible_chars = ['\u200b', '\u200c', '\u200d', '\ufeff', '\u00ad']
|
||||
cleaned = text
|
||||
for char in invisible_chars:
|
||||
cleaned = cleaned.replace(char, '')
|
||||
# 移除前后空白和换行
|
||||
cleaned = cleaned.strip()
|
||||
# 如果包含换行符,只取第一行
|
||||
if '\n' in cleaned:
|
||||
cleaned = cleaned.split('\n')[0].strip()
|
||||
return cleaned
|
||||
|
||||
|
||||
# ============== 主程序 ==============
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user