调整py脚本

This commit is contained in:
2026-01-28 12:28:04 +08:00
parent edebf424db
commit d1afc64e5a
9 changed files with 195 additions and 831 deletions

View File

@@ -14,8 +14,8 @@ public class BocaiApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(BocaiApplication.class, args);
BetSchedule betSchedule = context.getBean(BetSchedule.class);
betSchedule.placeBet();
// BetSchedule betSchedule = context.getBean(BetSchedule.class);
// betSchedule.placeBet();
// // 依次执行三个任务
//
@@ -24,20 +24,10 @@ public class BocaiApplication {
// CrawlerSchedule crawlerSchedule = context.getBean(CrawlerSchedule.class);
// crawlerSchedule.executeLotteryDraw();
//
// // 2. 执行ExAggregateDataScriptSchedule方法
// System.out.println("\n=== 开始执行ExAggregateDataScriptSchedule任务 ===");
// ExAggregateDataScriptSchedule exAggregateDataScriptSchedule = context.getBean(ExAggregateDataScriptSchedule.class);
// exAggregateDataScriptSchedule.executePythonScript();
//
// // 3. 执行ExBetScriptSchedule方法
// System.out.println("\n=== 开始执行ExBetScriptSchedule任务 ===");
// ExBetScriptSchedule exBetScriptSchedule = context.getBean(ExBetScriptSchedule.class);
// exBetScriptSchedule.executePythonScript();
//
// // 4. 执行GenBetRecordSchedule方法
// System.out.println("\n=== 开始执行GenBetRecordSchedule任务 ===");
// GenBetRecordSchedule genBetRecordSchedule = context.getBean(GenBetRecordSchedule.class);
// genBetRecordSchedule.processBetPredictions();
// 3. 执行ExBetScriptSchedule方法
System.out.println("\n=== 开始执行ExBetScriptSchedule任务 ===");
ExBetScriptSchedule exBetScriptSchedule = context.getBean(ExBetScriptSchedule.class);
exBetScriptSchedule.executePythonScript();
}
}

View File

