-
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.
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
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 1 * ?") // 매달 1일 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; | ||
} | ||
} |