From bb1a9c4470cf76f11efcd78729444aee7b1cd223 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 17 Jun 2026 20:48:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20video=20=E6=A8=A1=E5=9D=97=E4=B8=8E=20co?= =?UTF-8?q?ntinuation=20=E7=8A=B6=E6=80=81=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Simon --- djapi/api/video/audioRead.py | 9 ++++++--- djapi/api/video/newsProcess.py | 19 ++++++++++++++++++- djapi/continuation.md | 31 +++++++++++++++++++------------ 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/djapi/api/video/audioRead.py b/djapi/api/video/audioRead.py index d3a37f9..a282a5e 100644 --- a/djapi/api/video/audioRead.py +++ b/djapi/api/video/audioRead.py @@ -162,7 +162,7 @@ def transcribe_audio(audio_path): # 确保音频文件存在 if not os.path.exists(audio_path): logger.error(f"音频文件不存在: {audio_path}") - return "" + return ['', ''] dashscope.api_key = os.getenv('DASHSCOPE_API_KEY', '') # 创建识别对象 recognition = Recognition( @@ -187,10 +187,10 @@ def transcribe_audio(audio_path): return text else: logger.error(f"❌ 任务失败: {result.message}") - return "" + return ['', ''] except Exception as e: logger.error(f"识别过程中发生异常: {e}") - return "" + return ['', ''] def merge_transcripts(transcripts): """ @@ -287,6 +287,9 @@ def analyze_and_correct_text(text): # 首先修正文本错误 corrected_text = text_correction(text) + if corrected_text is None: + logger.warning("文本修正返回 None,使用原始文本") + corrected_text = text logger.info(f"原始文本长度: {len(text)}") logger.info(f"修正后文本长度: {len(corrected_text)}") diff --git a/djapi/api/video/newsProcess.py b/djapi/api/video/newsProcess.py index dd71bab..b2ecc4d 100644 --- a/djapi/api/video/newsProcess.py +++ b/djapi/api/video/newsProcess.py @@ -86,7 +86,24 @@ def news_to_db(target_date): prompt= "###请根据下面新闻内容的文本逻辑 \n - 帮我分割成各个独立的新闻内容(注意:不要修改新闻本身,仅分割文本),并给每个新闻总结一个标题; \n - 如果遇到'国内快讯'、'国际快讯'或'联播快讯',也请根据每个条快讯分割为一个新闻以及新闻标题; \n - 返回json格式。json格式包含:news_id,news_title,news_content; news_id从1开始递增。" try: response = deepseek_text(result, prompt) - news_list = json.loads(response) + data = json.loads(response) + # DeepSeek json_object 模式返回的是 dict(如 {"news": [...]}), + # 普通模式返回的是纯数组 [...],这里做自适应提取 + if isinstance(data, dict): + # 从 dict 中提取列表:找第一个 list 类型的 value + news_list = None + for v in data.values(): + if isinstance(v, list): + news_list = v + break + if news_list is None: + # 所有 value 都不是 list,可能是 {"1": {...}, "2": {...}} 格式 + news_list = list(data.values()) + elif isinstance(data, list): + news_list = data + else: + raise ValueError(f"不支持 DeepSeek 响应格式: {type(data)}") + db = MySQLDB() for news in news_list: db.insert_data( diff --git a/djapi/continuation.md b/djapi/continuation.md index 9d2474e..9460cb6 100644 --- a/djapi/continuation.md +++ b/djapi/continuation.md @@ -2,7 +2,7 @@ ## 当前项目状态 -djapi — Django 5.2 金融数据 API 项目,2026-06-03 已部署。 +djapi — Django 5.2 金融数据 API 项目,2026-06-17 已部署。 服务器:`simon@doorcome.cn`,路径 `/home/simon/myquant/djapi/`,虚拟环境 `/opt/miniconda/envs/django/`。 @@ -20,37 +20,43 @@ djapi — Django 5.2 金融数据 API 项目,2026-06-03 已部署。 - `api/stock/config.py`:拆分为 config / strategy_config / scan_config ### 3. drf-spectacular 集成 -- 15 端点 `@api_view` + `@extend_schema`,8 tag 分组 +- 14 端点 `@api_view` + `@extend_schema`,8 tag 分组 - 13 Serializer,Swagger `/api/docs/` -### 4. 新功能 -- akshare 股息率 API:`api/stock/getDivData_AK.py`,`GET /api/getdivak/` +### 4. 股息率 API 优化 +- **删除** `api/stock/getDivData_AK.py`(akshare 版),`/api/getdivak/` 路由移除 +- **优化** `api/stock/getStockDiv2.py`: + - TTM 计算:`calculate_ttm_div` 行级循环 O(n²) → `rolling('360D').sum()` O(n) + - 删除向前填充逻辑(~30 行),避免与毛刺平滑冲突 +- **修复** `api/stock/smoothBrush.py`:if/elif 分支中 prev_valid/next_valid 赋值反了 -### 5. video 模块重构 +### 5. video 模块重构与 Bug 修复 - 新增 `api/video/env.py` — .env 加载 - `newsRedo.py` 重写 — 三分支智能重处理 - **P0 修复**:`getVideo5.py` 日期校验 bug(`start_date > start_date` → `start_date > end_date`) - **P1 清理**:删除 `ai.py`(两个函数均为死代码),清理 `newsProcess.py` 冗余 import +- **P2 修复**:`audioRead.py` — `transcribe_audio` 异常时返回 `['', '']` 统一类型;`analyze_and_correct_text` 防御 None +- **P3 修复**:`newsProcess.py` — `news_to_db()` JSON 解析自适应 dict/list(DeepSeek json_object 模式返回 dict 包装) ### 6. 文档与测试 - `CLAUDE.md`、`README.md`、`continuation.md` - 18 个单元测试 -## video 目录文件现状(9 个 .py) +## video 目录文件现状(10 个 .py) | 文件 | 职责 | |------|------| | `env.py` | .env 加载 | | `getVideo5.py` | 主流程:抓取→下载→ASR→入库 | -| `audioRead.py` | 音频转换、分割、ASR 识别 | -| `deepseek.py` | DeepSeek API 封装(类 + 函数) | -| `newsProcess.py` | AI 新闻分割+标题提取 | +| `audioRead.py` | 音频转换、分割、ASR 识别、文本纠错 | +| `deepseek.py` | DeepSeek API 封装(类 + `deepseek_text` 函数,支持 `response_format`) | +| `newsProcess.py` | AI 新闻分割+标题提取,JSON 自适应解析 | | `newsRedo.py` | 手动重处理(三分支) | | `main.py` | 定时任务入口(当天) | | `main_videos.py` | 批量补缺(扫描缺失日期) | | `mysqlHandle.py` | MySQLDB 重新导出 | -## 所有 API 端点(15 个) +## 所有 API 端点(14 个) | 端点 | 数据源 | 说明 | |------|--------|------| @@ -65,8 +71,7 @@ djapi — Django 5.2 金融数据 API 项目,2026-06-03 已部署。 | `dailymargin/` | Tushare | 每日融资融券汇总 | | `stockmargin/` | Tushare | 个股融资融券 | | `finance/` | Tushare | 财务报表分析 | -| `getdiv/` | Tushare | 股息率 | -| `getdivak/` | akshare | 股息率(无需 token) | +| `getdiv/` | Tushare | 股息率(TTM rolling + 毛刺平滑) | | `xwlbNews/` | MySQL | 新闻联播原始文本 | | `xwlbFine/` | MySQL | 新闻联播 AI 精编 | @@ -94,3 +99,5 @@ ssh simon@doorcome.cn "kill \$(lsof -ti:5004); sleep 2; /opt/miniconda/envs/djan - video 模块保护、向后兼容优先、不使用 python-dotenv - rsync 陷阱:多文件源会展平路径 - `.env` 双加载:Django 端 `djapi/env_loader.py` + video 端 `api/video/env.py` +- 股息率 TTM 用 `rolling('360D').sum()` 向量化,不手动循环 +- DeepSeek json_object 模式返回 dict,newsProcess 自适应提取 list