-
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.
* feat(event): event 도메인 생성 * feat(event): event url 정의 * feat(event): event 정보 크롤러 구현 * feat(event): event 정보 추가 * refactor(event): scheduler cron 매주 월요일 5시로 변경
- Loading branch information
Showing
6 changed files
with
166 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.sickgyun.server.event.domain; | ||
|
||
import java.time.YearMonth; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Event { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String image; | ||
|
||
private String name; | ||
|
||
private String host; | ||
|
||
private String date; | ||
|
||
private String hashTags; | ||
|
||
private int year; | ||
|
||
private int month; | ||
|
||
public Event(String image, String name, String host, String date, String hashTags, YearMonth yearMonth) { | ||
this.image = image; | ||
this.name = name; | ||
this.host = host; | ||
this.date = date; | ||
this.hashTags = hashTags; | ||
this.year = yearMonth.getYear(); | ||
this.month = yearMonth.getMonth().getValue(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/sickgyun/server/event/domain/repository/EventRepository.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,8 @@ | ||
package com.sickgyun.server.event.domain.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.sickgyun.server.event.domain.Event; | ||
|
||
public interface EventRepository extends JpaRepository<Event, Long> { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sickgyun/server/event/exception/EventUrlConnectingError.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,11 @@ | ||
package com.sickgyun.server.event.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.sickgyun.server.common.exception.SickgyunException; | ||
|
||
public class EventUrlConnectingError extends SickgyunException { | ||
public EventUrlConnectingError() { | ||
super(HttpStatus.INTERNAL_SERVER_ERROR, "이벤트 정보 크롤링 도중 서버 연결에 실패하였습니다."); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/com/sickgyun/server/event/scheduler/EventScheduler.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,97 @@ | ||
package com.sickgyun.server.event.scheduler; | ||
|
||
import java.io.IOException; | ||
import java.time.YearMonth; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.sickgyun.server.event.domain.Event; | ||
import com.sickgyun.server.event.domain.repository.EventRepository; | ||
import com.sickgyun.server.event.exception.EventUrlConnectingError; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class EventScheduler { | ||
|
||
@Value("${event.url}") | ||
private String url; | ||
private final EventRepository eventRepository; | ||
|
||
@Scheduled(cron = "0 0 5 ? * MON") // 매주 월요일 5시 | ||
@Transactional | ||
public void getReqruitInformation() { | ||
YearMonth now = YearMonth.now(); | ||
YearMonth nextMonth = now.plusMonths(1); | ||
|
||
List<Event> thisMonthEvent = getEventsByMonth(now); | ||
List<Event> nextMonthEvent = getEventsByMonth(nextMonth); | ||
|
||
List<Event> allEvents = Stream.of(thisMonthEvent, nextMonthEvent) | ||
.flatMap(Collection::stream) | ||
.toList(); | ||
|
||
eventRepository.deleteAll(); | ||
eventRepository.saveAll(allEvents); | ||
} | ||
|
||
private List<Event> getEventsByMonth(YearMonth date) { | ||
Document document = connectToServer(date); | ||
Elements rawEvents = getRawEvents(document); | ||
return getAllEvents(rawEvents, date); | ||
} | ||
|
||
private static List<Event> getAllEvents(Elements rawEvents, YearMonth yearMonth) { | ||
List<Event> events = new ArrayList<>(); | ||
|
||
for (Element rawReqruit : rawEvents) { | ||
String imageSrc = "https://dev-event.vercel.app/" + rawReqruit.select("img").get(2).attr("src"); | ||
String name = rawReqruit.getElementsByClass("Item_item__content__title___fPQa").text(); | ||
String host = rawReqruit.getElementsByClass("Item_host__zNXMy").text(); | ||
String date = rawReqruit.getElementsByClass("Item_date__kVMJZ").text(); | ||
String hashtags = rawReqruit.getElementsByClass("Item_tags___ujeV").text() | ||
.replace(" ", "") | ||
.replace("#", " #") | ||
.substring(1); | ||
|
||
events.add( | ||
new Event( | ||
imageSrc, | ||
name, | ||
host, | ||
date, | ||
hashtags, | ||
yearMonth | ||
) | ||
); | ||
|
||
} | ||
return events; | ||
} | ||
|
||
private static Elements getRawEvents(Document document) { | ||
return document.getElementsByClass("Item_item__86e_I"); | ||
} | ||
|
||
private Document connectToServer(YearMonth date) { | ||
Document document; | ||
try { | ||
document = Jsoup.connect(String.format(url, date.getYear(), date.getMonth().getValue())).get(); | ||
} catch (IOException e) { | ||
throw new EventUrlConnectingError(); | ||
} | ||
return document; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -38,4 +38,9 @@ jwt: | |
--- | ||
#Reqruit | ||
reqruit: | ||
url: ${REQRUIT_URL} | ||
|
||
--- | ||
#event | ||
event: | ||
url: ${REQRUIT_URL} |
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 |
---|---|---|
|
@@ -24,4 +24,7 @@ jwt: | |
refresh-token-expiration-time: 15 | ||
|
||
reqruit: | ||
url: hahathisisurl | ||
|
||
event: | ||
url: hahathisisurl |