Skip to content

Commit

Permalink
Feat: 크롤링 데이터 저장 시, datetime으로 변환되어 저장되도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
yxhwxn committed Aug 7, 2024
1 parent 9c6e70a commit 7430730
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import com.cmc.suppin.event.crawl.domain.Comment;
import com.cmc.suppin.event.events.domain.Event;

import java.time.LocalDateTime;

public class CommentConverter {

public static Comment toCommentEntity(String author, String text, String time, String url, Event event) {
public static Comment toCommentEntity(String author, String text, LocalDateTime commentDate, String url, Event event) {
return Comment.builder()
.author(author)
.commentText(text)
.commentDate(time)
.commentDate(commentDate)
.url(url)
.event(event)
.build();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.cmc.suppin.event.crawl.converter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateConverter {

public static LocalDateTime convertRelativeTime(String relativeTime) {
LocalDateTime now = LocalDateTime.now();
if (relativeTime.contains("일 전")) {
int days = extractNumber(relativeTime);
return now.minusDays(days);
} else if (relativeTime.contains("시간 전")) {
int hours = extractNumber(relativeTime);
return now.minusHours(hours);
} else if (relativeTime.contains("분 전")) {
int minutes = extractNumber(relativeTime);
return now.minusMinutes(minutes);
} else if (relativeTime.contains("주 전")) {
int weeks = extractNumber(relativeTime);
return now.minusWeeks(weeks);
} else if (relativeTime.contains("개월 전")) {
int months = extractNumber(relativeTime);
return now.minusMonths(months);
} else if (relativeTime.contains("년 전")) {
int years = extractNumber(relativeTime);
return now.minusYears(years);
}
return now;
}

private static int extractNumber(String relativeTime) {
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(relativeTime);
if (matcher.find()) {
return Integer.parseInt(matcher.group());
}
return 0;
}

public static String formatLocalDateTime(LocalDateTime dateTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return dateTime.format(formatter);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cmc.suppin.event.crawl.service;

import com.cmc.suppin.event.crawl.converter.CommentConverter;
import com.cmc.suppin.event.crawl.converter.DateConverter;
import com.cmc.suppin.event.crawl.domain.Comment;
import com.cmc.suppin.event.crawl.domain.repository.CommentRepository;
import com.cmc.suppin.event.events.domain.Event;
Expand All @@ -21,6 +22,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -35,18 +37,12 @@ public class CrawlService {
private final MemberRepository memberRepository;

public void crawlYoutubeComments(String url, Long eventId, String userId) {
log.info("Start crawling comments for URL: {} by user: {}", url, userId);

Member member = memberRepository.findByUserIdAndStatusNot(userId, UserStatus.DELETED)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));

log.info("Member found: {}", member.getId());

Event event = eventRepository.findByIdAndMemberId(eventId, member.getId())
.orElseThrow(() -> new IllegalArgumentException("Event not found"));

log.info("Event found: {}", event.getId());

System.setProperty("webdriver.chrome.driver", "src/main/resources/drivers/chromedriver");

ChromeOptions options = new ChromeOptions();
Expand Down Expand Up @@ -84,9 +80,9 @@ public void crawlYoutubeComments(String url, Long eventId, String userId) {
uniqueComments.add(text);

// 엔티티 저장
Comment comment = CommentConverter.toCommentEntity(author, text, time, url, event);
LocalDateTime actualCommentDate = DateConverter.convertRelativeTime(time);
Comment comment = CommentConverter.toCommentEntity(author, text, actualCommentDate, url, event);
commentRepository.save(comment);
log.info("Comment saved: {}", comment.getId());
}
}
}
Expand All @@ -97,3 +93,4 @@ public void crawlYoutubeComments(String url, Long eventId, String userId) {
}
}
}

0 comments on commit 7430730

Please sign in to comment.