docker配置
This commit is contained in:
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')
|
||||
|
||||
Reference in New Issue
Block a user