diff --git a/src/main/java/com/catspot/CatspotApplication.java b/src/main/java/com/catspot/CatspotApplication.java index 2ca38ce..432c7e8 100644 --- a/src/main/java/com/catspot/CatspotApplication.java +++ b/src/main/java/com/catspot/CatspotApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class CatspotApplication { public static void main(String[] args) { diff --git a/src/main/java/com/catspot/crawler/CrawlerScheduler.java b/src/main/java/com/catspot/crawler/CrawlerScheduler.java new file mode 100644 index 0000000..739e713 --- /dev/null +++ b/src/main/java/com/catspot/crawler/CrawlerScheduler.java @@ -0,0 +1,36 @@ +package com.catspot.crawler; + +import lombok.AllArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Component +@AllArgsConstructor +public class CrawlerScheduler { + private final StudyPlaceRepository studyPlaceRepository; + + @Transactional + @Scheduled(fixedRate = 60000) + public void crawl() { + List places = LibraryCrawler.getData(); + List studyPlacesToSave = new ArrayList<>(); + + for (StudyPlace place : places) { + StudyPlace studyPlace = studyPlaceRepository.findById(place.getPlaceIdx()).orElseGet(() -> place); + + studyPlace.setPlaceName(place.getPlaceName()); + studyPlace.setUrl(place.getUrl()); + studyPlace.setAllSeats(place.getAllSeats()); + studyPlace.setUseSeats(place.getUseSeats()); + studyPlace.setRestSeats(place.getRestSeats()); + + studyPlacesToSave.add(studyPlace); + } + + studyPlaceRepository.saveAll(studyPlacesToSave); + } +} diff --git a/src/main/java/com/catspot/crawler/LibraryCrawler.java b/src/main/java/com/catspot/crawler/LibraryCrawler.java index 350b2a3..6613849 100644 --- a/src/main/java/com/catspot/crawler/LibraryCrawler.java +++ b/src/main/java/com/catspot/crawler/LibraryCrawler.java @@ -33,7 +33,7 @@ public static List getData() { Integer restSeats = Integer.parseInt(seatStatus.get(7).text()); StudyPlace studyPlace = StudyPlace.builder() - .placeIdx(i) + .placeIdx((long) i) .placeName(name) .url(url) .allSeats(allSeats) @@ -45,9 +45,7 @@ public static List getData() { } if (data.isEmpty()) throw new IOException(); - System.out.println("크롤링 성공 : 데이터 개수 " + data.size()); } catch (IOException e) { - System.out.println("에러 발생"); e.printStackTrace(); } return data; diff --git a/src/main/java/com/catspot/crawler/StudyPlace.java b/src/main/java/com/catspot/crawler/StudyPlace.java index 51615d1..820a541 100644 --- a/src/main/java/com/catspot/crawler/StudyPlace.java +++ b/src/main/java/com/catspot/crawler/StudyPlace.java @@ -12,7 +12,7 @@ @Builder public class StudyPlace { @Id - private Integer placeIdx; + private Long placeIdx; private String placeName; private String url; private Integer allSeats; diff --git a/src/test/java/com/catspot/crawler/StudyPlaceRepositoryTest.java b/src/test/java/com/catspot/crawler/StudyPlaceRepositoryTest.java index 363c606..0a08ad0 100644 --- a/src/test/java/com/catspot/crawler/StudyPlaceRepositoryTest.java +++ b/src/test/java/com/catspot/crawler/StudyPlaceRepositoryTest.java @@ -14,7 +14,7 @@ class StudyPlaceRepositoryTest { @Test void 저장_후_인덱스를_통한_조회() { StudyPlace studyPlace = StudyPlace.builder() - .placeIdx(1) + .placeIdx(1L) .placeName("1") .url("url") .allSeats(10) diff --git a/src/test/java/com/catspot/crawler/StudyPlaceTest.java b/src/test/java/com/catspot/crawler/StudyPlaceTest.java index 4f0573d..2a99742 100644 --- a/src/test/java/com/catspot/crawler/StudyPlaceTest.java +++ b/src/test/java/com/catspot/crawler/StudyPlaceTest.java @@ -7,7 +7,7 @@ class StudyPlaceTest { @Test void 열람실_엔티티_생성() { StudyPlace studyPlace = StudyPlace.builder() - .placeIdx(1) + .placeIdx(1L) .placeName("1") .url("url") .allSeats(10)