将CxstudyTask调整为普通类, 通过initialize入口进行初始化; 调整目录结构; 细化异常处理逻辑; 添加了配置: 是否启用AI答题、是否启用无头模式; 配置文件中可选择Selenium驱动,但除firefox外均未经测试;
144 lines
5.1 KiB
Java
144 lines
5.1 KiB
Java
package work.slhaf.config;
|
||
|
||
import cn.hutool.json.JSONUtil;
|
||
import lombok.Data;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.apache.commons.io.FileUtils;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.Scanner;
|
||
|
||
@Data
|
||
@Slf4j
|
||
public class Config {
|
||
private String browser;
|
||
private boolean aiEnable;
|
||
private boolean headless;
|
||
private CxstudyConfig cxstudyConfig;
|
||
private OcrConfig ocrConfig;
|
||
private AiConfig aiConfig;
|
||
|
||
private Config() {
|
||
}
|
||
|
||
public static Config load() throws IOException {
|
||
File file = new File("config.json");
|
||
boolean configExists = checkConfigExists(file);
|
||
|
||
if (!configExists) {
|
||
generateConfig(file);
|
||
}
|
||
|
||
return loadFromFile(file);
|
||
}
|
||
|
||
private static void generateConfig(File file) throws IOException {
|
||
Scanner sc = new Scanner(System.in);
|
||
System.out.println("\r\n---------------\r\n");
|
||
System.out.println("开始记录配置文件: ");
|
||
Config config = new Config();
|
||
|
||
System.out.println("选择Selenium驱动,必须与安装的浏览器保持一致。该程序开发时使用firefox驱动,其余浏览器驱动未经过测试");
|
||
System.out.println("可选值: edge, firefox, safari, chrome");
|
||
List<String> browsers = new ArrayList<>();
|
||
browsers.add("edge");
|
||
browsers.add("firefox");
|
||
browsers.add("chrome");
|
||
String browser;
|
||
System.out.print("输入你的浏览器名称: ");
|
||
while (true) {
|
||
browser = sc.nextLine();
|
||
if (browsers.contains(browser)){
|
||
config.setBrowser(browser);
|
||
break;
|
||
}
|
||
System.out.println("可选值为: edge, firefox, chrome");
|
||
System.out.print("输入你的浏览器名称: " );
|
||
}
|
||
|
||
System.out.println("\r\n---------------\r\n");
|
||
System.out.println("是否显示GUI界面?第一次运行建议选择显示,测试没有问题后,再修改配置文件为隐藏GUI界面");
|
||
System.out.print("输入 y (启用) 或 n (禁用) : ");
|
||
String headlessStr = "";
|
||
boolean headless;
|
||
while (true) {
|
||
headlessStr = sc.nextLine();
|
||
if (headlessStr.equals("y") || headlessStr.equals("n")){
|
||
headless = headlessStr.equals("y");
|
||
break;
|
||
}
|
||
System.out.print("输入 y (启用) 或 n (禁用) : ");
|
||
}
|
||
config.setHeadless(headless);
|
||
|
||
System.out.println("\r\n---------------\r\n");
|
||
String aiEnableStr;
|
||
boolean aiEnable;
|
||
System.out.print("是否启用答题功能(y / n): ");
|
||
while (true) {
|
||
aiEnableStr = sc.nextLine();
|
||
if (aiEnableStr.equals("y") || aiEnableStr.equals("n")) {
|
||
aiEnable = aiEnableStr.equals("y");
|
||
break;
|
||
}
|
||
System.out.println("请输入 y (启用) 或 n (禁用) : ");
|
||
}
|
||
config.setAiEnable(aiEnable);
|
||
|
||
AiConfig aiConfig = new AiConfig();
|
||
OcrConfig ocrConfig = new OcrConfig();
|
||
if (aiEnable) {
|
||
System.out.println("记录AI配置: ");
|
||
System.out.print("apikey: ");
|
||
aiConfig.setApikey(sc.nextLine());
|
||
System.out.print("baseUrl: ");
|
||
aiConfig.setBaseUrl(sc.nextLine());
|
||
System.out.print("model: ");
|
||
aiConfig.setModel(sc.nextLine());
|
||
|
||
System.out.println("\r\n---------------\r\n");
|
||
System.out.println("记录OCR(用于题目识别)配置: ");
|
||
System.out.print("accessKeyId: ");
|
||
ocrConfig.setAccessKeyId(sc.nextLine());
|
||
System.out.print("accessKeySecret: ");
|
||
ocrConfig.setAccessKeySecret(sc.nextLine());
|
||
}
|
||
|
||
System.out.println("\r\n---------------\r\n");
|
||
CxstudyConfig studyConfig = new CxstudyConfig();
|
||
System.out.println("记录学习通配置: ");
|
||
System.out.print("手机号: ");
|
||
studyConfig.setPhone(sc.nextLine());
|
||
System.out.print("密码: ");
|
||
studyConfig.setPassword(sc.nextLine());
|
||
System.out.print("目标课程: ");
|
||
studyConfig.setTarget(sc.nextLine());
|
||
|
||
System.out.println("\r\n---------------\r\n");
|
||
config.cxstudyConfig = studyConfig;
|
||
config.ocrConfig = ocrConfig;
|
||
config.aiConfig = aiConfig;
|
||
String configStr = JSONUtil.toJsonPrettyStr(config);
|
||
System.out.println("配置文件如下: \r\n" + configStr);
|
||
System.out.println("配置文件将位于: "+file.getAbsolutePath());
|
||
System.out.println("输入 ENTER 以创建配置文件: ");
|
||
sc.nextLine();
|
||
|
||
file.createNewFile();
|
||
FileUtils.writeStringToFile(file, configStr, StandardCharsets.UTF_8);
|
||
System.out.println("配置文件生成完毕");
|
||
}
|
||
|
||
private static Config loadFromFile(File file) throws IOException {
|
||
return JSONUtil.toBean(FileUtils.readFileToString(file, StandardCharsets.UTF_8), Config.class);
|
||
}
|
||
|
||
private static boolean checkConfigExists(File file) {
|
||
return file.exists();
|
||
}
|
||
}
|