Skip to content

Commit

Permalink
[FEAT] 크롤링 스케줄러 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SolfE committed Oct 21, 2024
1 parent a45d2c2 commit 79b395a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/catspot/CatspotApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/catspot/crawler/CrawlerScheduler.java
Original file line number Diff line number Diff line change
@@ -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<StudyPlace> places = LibraryCrawler.getData();
List<StudyPlace> 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);
}
}
4 changes: 1 addition & 3 deletions src/main/java/com/catspot/crawler/LibraryCrawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static List<StudyPlace> getData() {
Integer restSeats = Integer.parseInt(seatStatus.get(7).text());

StudyPlace studyPlace = StudyPlace.builder()
.placeIdx(i)
.placeIdx((long) i)
.placeName(name)
.url(url)
.allSeats(allSeats)
Expand All @@ -45,9 +45,7 @@ public static List<StudyPlace> getData() {
}

if (data.isEmpty()) throw new IOException();
System.out.println("크롤링 성공 : 데이터 개수 " + data.size());
} catch (IOException e) {
System.out.println("에러 발생");
e.printStackTrace();
}
return data;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/catspot/crawler/StudyPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Builder
public class StudyPlace {
@Id
private Integer placeIdx;
private Long placeIdx;
private String placeName;
private String url;
private Integer allSeats;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StudyPlaceRepositoryTest {
@Test
void 저장_후_인덱스를_통한_조회() {
StudyPlace studyPlace = StudyPlace.builder()
.placeIdx(1)
.placeIdx(1L)
.placeName("1")
.url("url")
.allSeats(10)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/catspot/crawler/StudyPlaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class StudyPlaceTest {
@Test
void 열람실_엔티티_생성() {
StudyPlace studyPlace = StudyPlace.builder()
.placeIdx(1)
.placeIdx(1L)
.placeName("1")
.url("url")
.allSeats(10)
Expand Down

0 comments on commit 79b395a

Please sign in to comment.