Compare commits
3 Commits
bfa286318d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3dca1bd6b | ||
|
|
f81af19a0a | ||
|
|
9e65266b7e |
12
.dockerignore
Normal file
12
.dockerignore
Normal file
@@ -0,0 +1,12 @@
|
||||
.git
|
||||
__pycache__/
|
||||
venv/
|
||||
*.pyc
|
||||
*.log
|
||||
*.backup
|
||||
*_old.*
|
||||
.last_*
|
||||
debug_page.html
|
||||
monitor.log
|
||||
config.ini
|
||||
data/
|
||||
23
Dockerfile
Normal file
23
Dockerfile
Normal file
@@ -0,0 +1,23 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
GPA_DATA_DIR=/data
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& useradd --create-home --shell /usr/sbin/nologin appuser \
|
||||
&& mkdir -p /data \
|
||||
&& chown -R appuser:appuser /app /data
|
||||
|
||||
COPY requirements.txt /app/
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY monitor.py /app/
|
||||
|
||||
USER appuser
|
||||
|
||||
CMD ["python", "/app/monitor.py", "--config", "/data/config.ini"]
|
||||
10
docker-compose.yml
Normal file
10
docker-compose.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
services:
|
||||
gpa-monitor:
|
||||
build: .
|
||||
container_name: gpa-monitor
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
GPA_DATA_DIR: /data
|
||||
volumes:
|
||||
- ./data:/data
|
||||
- ./config.ini:/data/config.ini:ro
|
||||
110
docs/完整使用文档.md
110
docs/完整使用文档.md
@@ -24,6 +24,7 @@
|
||||
- Python 3.7+
|
||||
- 服务器能访问外网
|
||||
- SSH访问权限
|
||||
- (可选)Docker 20+ / Docker Compose v2
|
||||
|
||||
## 📦 项目文件说明
|
||||
|
||||
@@ -59,34 +60,12 @@
|
||||
```bash
|
||||
# 在本地打包必需文件(WSL或Git Bash)
|
||||
cd /mnt/e/50425/Documents/Github/GPA_Monitoring
|
||||
|
||||
# 创建压缩包(只包含必需文件)
|
||||
tar -czf gpa_monitor.tar.gz \
|
||||
monitor.py \
|
||||
config.ini \
|
||||
requirements.txt \
|
||||
setup_python.sh \
|
||||
diagnose.sh \
|
||||
grade-monitor.service \
|
||||
README.md
|
||||
|
||||
# 查看压缩包内容
|
||||
tar -tzf gpa_monitor.tar.gz
|
||||
```
|
||||
|
||||
### 第二步:上传到服务器
|
||||
|
||||
```bash
|
||||
# 方法1:使用 scp
|
||||
scp gpa_monitor.tar.gz 用户名@服务器IP:/home/用户名/
|
||||
|
||||
# 方法2:使用 rsync(推荐)
|
||||
rsync -avz gpa_monitor.tar.gz 用户名@服务器IP:/home/用户名/
|
||||
|
||||
# 方法3:使用 sftp
|
||||
sftp 用户名@服务器IP
|
||||
put gpa_monitor.tar.gz
|
||||
exit
|
||||
```
|
||||
|
||||
### 第三步:在服务器上解压并安装
|
||||
@@ -95,10 +74,7 @@ exit
|
||||
# 登录到服务器
|
||||
ssh 用户名@服务器IP
|
||||
|
||||
# 解压文件
|
||||
cd ~
|
||||
tar -xzf gpa_monitor.tar.gz
|
||||
cd grade_monitor # 或者你解压到的目录
|
||||
cd grade_monitor # 你解压到的目录
|
||||
|
||||
# 给脚本添加执行权限
|
||||
chmod +x setup_python.sh diagnose.sh
|
||||
@@ -152,9 +128,48 @@ python3 monitor.py --test
|
||||
cat .last_grade_content.txt
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🐳 Docker 部署(推荐)
|
||||
|
||||
> 适合已有 Docker 环境的服务器(例如 docker.aizhangz.top)。
|
||||
|
||||
### 1) 准备文件(本地或服务器)
|
||||
|
||||
必需文件:
|
||||
- `monitor.py`
|
||||
- `requirements.txt`
|
||||
- `Dockerfile`
|
||||
- `docker-compose.yml`
|
||||
- `config.ini`(⚠️ 敏感文件)
|
||||
|
||||
### 2) 在服务器上运行
|
||||
|
||||
```bash
|
||||
# 进入项目目录
|
||||
cd ~/grade_monitor
|
||||
|
||||
# 首次构建并启动
|
||||
docker compose up -d --build
|
||||
|
||||
# 查看日志
|
||||
docker logs -f gpa-monitor
|
||||
```
|
||||
|
||||
### 3) 测试模式(可选)
|
||||
|
||||
```bash
|
||||
docker compose run --rm gpa-monitor python /app/monitor.py --config /data/config.ini --test
|
||||
```
|
||||
|
||||
### 4) 说明
|
||||
|
||||
- 运行日志和缓存文件会写入 `./data/`
|
||||
- 配置文件通过 `./config.ini:/data/config.ini:ro` 挂载
|
||||
|
||||
## 🔧 设置后台运行
|
||||
|
||||
### 方案A:使用 systemd(推荐,生产环境)
|
||||
### 使用 systemd(推荐,生产环境)
|
||||
|
||||
**优点:** 开机自启、崩溃自动重启、系统化管理
|
||||
|
||||
@@ -207,47 +222,6 @@ sudo systemctl enable grade-monitor
|
||||
sudo systemctl disable grade-monitor
|
||||
```
|
||||
|
||||
### 方案B:使用 tmux(简单易用)
|
||||
|
||||
**优点:** 简单、可随时查看输出、适合测试阶段
|
||||
|
||||
```bash
|
||||
# 1. 安装 tmux(如果没有)
|
||||
sudo apt update && sudo apt install tmux
|
||||
|
||||
# 2. 创建会话并运行
|
||||
tmux new -s grade_monitor
|
||||
source venv/bin/activate
|
||||
python3 monitor.py
|
||||
|
||||
# 3. 离开会话(程序继续运行)
|
||||
# 按 Ctrl+B,然后按 D
|
||||
|
||||
# 4. 重新连接查看
|
||||
tmux attach -t grade_monitor
|
||||
|
||||
# 5. 查看所有会话
|
||||
tmux ls
|
||||
|
||||
# 6. 关闭会话
|
||||
tmux kill-session -t grade_monitor
|
||||
```
|
||||
|
||||
### 方案C:使用 nohup(最简单)
|
||||
|
||||
**优点:** 快速、无需额外工具
|
||||
|
||||
```bash
|
||||
# 后台运行
|
||||
source venv/bin/activate
|
||||
nohup python3 monitor.py > output.log 2>&1 &
|
||||
|
||||
# 查看进程
|
||||
ps aux | grep monitor.py
|
||||
|
||||
# 停止程序
|
||||
pkill -f monitor.py
|
||||
```
|
||||
|
||||
## 📊 监控和维护
|
||||
|
||||
|
||||
23
monitor.py
23
monitor.py
@@ -26,6 +26,11 @@ from email.mime.multipart import MIMEMultipart
|
||||
from typing import Optional, Dict
|
||||
import signal
|
||||
|
||||
# 数据目录(用于日志和运行时文件)
|
||||
_DEFAULT_DATA_DIR = Path(__file__).parent.resolve()
|
||||
DATA_DIR = Path(os.environ.get('GPA_DATA_DIR', str(_DEFAULT_DATA_DIR))).expanduser()
|
||||
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 配置日志
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -40,7 +45,7 @@ if not logger.handlers:
|
||||
)
|
||||
|
||||
# 文件处理器
|
||||
file_handler = logging.FileHandler('monitor.log', encoding='utf-8')
|
||||
file_handler = logging.FileHandler(DATA_DIR / 'monitor.log', encoding='utf-8')
|
||||
file_handler.setFormatter(formatter)
|
||||
logger.addHandler(file_handler)
|
||||
|
||||
@@ -67,10 +72,10 @@ class GradeMonitor:
|
||||
})
|
||||
|
||||
# 文件路径
|
||||
self.script_dir = Path(__file__).parent.absolute()
|
||||
self.last_grade_file = self.script_dir / '.last_grade_hash.txt'
|
||||
self.last_grade_html_file = self.script_dir / '.last_grade_page.html'
|
||||
self.last_grade_content_file = self.script_dir / '.last_grade_content.txt'
|
||||
self.data_dir = DATA_DIR
|
||||
self.last_grade_file = self.data_dir / '.last_grade_hash.txt'
|
||||
self.last_grade_html_file = self.data_dir / '.last_grade_page.html'
|
||||
self.last_grade_content_file = self.data_dir / '.last_grade_content.txt'
|
||||
|
||||
# 运行标志
|
||||
self.running = True
|
||||
@@ -362,7 +367,7 @@ class GradeMonitor:
|
||||
if not result:
|
||||
logger.warning("未能提取到成绩信息,页面可能结构异常")
|
||||
# 保存HTML以便调试
|
||||
debug_file = self.script_dir / 'debug_page.html'
|
||||
debug_file = self.data_dir / 'debug_page.html'
|
||||
debug_file.write_text(html, encoding='utf-8')
|
||||
logger.info(f"已保存HTML到 {debug_file} 供调试")
|
||||
|
||||
@@ -469,7 +474,7 @@ class GradeMonitor:
|
||||
logger.info("=" * 60)
|
||||
|
||||
# 保存课程列表
|
||||
courses_file = self.script_dir / '.last_courses.txt'
|
||||
courses_file = self.data_dir / '.last_courses.txt'
|
||||
courses_file.write_text('\n'.join(current_courses), encoding='utf-8')
|
||||
|
||||
# 计算哈希
|
||||
@@ -478,7 +483,7 @@ class GradeMonitor:
|
||||
return (False, [])
|
||||
|
||||
# 读取上次的课程列表
|
||||
courses_file = self.script_dir / '.last_courses.txt'
|
||||
courses_file = self.data_dir / '.last_courses.txt'
|
||||
if courses_file.exists():
|
||||
last_courses = courses_file.read_text(encoding='utf-8').strip().split('\n')
|
||||
else:
|
||||
@@ -792,7 +797,7 @@ class GradeMonitor:
|
||||
# 如果解析失败(0门课程)且之前有课程,可能是临时问题
|
||||
if len(current_courses) == 0:
|
||||
# 检查是否之前有课程记录
|
||||
courses_file = self.script_dir / '.last_courses.txt'
|
||||
courses_file = self.data_dir / '.last_courses.txt'
|
||||
if courses_file.exists():
|
||||
try:
|
||||
previous_courses = courses_file.read_text(encoding='utf-8').strip().split('\n')
|
||||
|
||||
20
readme.md
20
readme.md
@@ -54,6 +54,24 @@ python3 monitor.py
|
||||
# 按 Ctrl+B 然后按 D 离开会话
|
||||
```
|
||||
|
||||
## Docker 部署(推荐)
|
||||
|
||||
> 运行日志和缓存文件会写入 `./data/`,配置文件挂载为只读。
|
||||
|
||||
```bash
|
||||
# 1) 准备配置文件(同级目录下的 config.ini)
|
||||
# 2) 启动
|
||||
docker compose up -d --build
|
||||
|
||||
# 查看日志
|
||||
docker logs -f gpa-monitor
|
||||
```
|
||||
|
||||
**测试模式:**
|
||||
```bash
|
||||
docker compose run --rm gpa-monitor python /app/monitor.py --config /data/config.ini --test
|
||||
```
|
||||
|
||||
## 功能特点
|
||||
|
||||
✅ **智能检测** - 检测新增课程(而非简单的页面变化)
|
||||
@@ -193,4 +211,4 @@ tmux attach -t grade_monitor
|
||||
|
||||
## 许可证
|
||||
|
||||
MIT License
|
||||
MIT License
|
||||
|
||||
Reference in New Issue
Block a user