调整逻辑
This commit is contained in:
@@ -12,8 +12,8 @@ public class TessConfig {
|
|||||||
|
|
||||||
Tesseract instance = new Tesseract();
|
Tesseract instance = new Tesseract();
|
||||||
instance.setLanguage("oci"); // 设置语言包,这里使用英语
|
instance.setLanguage("oci"); // 设置语言包,这里使用英语
|
||||||
instance.setDatapath("src/main/resources/tessdata"); // 设置语言包路径
|
// instance.setDatapath("src/main/resources/tessdata"); // 设置语言包路径
|
||||||
// instance.setDatapath("tessdata"); // 设置语言包路径
|
instance.setDatapath("tessdata"); // 设置语言包路径
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ public class CrawlerSchedule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// @Scheduled(cron = "55 0/5 * * * ?")
|
@Scheduled(cron = "55 0/5 * * * ?")
|
||||||
//@Scheduled(cron = "*/9 * * * * ?")
|
//@Scheduled(cron = "*/9 * * * * ?")
|
||||||
public void executePksHistory() {
|
public void executePksHistory() {
|
||||||
log.info("开始获取历史开奖结果");
|
log.info("开始获取历史开奖结果");
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.tem.bocai.entity.BetRecord;
|
import com.tem.bocai.entity.BetRecord;
|
||||||
|
import com.tem.bocai.entity.LotteryResult;
|
||||||
import com.tem.bocai.repository.BetRecordRepository;
|
import com.tem.bocai.repository.BetRecordRepository;
|
||||||
|
import com.tem.bocai.repository.LotteryResultRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,6 +41,9 @@ public class ExBetScriptSchedule {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private BetRecordRepository betRecordRepository;
|
private BetRecordRepository betRecordRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private LotteryResultRepository lotteryResultRepository;
|
||||||
|
|
||||||
@Value("${pypath}")
|
@Value("${pypath}")
|
||||||
private String pypath;
|
private String pypath;
|
||||||
|
|
||||||
@@ -67,8 +72,8 @@ public class ExBetScriptSchedule {
|
|||||||
|
|
||||||
log.info("开始执行Python脚本...");
|
log.info("开始执行Python脚本...");
|
||||||
|
|
||||||
// 获取当前时间,格式化为yyyy-MM-dd HH:mm:ss
|
// 获取当前时间减去5分钟,格式化为yyyy-MM-dd HH:mm:ss
|
||||||
String currentTime = java.time.LocalDateTime.now().format(
|
String currentTime = java.time.LocalDateTime.now().plusMinutes(5).format(
|
||||||
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -309,57 +314,44 @@ public class ExBetScriptSchedule {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查PyModel/current_data目录下最新的文件是否存在新数据
|
* 检查数据库中最新的开奖记录是否存在新数据
|
||||||
* @return 如果存在新数据返回true,否则返回false
|
* @return 如果存在新数据返回true,否则返回false
|
||||||
*/
|
*/
|
||||||
private boolean checkNewDataExists() {
|
private boolean checkNewDataExists() {
|
||||||
try {
|
try {
|
||||||
File currentDataDir = new File(pypath, "current_data");
|
// 查询最新的开奖记录
|
||||||
if (!currentDataDir.exists() || !currentDataDir.isDirectory()) {
|
Optional<LotteryResult> latestResult = lotteryResultRepository.findTopByOrderByTimeDesc();
|
||||||
log.info("current_data目录不存在");
|
if (!latestResult.isPresent()) {
|
||||||
|
log.info("数据库中未找到开奖记录");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
File[] files = currentDataDir.listFiles();
|
// 获取最新记录的时间
|
||||||
if (files == null || files.length == 0) {
|
String timeStr = latestResult.get().getTime();
|
||||||
log.info("current_data目录为空");
|
if (timeStr == null || timeStr.isEmpty()) {
|
||||||
|
log.info("最新开奖记录时间为空");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
File latestFile = null;
|
// 解析时间字符串为LocalDateTime
|
||||||
FileTime latestTime = null;
|
LocalDateTime recordTime;
|
||||||
|
try {
|
||||||
for (File file : files) {
|
// 假设时间格式为yyyy-MM-dd HH:mm:ss
|
||||||
if (file.getName().endsWith(".json")) {
|
recordTime = LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
Path path = Paths.get(file.getAbsolutePath());
|
} catch (Exception e) {
|
||||||
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
|
log.error("解析开奖记录时间失败: {}", timeStr, e);
|
||||||
FileTime fileTime = attrs.lastModifiedTime();
|
|
||||||
|
|
||||||
if (latestTime == null || fileTime.compareTo(latestTime) > 0) {
|
|
||||||
latestTime = fileTime;
|
|
||||||
latestFile = file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (latestFile == null) {
|
|
||||||
log.info("未找到JSON文件");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalDateTime fileModifiedTime = LocalDateTime.ofInstant(
|
// 检查时间是否在最近5分钟内
|
||||||
latestTime.toInstant(),
|
|
||||||
ZoneId.systemDefault()
|
|
||||||
);
|
|
||||||
|
|
||||||
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime now = LocalDateTime.now();
|
||||||
LocalDateTime fiveMinutesAgo = now.minusMinutes(5);
|
LocalDateTime fiveMinutesAgo = now.minusMinutes(5);
|
||||||
|
|
||||||
if (fileModifiedTime.isAfter(fiveMinutesAgo)) {
|
if (recordTime.isAfter(fiveMinutesAgo)) {
|
||||||
log.info("发现新数据文件: {}, 修改时间: {}", latestFile.getName(), fileModifiedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
log.info("发现新开奖记录: 期号={}, 时间={}", latestResult.get().getIssue(), timeStr);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
log.info("最新数据文件: {}, 修改时间: {}, 超过5分钟,跳过执行", latestFile.getName(), fileModifiedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
log.info("最新开奖记录: 期号={}, 时间={}, 超过5分钟,跳过执行", latestResult.get().getIssue(), timeStr);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|||||||
*
|
*
|
||||||
* @return 当天日期的字符串,例如:2026-01-27
|
* @return 当天日期的字符串,例如:2026-01-27
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||||
public static String getTodayDate() {
|
public static String getTodayDate() {
|
||||||
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime now = LocalDateTime.now();
|
||||||
LocalTime thresholdTime = LocalTime.of(6, 1, 0);
|
LocalTime thresholdTime = LocalTime.of(6, 1, 0);
|
||||||
@@ -60,8 +62,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|||||||
return date.format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
|
return date.format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
|
||||||
}
|
}
|
||||||
/*public static String getTodayDate() {
|
/*public static String getTodayDate() {
|
||||||
LocalDate today = LocalDate.now();
|
// Java 17中可使用var简化局部变量声明
|
||||||
return today.format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
|
var now = LocalDateTime.now();
|
||||||
|
// 核心判断逻辑:小时数<6则取前一天,否则取当天
|
||||||
|
LocalDate targetDate = now.getHour() <= 6
|
||||||
|
? now.toLocalDate().minusDays(1)
|
||||||
|
: now.toLocalDate();
|
||||||
|
// 格式化返回
|
||||||
|
return targetDate.format(DATE_FORMATTER);
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -35,9 +35,10 @@ public class HttpClientExample {
|
|||||||
String url = "https://api.api168168.com/pks/getPksHistoryList.do?lotCode=10058&"+todayDate;
|
String url = "https://api.api168168.com/pks/getPksHistoryList.do?lotCode=10058&"+todayDate;
|
||||||
|
|
||||||
// 设置代理 127.0.0.1:7890
|
// 设置代理 127.0.0.1:7890
|
||||||
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
|
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
|
||||||
|
|
||||||
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(proxy);
|
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
||||||
|
// HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(proxy);
|
||||||
conn.setRequestMethod("GET");
|
conn.setRequestMethod("GET");
|
||||||
conn.setRequestProperty("Origin", "https://xy678kjw.com");
|
conn.setRequestProperty("Origin", "https://xy678kjw.com");
|
||||||
conn.setRequestProperty("Referer", "https://xy678kjw.com/");
|
conn.setRequestProperty("Referer", "https://xy678kjw.com/");
|
||||||
|
|||||||
@@ -290,7 +290,7 @@ public class LotteryWebMagicCrawler implements PageProcessor {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String url = "https://4701268539-esh.qdk63ayw8g.com/member/dresult?lottery=SGFT&date=2026-01-18";
|
String url = "https://4701268539-esh.qdk63ayw8g.com/member/dresult?lottery=SGFT&date=2026-02-06";
|
||||||
|
|
||||||
// 创建爬虫
|
// 创建爬虫
|
||||||
Spider.create(new LotteryWebMagicCrawler("",""))
|
Spider.create(new LotteryWebMagicCrawler("",""))
|
||||||
|
|||||||
@@ -373,16 +373,16 @@ public class TokenCacheService {
|
|||||||
// 等待一下再发送登录请求
|
// 等待一下再发送登录请求
|
||||||
Thread.sleep(1500 + (long) (Math.random() * 1000));
|
Thread.sleep(1500 + (long) (Math.random() * 1000));
|
||||||
|
|
||||||
// 新增代码:增加代理
|
// // 新增代码:增加代理
|
||||||
String proxyHost = "127.0.0.1";
|
// String proxyHost = "127.0.0.1";
|
||||||
int proxyPort = 7890;
|
// int proxyPort = 7890;
|
||||||
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
|
// HttpHost proxy = new HttpHost(proxyHost, proxyPort);
|
||||||
RequestConfig proxyConfig = RequestConfig.custom()
|
// RequestConfig proxyConfig = RequestConfig.custom()
|
||||||
.setProxy(proxy)
|
// .setProxy(proxy)
|
||||||
.build();
|
// .build();
|
||||||
HttpPost loginPost = createLoginRequest(code,loginInfoParam);
|
HttpPost loginPost = createLoginRequest(code,loginInfoParam);
|
||||||
// 新增代码:将代理类放入配置中
|
// // 新增代码:将代理类放入配置中
|
||||||
loginPost.setConfig(proxyConfig);
|
// loginPost.setConfig(proxyConfig);
|
||||||
try (CloseableHttpResponse loginResponse = httpClient.execute(loginPost)) {
|
try (CloseableHttpResponse loginResponse = httpClient.execute(loginPost)) {
|
||||||
return processLoginResponse(loginResponse, cookieStore);
|
return processLoginResponse(loginResponse, cookieStore);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user