Files
myquant/djapi/api/video/newsProcess.py
T
simonandClaude Opus 4.7 271a9343a5 Initial commit: cc-cursor 全链路量化研究平台
7 Sprints 全部完成:
  Sprint 0: 基础设施 (DataManager + MariaDB)
  Sprint 1: 因子引擎 (34因子/12分类)
  Sprint 2: VectorBT 回测 (5策略+截面)
  Sprint 3: Optuna 优化 (+Walk-Forward)
  Sprint 4: ML 模型 (LightGBM+CatBoost)
  Sprint 5: Qwen 情绪因子 (三源新闻+日期对齐)
  Sprint 6: Agent 系统 (4Agent+日报.md/.html)

生产加固 (15项): Tushare双源fallback, SSH自动恢复, pool_pre_ping,
  save_daily先删后插, load_dotenv绝对路径, 日报5d/20d修复,
  RiskAgent改上证指数, 昨日对比+数据截止, mac_report utf8mb4,
  CLAUDE-*.md 9条已知Bug, demo全参数化, djapi数据源归一化,
  indexDatas API修正

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 15:59:05 +08:00

116 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
xwlb_daily 表结构如下:
+--------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+----------------+
| nid | int(11) | NO | PRI | NULL | auto_increment |
| news_days | date | NO | | NULL | |
| daily_sub_id | int(11) | NO | | NULL | |
| news_raw | text | NO | | NULL | |
| news_improve | text | NO | | NULL | |
| news_title | text | NO | | NULL | |
+--------------+---------+------+-----+---------+----------------+
xwlb_daily_ext 表结构如下:
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| extid | int(11) | NO | PRI | NULL | auto_increment |
| news_date | date | NO | | NULL | |
| sub_id | tinyint(4) | NO | | NULL | |
| news_title | varchar(256) | NO | | NULL | |
| news_content | text | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
获取给定日期的所有news_improve字段内容,以daily_sub_id 顺序拼接为一个字符串返回。
调用mysqlHandler中的方法执行SQL查询。
"""
from mysqlHandle import MySQLDB
from deepseek import deepseek_text
import json
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def get_news_improve_by_date(target_date):
"""
获取指定日期的所有news_improve内容,按daily_sub_id顺序拼接
Args:
target_date: 目标日期,格式为'YYYY-MM-DD'
Returns:
str: 拼接后的字符串
"""
try:
# 创建数据库连接对象
db = MySQLDB()
# 查询目标日期在xwlb_daily_ext表中的记录数量
count_result = db.query_data(
table="xwlb_daily_ext",
columns="COUNT(*) as count",
where="news_date = %s",
params=(target_date,)
)
# 如果记录数量存在且大于5条,则返回空字符串
if count_result and count_result[0]['count'] > 5:
return None
# 重新创建数据库连接对象,因为每次查询都会关闭连接
db = MySQLDB()
# 查询目标日期在xwlb_daily表中的news_improve字段,按daily_sub_id升序排列
result = db.query_data(
table="xwlb_daily",
columns="news_improve",
where="news_days = %s order by daily_sub_id ASC",
params=(target_date,))
# 如果查询结果不为空
if result:
# 将每条记录的news_improve字段用换行符连接成字符串
combined_content = '\n'.join([row['news_improve'] for row in result])
# 返回拼接后的字符串
return combined_content
# 查询结果为空时返回空字符串
return None
except Exception as e:
#print(f"查询失败: {e}")
logger.error(f"查询失败: {e}")
return None
def news_to_db(target_date):
result = get_news_improve_by_date(target_date)
if result is None:
logger.warning(f"日期 {target_date} 没有新闻内容或者已经存在处理后的记录。跳过")
return None
logger.info(f"日期 {target_date} 的新闻内容长度:{len(result)} 字符")
prompt= "###请根据下面新闻内容的文本逻辑 \n - 帮我分割成各个独立的新闻内容(注意:不要修改新闻本身,仅分割文本),并给每个新闻总结一个标题; \n - 如果遇到'国内快讯'、'国际快讯'或'联播快讯',也请根据每个条快讯分割为一个新闻以及新闻标题; \n - 返回json格式。json格式包含:news_idnews_titlenews_content; news_id从1开始递增。"
try:
response = deepseek_text(result, prompt)
news_list = json.loads(response)
db = MySQLDB()
for news in news_list:
db.insert_data(
table="xwlb_daily_ext",
data={
"news_date": target_date,
"sub_id": news["news_id"],
"news_title": news["news_title"][:256], # 确保不超过varchar(256)限制
"news_content": news["news_content"]
}
)
logger.info(f"成功插入 {len(news_list)} 条新闻到数据库")
except json.JSONDecodeError as e:
logger.error(f"JSON解析失败: {e}")
logger.error(f"DeepSeek API返回内容: {response}")
except Exception as e:
logger.error(f"插入数据库失败: {e}")
#print(f"DeepSeek API返回结果:{response}")
if __name__ == "__main__":
import sys
from datetime import datetime
#提供日期参数,格式:YYYY-MM-DD
target_date = datetime.now().strftime('%Y-%m-%d')
news_to_db(target_date)