@@ -46,7 +46,7 @@ public class BetSchedule {
private BetRecordRepository betRecordRepository;
// 每天早上7.07分开始每5分钟执行一次到第二天早上6点结束7:07, 7:12, 7:17...23:57, 0:02, 0:07...5:57
@Scheduled(cron = "0 7/5 7-23,0-6 * * ?")
// @Scheduled(cron = "0 7/5 7-23,0-6 * * ?")
public void placeBet() {
LocalDateTime now = LocalDateTime.now();
int hour = now.getHour();

View File

@@ -26,7 +26,7 @@ public class CrawlerSchedule {
// 每天凌晨2点执行爬取开奖结果
//@Scheduled(cron = "0 0 2 * * ?")
// 每7秒执行一次爬取开奖结果
@Scheduled(cron = "*/9 * * * * ?")
// @Scheduled(cron = "*/9 * * * * ?")
/*@Scheduled(cron = "0 6-59/5 7-23 * * ?")
@Scheduled(cron = "0 0-55/5 0-6 * * ?")*/
public void executeLotteryDraw() {

View File

@@ -1,121 +0,0 @@
package com.tem.bocai.schedules;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 执行统计脚本
*/
@Component
public class ExAggregateDataScriptSchedule {
/**
* 处理文件路径,确保路径正确
* @param filePath 文件路径
* @return 处理后的文件路径
*/
private String handleFilePath(String filePath) {
// 处理路径分隔符,统一使用系统默认分隔符
filePath = filePath.replace("/", System.getProperty("file.separator"));
filePath = filePath.replace("\\", System.getProperty("file.separator"));
// 如果路径包含空格,确保路径被正确处理
// Runtime.exec会自动处理带空格的路径不需要手动添加引号
return filePath;
}
public static void main(String[] args) {
ExAggregateDataScriptSchedule schedule = new ExAggregateDataScriptSchedule();
schedule.executePythonScript();
}
// 每天凌晨2点执行
// @Scheduled(cron = "0 10 6 * * ?")
public void executePythonScript() {
System.out.println("开始执行Python脚本...");
// 执行aggregate_data_v7.py脚本不需要参数
String[] params = {};
executePythonScriptWithParams("aggregate_data_v7.py", params);
}
/**
* 执行带参数的Python脚本
* @param scriptPath Python脚本路径
* @param params 脚本参数数组(可以包含文件路径)
*/
public void executePythonScriptWithParams(String scriptPath, String[] params) {
try {
// 处理脚本路径,确保路径正确
scriptPath = handleFilePath(scriptPath);
// 构建命令数组
String[] command = new String[params.length + 2];
command[0] = "python";
command[1] = scriptPath;
// 添加参数,处理文件路径参数
for (int i = 0; i < params.length; i++) {
// 检查参数是否为文件路径(包含路径分隔符)
if (params[i].contains("\\") || params[i].contains("/")) {
command[i + 2] = handleFilePath(params[i]);
} else {
command[i + 2] = params[i];
}
}
System.out.println("执行命令: " + String.join(" ", command));
// 创建ProcessBuilder并设置工作目录为PyModel
ProcessBuilder pb = new ProcessBuilder(command);
// 获取项目根目录的绝对路径
String projectRoot = System.getProperty("user.dir");
// 设置工作目录为PyModel的绝对路径
java.io.File pyModelDir = new java.io.File(projectRoot, "PyModel");
pb.directory(pyModelDir);
System.out.println("执行目录: " + pyModelDir.getAbsolutePath());
// 执行Python脚本
Process process = pb.start();
// 读取脚本输出
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "UTF-8")
);
String line;
StringBuilder output = new StringBuilder();
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
// 读取错误输出
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(process.getErrorStream(), "UTF-8")
);
StringBuilder errorOutput = new StringBuilder();
while ((line = errorReader.readLine()) != null) {
errorOutput.append(line).append("\n");
}
// 等待脚本执行完成
int exitCode = process.waitFor();
System.out.println("Python脚本执行完成退出码: " + exitCode);
System.out.println("脚本输出:\n" + output.toString());
if (exitCode != 0) {
System.err.println("脚本执行错误:\n" + errorOutput.toString());
}
} catch (IOException | InterruptedException e) {
System.err.println("执行Python脚本时发生错误:");
e.printStackTrace();
}
}
}

View File

@@ -1,11 +1,23 @@
package com.tem.bocai.schedules;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.json.JSONArray;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.tem.bocai.entity.BetRecord;
import com.tem.bocai.repository.BetRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 执行预测脚本
@@ -13,6 +25,9 @@ import java.io.InputStreamReader;
@Component
public class ExBetScriptSchedule {
@Autowired
private BetRecordRepository betRecordRepository;
/**
* 处理文件路径,确保路径正确
* @param filePath 文件路径
@@ -34,14 +49,19 @@ public class ExBetScriptSchedule {
schedule.executePythonScript();
}
// 每天凌晨2点执行
// @Scheduled(cron = "0 10 6 * * ?")
// 每分钟执行一次
@Scheduled(cron = "0 * * * * ?")
public void executePythonScript() {
System.out.println("开始执行Python脚本...");
// 执行aggregate_data_v7.py脚本不需要参数
String[] params = {};
executePythonScriptWithParams("batch_predict_betting_v7.py", params);
// 获取当前时间格式化为yyyy-MM-dd HH:mm:ss
String currentTime = java.time.LocalDateTime.now().format(
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
);
// 执行batch_predict_betting_v8.py脚本添加当前时间作为参数
String[] params = {"--next_period_time", currentTime};
executePythonScriptWithParams("batch_predict_betting_v8.py", params);
}
/**
@@ -108,6 +128,8 @@ public class ExBetScriptSchedule {
if (exitCode != 0) {
System.err.println("脚本执行错误:\n" + errorOutput.toString());
} else {
parseScriptOutput(output.toString());
}
} catch (IOException | InterruptedException e) {
@@ -115,4 +137,161 @@ public class ExBetScriptSchedule {
e.printStackTrace();
}
}
/**
* 解析脚本输出提取result字段中值不为None的数据
* @param output 脚本输出
*/
private void parseScriptOutput(String output) {
try {
System.out.println("开始解析脚本输出...");
JSONObject jsonOutput = JSON.parseObject(output);
if (jsonOutput == null) {
System.out.println("输出为空或无法解析为JSON");
return;
}
JSONObject result = jsonOutput.getJSONObject("result");
if (result == null) {
System.out.println("未找到result字段");
return;
}
Map<Integer, Map<Integer, Object>> validResults = new HashMap<>();
for (String key : result.keySet()) {
int pos = Integer.parseInt(key);
JSONObject posResult = result.getJSONObject(key);
Map<Integer, Object> validNums = new HashMap<>();
for (String numKey : posResult.keySet()) {
Object value = posResult.get(numKey);
if (value != null && !value.toString().equals("null") && !value.toString().equals("None")) {
validNums.put(Integer.parseInt(numKey), value);
}
}
if (!validNums.isEmpty()) {
validResults.put(pos, validNums);
}
}
if (!validResults.isEmpty()) {
System.out.println("提取到的有效预测结果:");
for (Map.Entry<Integer, Map<Integer, Object>> entry : validResults.entrySet()) {
System.out.println("位置 " + entry.getKey() + ": " + entry.getValue());
}
// 生成符合test.json格式的数据
generateOutputData(validResults);
} else {
System.out.println("未找到有效的预测结果所有值均为None");
}
} catch (Exception e) {
System.err.println("解析脚本输出时发生错误:");
e.printStackTrace();
}
}
/**
* 生成符合test.json格式的数据
*/
private void generateOutputData(Map<Integer, Map<Integer, Object>> validResults) {
org.json.JSONObject outputData = new org.json.JSONObject();
// 设置固定字段
outputData.put("lottery", "SGFT");
// 生成drawNumber: 年月日+001-288
String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
// 计算期数从00:00:00起每5分钟为一期获取当前时间当前期数
LocalDateTime now = LocalDateTime.now();
int hour = now.getHour();
int minute = now.getMinute();
int totalMinutes = hour * 60 + minute;
int period = (totalMinutes / 5) + 1;
// 确保期数在有效范围内
if (period < 1) {
period = 1;
} else if (period > 288) {
period = 288;
}
// 格式化期数为3位数字
String periodStr = String.format("%03d", period);
String drawNumber = dateStr + periodStr;
outputData.put("drawNumber", drawNumber);
// 生成bets数组
org.json.JSONArray betsArray = new org.json.JSONArray();
// 位置标题映射
String[] titles = {"冠军", "亚军", "季军", "第四名", "第五名", "第六名", "第七名", "第八名", "第九名", "第十名"};
for (Map.Entry<Integer, Map<Integer, Object>> posEntry : validResults.entrySet()) {
int pos = posEntry.getKey();
Map<Integer, Object> numsMap = posEntry.getValue();
for (Map.Entry<Integer, Object> numEntry : numsMap.entrySet()) {
int num = numEntry.getKey();
Object value = numEntry.getValue();
if (value != null && !"null".equals(value.toString())) {
org.json.JSONObject betObject = new org.json.JSONObject();
// 生成game字段: "B" + 位置
betObject.put("game", "B" + (pos + 1));
// 设置投注内容
betObject.put("contents", String.valueOf(num));
// 设置固定字段
betObject.put("amount", 1);
betObject.put("odds", 9.599);
// 设置标题
if (pos >= 0 && pos < titles.length) {
betObject.put("title", titles[pos]);
} else {
betObject.put("title", "位置" + (pos + 1));
}
betsArray.put(betObject);
}
}
}
outputData.put("bets", betsArray);
outputData.put("fastBets", false);
outputData.put("ignore", false);
System.out.println("生成的输出数据:");
System.out.println(outputData.toString(2));
// 保存到数据库
try {
// 创建BetRecord对象
BetRecord betRecord = new BetRecord();
betRecord.setBetTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
betRecord.setBetData(outputData.toString());
betRecord.setCreateTime(new Date());
betRecord.setUpdateTime(new Date());
betRecord.setBetNum(drawNumber);
// 检查betNum是否已存在避免重复保存
if (!betRecordRepository.existsByBetNum(drawNumber)) {
betRecordRepository.save(betRecord);
System.out.println("保存投注记录到数据库成功,期数: " + drawNumber);
} else {
System.out.println("投注记录已存在,期数: " + drawNumber);
}
} catch (Exception e) {
System.err.println("保存投注记录到数据库失败:");
e.printStackTrace();
}
}
}

View File

@@ -1,210 +0,0 @@
package com.tem.bocai.schedules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import com.tem.bocai.entity.BetRecord;
import com.tem.bocai.repository.BetRecordRepository;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
@Component
public class GenBetRecordSchedule {
@Autowired
private BetRecordRepository betRecordRepository;
// 静态变量,用于跟踪顺序序号
private static int seqCounter = 1;
public static void main(String[] args) {
// 注意直接运行此方法会导致betRecordRepository为null
// 请通过BocaiApplication的main方法运行或使用Spring Boot启动
System.out.println("请通过BocaiApplication的main方法运行或使用Spring Boot启动");
}
// 每天6:30执行
// @Scheduled(cron = "0 30 6 * * ?")
public void processBetPredictions() {
LocalDateTime now = LocalDateTime.now();
String currentTime = now.format(
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
);
System.out.println(currentTime + " - 开始处理投注预测数据...");
try {
// 删除所有旧数据
betRecordRepository.deleteAll();
System.out.println(" - 已删除所有旧数据");
// 读取投注预测数据
JSONArray betDataArray = readBetDataFromJson();
System.out.println(" - 成功读取投注预测数据,共" + betDataArray.length() + "条记录");
// 处理每条投注预测数据
for (int i = 0; i < betDataArray.length(); i++) {
JSONObject betData = betDataArray.getJSONObject(i);
String time = betData.getString("time");
System.out.println(" - 处理投注时间: " + time);
// 获取result中值不为null的对象
JSONObject result = betData.getJSONObject("result");
// 生成符合test.json格式的数据
JSONObject outputData = generateOutputData(betData, result);
// 打印处理结果
System.out.println(" - 生成的投注数据: " + outputData);
// 保存处理结果到数据库
saveToDatabase(outputData, time);
}
System.out.println(currentTime + " - 投注预测数据处理完成");
} catch (Exception e) {
System.err.println(currentTime + " - 处理投注预测数据失败:");
e.printStackTrace();
}
}
/**
* 生成符合test.json格式的数据
*/
private JSONObject generateOutputData(JSONObject betData, JSONObject result) {
JSONObject outputData = new JSONObject();
// 设置固定字段
outputData.put("lottery", "SGFT");
// 生成drawNumber: 年月日+001-288
String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
// 确保seqCounter在1-288之间循环
if (seqCounter > 288) {
seqCounter = 1;
}
// 使用顺序序号
String seqStr = String.format("%03d", seqCounter);
String drawNumber = dateStr + seqStr;
// 序号自增
seqCounter++;
outputData.put("drawNumber", drawNumber);
// 生成bets数组
JSONArray betsArray = new JSONArray();
// 位置标题映射
String[] titles = {"冠军", "亚军", "季军", "第四名", "第五名", "第六名", "第七名", "第八名", "第九名", "第十名"};
for (String pos : result.keySet()) {
JSONObject posData = result.getJSONObject(pos);
for (String num : posData.keySet()) {
Object value = posData.get(num);
if (value != null && !"null".equals(value.toString())) {
JSONObject betObject = new JSONObject();
// 生成game字段: "B" + 位置
int position = Integer.parseInt(pos);
betObject.put("game", "B" + (position + 1));
// 设置投注内容
betObject.put("contents", num);
// 设置固定字段
betObject.put("amount", 1);
betObject.put("odds", 9.599);
// 设置标题
if (position >= 0 && position < titles.length) {
betObject.put("title", titles[position]);
} else {
betObject.put("title", "位置" + (position + 1));
}
betsArray.put(betObject);
}
}
}
outputData.put("bets", betsArray);
outputData.put("fastBets", false);
outputData.put("ignore", false);
return outputData;
}
/**
* 从json文件中读取投注预测数据
*/
private JSONArray readBetDataFromJson() throws IOException {
// 使用绝对路径读取文件
String projectRoot = System.getProperty("user.dir");
String filePath = projectRoot + "/PyModel/data_test_predict/betting_predictions_final_1_20.json";
System.out.println(" - 读取投注预测数据文件: " + filePath);
FileReader reader = new FileReader(filePath);
JSONTokener tokener = new JSONTokener(reader);
JSONArray betData = new JSONArray(tokener);
reader.close();
return betData;
}
/**
* 保存有效投注结果到文件
*/
private void saveValidResults(JSONObject validResults, String time) throws IOException {
// 使用绝对路径保存文件
String projectRoot = System.getProperty("user.dir");
String filePath = projectRoot + "/PyModel/bet_output_" + time.replace(":", "-") + ".json";
System.out.println(" - 保存有效投注结果到文件: " + filePath);
FileWriter writer = new FileWriter(filePath);
writer.write(validResults.toString(2)); // 格式化输出缩进2个空格
writer.close();
}
/**
* 保存有效投注结果到数据库
*/
private void saveToDatabase(JSONObject validResults, String time) {
try {
// 从validResults中获取drawNumber作为betNum
String drawNumber = validResults.getString("drawNumber");
// 检查betNum是否已存在
if (betRecordRepository.existsByBetNum(drawNumber)) {
System.out.println(" - 投注编号已存在,跳过保存: " + drawNumber);
return;
}
// 创建BetRecord实体
BetRecord betRecord = new BetRecord();
betRecord.setBetTime(time);
betRecord.setBetData(validResults.toString());
betRecord.setBetNum(drawNumber);
betRecord.setCreateTime(new Date());
betRecord.setUpdateTime(new Date());
// 保存到数据库
BetRecord savedRecord = betRecordRepository.save(betRecord);
System.out.println(" - 保存有效投注结果到数据库ID: " + savedRecord.getId() + ", 投注编号: " + drawNumber);
} catch (Exception e) {
System.err.println(" - 保存有效投注结果到数据库失败: " + e.getMessage());
e.printStackTrace();
}
}
}