255 lines
5.9 KiB
Markdown
255 lines
5.9 KiB
Markdown
# 报告生成器使用说明
|
||
|
||
## 功能概述
|
||
|
||
本模块提供了日报和周报生成功能,主要特点:
|
||
|
||
1. **AI智能筛选**:从AI分析结果表获取已筛选的相关内容(是否相关=1)
|
||
2. **多格式输出**:同时生成HTML和Markdown格式的报告
|
||
3. **钉钉推送**:支持自动推送到钉钉群
|
||
4. **可扩展数据源**:支持添加多个数据源(RSS、投诉、API等)
|
||
5. **灵活模板系统**:支持内置HTML模板和外部HTML模板
|
||
|
||
## 快速开始
|
||
|
||
### 生成日报(24小时内数据)
|
||
|
||
```python
|
||
from applications.reporter.daily import DailyReporter
|
||
|
||
reporter = DailyReporter()
|
||
result = reporter.generate()
|
||
print(f"日报已生成:")
|
||
print(f" HTML: {result.get('html_path')}")
|
||
print(f" Markdown: {result.get('markdown_path')}")
|
||
```
|
||
|
||
### 生成周报(7天内数据)
|
||
|
||
```python
|
||
from applications.reporter.weekly import WeeklyReporter
|
||
|
||
reporter = WeeklyReporter()
|
||
result = reporter.generate()
|
||
print(f"周报已生成:")
|
||
print(f" HTML: {result.get('html_path')}")
|
||
print(f" Markdown: {result.get('markdown_path')}")
|
||
```
|
||
|
||
## 钉钉推送配置
|
||
|
||
### 1. 获取钉钉Webhook地址
|
||
|
||
1. 在钉钉群中,点击"群设置" -> "智能群助手" -> "添加机器人"
|
||
2. 选择"自定义"机器人
|
||
3. 设置机器人名称和头像
|
||
4. 复制Webhook地址(格式:`https://oapi.dingtalk.com/robot/send?access_token=xxx`)
|
||
|
||
### 2. 配置Webhook地址
|
||
|
||
**方式1:通过环境变量(推荐)**
|
||
|
||
```bash
|
||
export DINGTALK_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=xxx"
|
||
```
|
||
|
||
**方式2:在config.py中配置**
|
||
|
||
```python
|
||
DINGTALK_WEBHOOK = "https://oapi.dingtalk.com/robot/send?access_token=xxx"
|
||
```
|
||
|
||
**方式3:在代码中指定**
|
||
|
||
```python
|
||
from applications.reporter.daily import DailyReporter
|
||
|
||
reporter = DailyReporter(dingtalk_webhook="https://oapi.dingtalk.com/robot/send?access_token=xxx")
|
||
result = reporter.generate()
|
||
```
|
||
|
||
### 3. 控制推送行为
|
||
|
||
```python
|
||
# 生成报告但不推送到钉钉
|
||
reporter = DailyReporter()
|
||
result = reporter.generate(send_dingtalk=False)
|
||
|
||
# 不保存Markdown文件
|
||
result = reporter.generate(save_markdown=False)
|
||
|
||
# 同时控制
|
||
result = reporter.generate(save_markdown=True, send_dingtalk=True)
|
||
```
|
||
|
||
### 4. 钉钉消息格式
|
||
|
||
- 自动使用Markdown格式发送
|
||
- 如果内容过长(超过5000字符),会自动截断并显示摘要
|
||
- 包含报告文件路径提示
|
||
|
||
## 添加自定义数据源
|
||
|
||
### 1. 创建数据源类
|
||
|
||
数据源类需要继承 `DataSource` 基类并实现以下方法:
|
||
|
||
```python
|
||
from applications.reporter.base_reporter import DataSource
|
||
from typing import List, Dict, Any
|
||
from datetime import datetime
|
||
|
||
class MyCustomDataSource(DataSource):
|
||
def fetch_data(self, start_time: datetime, end_time: datetime) -> List[Dict[str, Any]]:
|
||
"""获取指定时间范围内的数据"""
|
||
# 返回格式:
|
||
return [
|
||
{
|
||
'title': '标题',
|
||
'link': '链接',
|
||
'summary': '摘要',
|
||
'publish_time': '发布时间',
|
||
'source_url': '来源URL'
|
||
}
|
||
]
|
||
|
||
def get_source_name(self) -> str:
|
||
return "数据源名称"
|
||
```
|
||
|
||
### 2. 添加到报告生成器
|
||
|
||
```python
|
||
from applications.reporter.daily import DailyReporter
|
||
from my_module import MyCustomDataSource
|
||
|
||
reporter = DailyReporter()
|
||
custom_source = MyCustomDataSource(...)
|
||
reporter.add_data_source(custom_source)
|
||
|
||
# 生成报告(会自动包含新数据源的数据)
|
||
report_path = reporter.generate()
|
||
```
|
||
|
||
## 使用外部HTML模板
|
||
|
||
### 1. 创建HTML模板文件
|
||
|
||
创建外部HTML模板文件(如 `custom_template.html`):
|
||
|
||
```html
|
||
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>自定义报告模板</title>
|
||
<style>
|
||
/* 自定义样式 */
|
||
body { ... }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<!-- 内容占位符,支持以下格式之一: -->
|
||
<!-- {{content}} 或 {content} 或 <!-- content --> -->
|
||
{{content}}
|
||
</body>
|
||
</html>
|
||
```
|
||
|
||
### 2. 使用外部模板生成报告
|
||
|
||
```python
|
||
from applications.reporter.daily import DailyReporter
|
||
|
||
reporter = DailyReporter()
|
||
report_path = reporter.generate(template_path="path/to/custom_template.html")
|
||
```
|
||
|
||
## 配置说明
|
||
|
||
### AI配置
|
||
|
||
在 `config.py` 中配置百度AI API:
|
||
|
||
```python
|
||
BAIDU_AI_CONFIG = {
|
||
'api_key': 'your_api_key',
|
||
'model': 'ernie-x1-turbo-32k',
|
||
}
|
||
```
|
||
|
||
### 数据库配置
|
||
|
||
确保 `config.py` 中的数据库配置正确:
|
||
|
||
```python
|
||
MYSQL_CONFIG = {
|
||
'host': 'your_host',
|
||
'port': 3306,
|
||
'user': 'your_user',
|
||
'password': 'your_password',
|
||
'database': 'intelligence_system',
|
||
}
|
||
```
|
||
|
||
## 输出目录
|
||
|
||
- 日报:`output/reports/daily/`
|
||
- 周报:`output/reports/weekly/`
|
||
|
||
报告文件名格式:
|
||
- HTML:`daily_report_YYYYMMDD_HHMMSS.html` / `weekly_report_YYYYMMDD_HHMMSS.html`
|
||
- Markdown:`daily_report_YYYYMMDD_HHMMSS.md` / `weekly_report_YYYYMMDD_HHMMSS.md`
|
||
|
||
## 报告内容
|
||
|
||
生成的报告包含:
|
||
|
||
1. **报告时间信息**:生成时间和时间范围
|
||
2. **数据统计**:相关文章数
|
||
3. **相关新闻列表**(从AI分析结果表筛选,是否相关=1):
|
||
- 标题
|
||
- 分类
|
||
- 标签
|
||
- 摘要
|
||
- 链接
|
||
- 发布时间
|
||
- 相关度评分
|
||
- 分析说明
|
||
|
||
如果没有相关数据,会显示:
|
||
- 日报:`昨日无汽车后市场相关的新闻`
|
||
- 周报:`上周无汽车后市场相关的新闻`
|
||
|
||
## AI筛选说明
|
||
|
||
AI会根据以下定义筛选汽车后市场相关内容:
|
||
|
||
- 汽车维修保养
|
||
- 汽车配件
|
||
- 汽车改装
|
||
- 汽车美容
|
||
- 汽车用品
|
||
- 汽车金融
|
||
- 汽车保险
|
||
- 二手车交易
|
||
- 汽车租赁
|
||
- 汽车检测
|
||
- 汽车报废回收
|
||
- 汽车相关法律法规和政策
|
||
|
||
## 扩展示例
|
||
|
||
参考 `data_source_example.py` 查看如何:
|
||
- 添加数据库数据源
|
||
- 添加外部API数据源
|
||
- 实现自定义数据源
|
||
|
||
## 注意事项
|
||
|
||
1. 确保数据库连接正常
|
||
2. 确保AI API配置正确且有足够配额
|
||
3. 外部模板文件需要包含内容占位符
|
||
4. 数据源返回的数据格式需要符合规范
|
||
|