-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Oh-Lottery/crawler2
Auto Crawler 완성
- Loading branch information
Showing
14 changed files
with
375 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/ohlottery/controller/LotteryCrawlerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.ohlottery.controller; | ||
|
||
import com.ohlottery.service.DHLotteryCrawlerService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
//Authentication 과정 필수로 필요 | ||
|
||
@RestController | ||
@RequestMapping("/crawler") | ||
@RequiredArgsConstructor | ||
public class LotteryCrawlerController { | ||
|
||
private final DHLotteryCrawlerService lotteryCrawlerService; | ||
|
||
@PatchMapping("/lottery645/{round}") | ||
public ResponseEntity<Void> fetchLottery645(@PathVariable long round) { | ||
lotteryCrawlerService.fetchLottery645Data(round); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PatchMapping("/lottery720/{round}") | ||
public ResponseEntity<Void> fetchLottery720(@PathVariable long round) { | ||
lotteryCrawlerService.fetchLottery720Data(round); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
package com.ohlottery.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.*; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Entity | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Lottery720Entity { | ||
|
||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Id | ||
private long round;// 회차 | ||
|
||
private LocalDate drawDate;// 추첨 날짜 | ||
|
||
private short rankWinNum;// 등수 | ||
private byte rankWinNum;// 등수 | ||
private byte rankClass;// 조 번호 | ||
private int rankNo;// 당첨 번호 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/com/ohlottery/scheduler/LotteryCrawlerScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.ohlottery.scheduler; | ||
|
||
import com.ohlottery.service.DHLotteryCrawlerService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class LotteryCrawlerScheduler { | ||
|
||
private final DHLotteryCrawlerService lotteryCrawlerService; | ||
|
||
@Scheduled(cron = "0 35 20 * * 6") // 매주 토요일 20:35 | ||
public void scheduleFetchLottery645Data() { | ||
log.info("Scheduled Task: fetchLottery645Data 실행 중"); | ||
try { | ||
int latestRound = getLatestLottery645Round(); | ||
log.info("가져온 최신 6/45 로또 회차: {}", latestRound); | ||
scheduleRetryFetch(() -> lotteryCrawlerService.fetchLottery645Data(latestRound), "6/45", latestRound); | ||
} catch (Exception e) { | ||
log.error("fetchLottery645Data 실행 중 에러 발생", e); | ||
} | ||
} | ||
|
||
@Scheduled(cron = "0 5 19 * * 4") // 매주 목요일 19:05 | ||
public void scheduleFetchLottery720Data() { | ||
log.info("Scheduled Task: fetchLottery720Data 실행 중"); | ||
try { | ||
int latestRound = getLatestLottery720Round(); | ||
log.info("가져온 최신 720 로또 회차: {}", latestRound); | ||
scheduleRetryFetch(() -> lotteryCrawlerService.fetchLottery720Data(latestRound), "720", latestRound); | ||
} catch (Exception e) { | ||
log.error("fetchLottery720Data 실행 중 에러 발생", e); | ||
} | ||
} | ||
|
||
private void scheduleRetryFetch(Runnable fetchTask, String lotteryType, int round) { | ||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); | ||
final int[] retryCount = {0}; // 재시도 횟수 | ||
final int maxRetries = 12; // 최대 12번 재시도 (5분 x 12 = 1시간) | ||
|
||
executor.scheduleAtFixedRate(() -> { | ||
try { | ||
log.info("재시도 실행 중: {} 로또 회차 {} | 시도 횟수: {}/{}", lotteryType, round, retryCount[0] + 1, maxRetries); | ||
fetchTask.run(); | ||
log.info("재시도 성공: {} 로또 회차 {}", lotteryType, round); | ||
executor.shutdown(); // 성공하면 재시도 종료 | ||
} catch (Exception e) { | ||
retryCount[0]++; | ||
log.error("재시도 실패: {} 로또 회차 {} | 시도 횟수: {}/{}", lotteryType, round, retryCount[0], maxRetries, e); | ||
if (retryCount[0] >= maxRetries) { | ||
log.error("재시도 횟수 초과로 중단: {} 로또 회차 {}", lotteryType, round); | ||
executor.shutdown(); // 최대 재시도 횟수 초과 시 종료 | ||
} | ||
} | ||
}, 0, 5, TimeUnit.MINUTES); // 즉시 실행, 이후 5분 간격 | ||
} | ||
|
||
private int getLatestLottery645Round() { | ||
try { | ||
int latestRound = lotteryCrawlerService.getLatest645Round(); | ||
return latestRound + 1; | ||
} catch (Exception e) { | ||
log.error("6/45 최신 회차를 가져오는 중 에러 발생", e); | ||
return -1; | ||
} | ||
} | ||
|
||
private int getLatestLottery720Round() { | ||
try { | ||
int latestRound = lotteryCrawlerService.getLatest720Round(); | ||
return latestRound + 1; | ||
} catch (Exception e) { | ||
log.error("720 최신 회차를 가져오는 중 에러 발생", e); | ||
return -1; | ||
} | ||
} | ||
} |
Oops, something went wrong.