定时任务
This commit is contained in:
65
src/main/java/com/tem/bocai/schedules/CrawlerSchedule.java
Normal file
65
src/main/java/com/tem/bocai/schedules/CrawlerSchedule.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package com.tem.bocai.schedules;
|
||||||
|
|
||||||
|
import com.tem.bocai.entity.LoginInfoResult;
|
||||||
|
import com.tem.bocai.repository.LoginInfoRepository;
|
||||||
|
import com.tem.bocai.util.*;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import us.codecraft.webmagic.Spider;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class CrawlerSchedule {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TokenCacheService tokenCacheService;
|
||||||
|
@Autowired
|
||||||
|
private LoginInfoRepository loginInfoRepository;
|
||||||
|
// 每天凌晨2点执行爬取开奖结果
|
||||||
|
@Scheduled(cron = "0 0 2 * * ?")
|
||||||
|
// 每7秒执行一次爬取开奖结果
|
||||||
|
//@Scheduled(cron = "*/7 * * * * ?")
|
||||||
|
public void executeLotteryDraw() {
|
||||||
|
System.out.println("开始爬取开奖结果...");
|
||||||
|
String token = tokenCacheService.getToken();
|
||||||
|
System.out.println("得到token = " + token);
|
||||||
|
if (token != null) {
|
||||||
|
LoginInfoResult firstByOrderByCreateTimeDesc = loginInfoRepository.findFirstByOrderByCreateTimeDesc()
|
||||||
|
.orElse(null);// 2. 创建爬虫实例,传入token
|
||||||
|
LotteryWebMagicCrawler crawler = new LotteryWebMagicCrawler(token);
|
||||||
|
String YesterdayDate = DateUtils.getYesterdayDate();// 4. 执行爬虫
|
||||||
|
String url = firstByOrderByCreateTimeDesc.getLoginUrl()+"/member/dresult?lottery=SGFT&date="+YesterdayDate;
|
||||||
|
Spider.create(crawler)
|
||||||
|
.addUrl(url)
|
||||||
|
.thread(1)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 每7秒执行一次爬取今日已经结算
|
||||||
|
//@Scheduled(cron = "*/7 * * * * ?")
|
||||||
|
public void executeSettlement() {
|
||||||
|
String token = tokenCacheService.getToken();
|
||||||
|
System.out.println("得到token = " + token);
|
||||||
|
if (token != null && !token.isEmpty()) {
|
||||||
|
// 2. 创建爬虫实例,传入token
|
||||||
|
CompletedTodayCrawler crawler = new CompletedTodayCrawler(token);
|
||||||
|
LoginInfoResult firstByOrderByCreateTimeDesc = loginInfoRepository.findFirstByOrderByCreateTimeDesc()
|
||||||
|
.orElse(null);
|
||||||
|
// 4. 执行爬虫
|
||||||
|
String url = firstByOrderByCreateTimeDesc.getLoginUrl()+"/member/bets?settled=true";
|
||||||
|
Spider.create(crawler)
|
||||||
|
.addUrl(url)
|
||||||
|
.thread(1)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -46,7 +46,7 @@ public class LoginServiceImpl implements LoginService {
|
|||||||
// 3. 创建数据处理器
|
// 3. 创建数据处理器
|
||||||
LotteryDataPipeline pipeline = new LotteryDataPipeline();
|
LotteryDataPipeline pipeline = new LotteryDataPipeline();
|
||||||
// 4. 执行爬虫
|
// 4. 执行爬虫
|
||||||
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-01-21";
|
||||||
|
|
||||||
Spider.create(crawler)
|
Spider.create(crawler)
|
||||||
.addUrl(url)
|
.addUrl(url)
|
||||||
|
|||||||
58
src/main/java/com/tem/bocai/util/DateUtils.java
Normal file
58
src/main/java/com/tem/bocai/util/DateUtils.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package com.tem.bocai.util;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.*;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 时间工具类
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
||||||
|
{
|
||||||
|
public static String YYYY = "yyyy";
|
||||||
|
|
||||||
|
public static String YYYY_MM = "yyyy-MM";
|
||||||
|
|
||||||
|
public static String YYYY_MM_DD = "yyyy-MM-dd";
|
||||||
|
|
||||||
|
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
||||||
|
|
||||||
|
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取昨天的日期,格式为 yyyy-MM-dd
|
||||||
|
*
|
||||||
|
* @return 昨天日期的字符串,例如:2026-01-18
|
||||||
|
*/
|
||||||
|
public static String getYesterdayDate() {
|
||||||
|
LocalDate yesterday = LocalDate.now().minusDays(1);
|
||||||
|
return yesterday.format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<String> test = new ArrayList<>();
|
||||||
|
test.add("12312");
|
||||||
|
test.add("12312");
|
||||||
|
System.out.println("====="+test.get(6));
|
||||||
|
/* Date now = new Date(); // 当前时间
|
||||||
|
Date fifteenMinutesAgo = DateUtil.offsetMinute(now, -15); // 15分钟前的时间
|
||||||
|
|
||||||
|
log.info("当前时间: {}", DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
|
||||||
|
log.info("15分钟前的时间: {}", DateUtil.format(fifteenMinutesAgo, "yyyy-MM-dd HH:mm:ss"));*/
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -67,12 +67,16 @@ public class TokenCacheService {
|
|||||||
public String getToken() {
|
public String getToken() {
|
||||||
Cache cache = tokenCacheManager.getCache(TOKEN_CACHE_NAME);
|
Cache cache = tokenCacheManager.getCache(TOKEN_CACHE_NAME);
|
||||||
if (cache != null) {
|
if (cache != null) {
|
||||||
return cache.get(TOKEN_KEY, String.class);
|
String token = cache.get(TOKEN_KEY, String.class);
|
||||||
} else {
|
if (token != null && !token.isEmpty()) {
|
||||||
String token = getTokenSqlite();
|
System.out.println("从缓存获取到token");
|
||||||
System.out.println("重新登入获取的token==" + token);
|
return token;
|
||||||
return token;
|
}
|
||||||
}
|
}
|
||||||
|
// 缓存不存在或token为空时,从SQLite获取
|
||||||
|
String token = getTokenSqlite();
|
||||||
|
System.out.println("重新登入获取的token==" + token);
|
||||||
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,8 +142,9 @@ public class TokenCacheService {
|
|||||||
loginInfoParam.setPassword(firstByOrderByCreateTimeDesc.getPassword());
|
loginInfoParam.setPassword(firstByOrderByCreateTimeDesc.getPassword());
|
||||||
loginInfoParam.setLoginUrl(firstByOrderByCreateTimeDesc.getLoginUrl());
|
loginInfoParam.setLoginUrl(firstByOrderByCreateTimeDesc.getLoginUrl());
|
||||||
token = attemptLogin(loginInfoParam);
|
token = attemptLogin(loginInfoParam);
|
||||||
System.out.println("2token = " + token);
|
|
||||||
if (token != null && !token.isEmpty()) {
|
if (token != null && !token.isEmpty()) {
|
||||||
|
//保存缓存token
|
||||||
|
saveToken(token);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
if (attempt < MAX_RETRY) {
|
if (attempt < MAX_RETRY) {
|
||||||
|
|||||||
Reference in New Issue
Block a user