Files
ai_image_quary/README.md

196 lines
5.8 KiB
Markdown
Raw Permalink 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.

# AI关键词导入与统计工具集
## 项目简介
用于管理 `ai_article` 数据库中百度关键词数据的导入、更新和统计导出。
## 环境依赖
```bash
pip install pandas pymysql openpyxl
```
## 文件说明
| 文件 | 功能 |
|------|------|
| `database_config.py` | 数据库配置与连接管理 |
| `import_keywords.py` | 定频轮询导入关键词到baidu_keyword表 |
| `start_import_keywords.sh` | 服务管理脚本(启动/停止/重启/状态) |
| `update_keywords_from_excel.py` | 根据Excel更新已有关键词记录 |
| `export_author_stats.py` | 导出作者发文统计到CSV |
## 目录结构
```
ai_import_quary/
├── database_config.py # 数据库配置
├── import_keywords.py # 关键词导入脚本(定频轮询)
├── start_import_keywords.sh # 服务管理脚本
├── update_keywords_from_excel.py # 关键词更新脚本
├── export_author_stats.py # 统计导出脚本
├── query_upload/ # Excel导入文件存放目录
└── exports/ # CSV导出文件存放目录
```
## 数据库表关系
```
ai_departments (科室表)
│ id → department_id
ai_authors (作者表)
│ id → author_id
baidu_keyword (关键词表)
│ keyword
ai_articles (文章表)
```
## 使用方法
### 1. 导入关键词 (import_keywords.py)
**服务管理**(推荐):
```bash
./start_import_keywords.sh start # 启动服务
./start_import_keywords.sh stop # 停止服务
./start_import_keywords.sh restart # 重启服务
./start_import_keywords.sh status # 查看状态
./start_import_keywords.sh logs # 查看日志
./start_import_keywords.sh logs-follow # 实时日志
```
**直接运行**
```bash
python import_keywords.py
```
**运行模式**定频轮询默认每60秒轮询一次
**工作流程**
1. 脚本启动后持续监控 `query_upload/` 目录
2. 发现Excel文件后自动处理
3. 处理成功后自动删除源文件
**Excel格式要求**
- 必须包含 `query` 列(关键词,不能为空)
- 必须包含 `科室` 列(部门信息)
**配置参数**(在脚本头部修改):
```python
POLL_INTERVAL = 60 # 轮询间隔(秒)
UPLOAD_FOLDER = 'query_upload' # 监控目录
SEED_ID = 9999 # 固定值
SEED_NAME = '手动提交' # 固定值
CRAWLED = 1 # 固定值
```
**流程图**
```
┌─────────────────┐
│ 定频轮询启动 │
└────────┬────────┘
┌─────────────────┐
│ 读取Excel │
│ (query+科室) │
└────────┬────────┘
┌─────────────────┐
│ 查询科室 │
│ ai_departments │
│ → department_id│
└────────┬────────┘
┌─────────────────┐
│ 指定科室 │
│ 获取author信息 │
│ ai_authors │
│ → author_id │
│ → author_name │
└────────┬────────┘
┌─────────────────┐
│ 判重 │
│ keyword不能重复 │
└────────┬────────┘
┌────┴────┐
│ │
重复 不重复
│ │
▼ ▼
┌──────┐ ┌───────────────────────┐
│ 跳过 │ │ INSERT baidu_keyword │
└──────┘ │ ┌───────────────────┐ │
│ │ keyword = query值 │ │
│ │ crawled = 1 │ │
│ │ seed_id = 9999 │ │
│ │ seed_name = 手动提交│ │
│ │ department = 科室 │ │
│ │ department_id = x │ │
│ │ author_id = 随机 │ │
│ │ author_name = 随机│ │
│ │ query_status = │ │
│ │ manual_review │ │
│ └───────────────────┘ │
└───────────────────────┘
```
### 2. 更新关键词 (update_keywords_from_excel.py)
```bash
python update_keywords_from_excel.py
```
根据Excel更新已存在的关键词记录department、department_id、author_id、author_name
### 3. 导出统计 (export_author_stats.py)
```bash
python export_author_stats.py
python export_author_stats.py --date 2026-01-28
```
**参数**
- `--date`: 目标日期,默认当天
- `--output-dir`: 输出目录,默认 `./exports`
**输出文件**
- `author_review_stats_{date}.csv` - 发文审核统计(所有状态)
- `author_published_stats_{date}.csv` - 发文成功统计published状态
## 数据库配置
默认配置在 `database_config.py`
```python
DB_CONFIG = {
'host': '8.149.233.36',
'user': 'ai_article_read',
'password': '***',
'database': 'ai_article',
'charset': 'utf8mb4'
}
```
## 导入记录字段说明
`baidu_keyword` 表插入字段:
| 字段 | 值 | 说明 |
|------|-----|------|
| keyword | Excel中query列 | 关键词 |
| seed_id | 9999 | 种子ID |
| seed_name | 手动提交 | 种子名称 |
| crawled | 1 | 已爬取标记 |
| parents_id | 0 | 父级ID |
| department | Excel中科室列 | 科室名称 |
| department_id | 查表获取 | 科室ID |
| query_status | manual_review | 查询状态 |
| author_id | 查表获取 | 作者ID |
| author_name | 查表获取 | 作者名称 |