import json from typing import Literal, Dict, List from enum import Enum from dataclasses import dataclass, field from dataclasses_json import dataclass_json class ErrorType(Enum): NONE = "" ID = "id" PASSWD = "passwd" CAPTCHA = "captcha" @staticmethod def from_error_message(error_msg: str) -> "ErrorType": if "用户名或密码错误" in error_msg: return ErrorType.ID elif "验证码错误" in error_msg: return ErrorType.CAPTCHA elif "密码错误" in error_msg: return ErrorType.PASSWD else: return ErrorType.NONE @dataclass_json @dataclass class Course: course_id: str course_index: str @dataclass_json @dataclass class CourseData: a: List[Course] = field(default_factory=list) # 方案选课 """ 方案选课 """ b: List[Course] = field(default_factory=list) # 自由选课 """ 自由选课 """ @dataclass_json @dataclass class UserData: def __init__(self, std_id: str = "", password: str = "", course: CourseData = CourseData()): self.std_id = std_id self.password = password self.course = course """ 用户数据 """ std_id: str """ 学号 """ password: str """ 密码 """ course: CourseData = field(default_factory=CourseData) """ 课程数据 """ @staticmethod def from_file(file_path: str) -> "UserData": with open(file_path, encoding="utf-8") as f: user_data_load = json.load(f) return UserData.from_dict(user_data_load) @dataclass_json @dataclass class Setting: """ 全局设置 """ manual_login: bool = False """ 是否手动登录 """ browser: Literal["chrome", "edge"] = "edge" """ 使用的浏览器 """ @staticmethod def from_file(file_path: str) -> "Setting": with open(file_path, encoding="utf-8") as f: setting_load = json.load(f) return Setting.from_dict(setting_load)