Skip to content

Commit

Permalink
Refactor: 유튜브 댓글 크롤링 API response 값 수정(시간, 총 댓글 수 추가)
Browse files Browse the repository at this point in the history
  • Loading branch information
yxhwxn committed Aug 12, 2024
1 parent d593e34 commit db3407e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cmc.suppin.event.crawl.controller;

import com.cmc.suppin.event.crawl.controller.dto.CrawlResponseDTO;
import com.cmc.suppin.event.crawl.service.CrawlService;
import com.cmc.suppin.global.response.ApiResponse;
import com.cmc.suppin.global.response.ResponseCode;
Expand Down Expand Up @@ -51,9 +52,9 @@ public ResponseEntity<ApiResponse<String>> checkExistingComments(@RequestParam("
"크롤링하려는 URL이 중복되지 않았을 때의 요청이기 때문에, 새로운 댓글을 크롤링합니다. <br>" +
"- DB에 기존 댓글이 존재하는 경우: 크롤링을 중지하고 예외를 던집니다. <br>" +
"- DB에 기존 댓글이 존재하지 않는 경우: 새로운 댓글을 크롤링하고 이를 DB에 저장합니다.")
public ResponseEntity<ApiResponse<String>> crawlYoutubeComments(@RequestParam("url") String url, @RequestParam("eventId") Long eventId, @RequestParam("forceUpdate") boolean forceUpdate, @CurrentAccount Account account) {
crawlService.crawlYoutubeComments(url, eventId, account.userId(), forceUpdate);
return ResponseEntity.ok(ApiResponse.of(ResponseCode.SUCCESS, "댓글 수집이 완료되었습니다."));
public ResponseEntity<ApiResponse<CrawlResponseDTO.CrawlResultDTO>> crawlYoutubeComments(@RequestParam("url") String url, @RequestParam("eventId") Long eventId, @RequestParam("forceUpdate") boolean forceUpdate, @CurrentAccount Account account) {
CrawlResponseDTO.CrawlResultDTO crawlResultDTO = crawlService.crawlYoutubeComments(url, eventId, account.userId(), forceUpdate);
return ResponseEntity.ok(ApiResponse.of(ResponseCode.SUCCESS, crawlResultDTO));
}

// @GetMapping("/count")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public class CrawlResponseDTO {
@NoArgsConstructor
@AllArgsConstructor
public static class CrawlResultDTO {
private String author;
private String commentText;
private String date;
private String crawlingDate;
private int totalCommentCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cmc.suppin.event.crawl.controller.dto.CommentRequestDTO;
import com.cmc.suppin.event.crawl.controller.dto.CommentResponseDTO;
import com.cmc.suppin.event.crawl.controller.dto.CrawlResponseDTO;
import com.cmc.suppin.event.crawl.domain.Comment;
import com.cmc.suppin.event.events.domain.Event;

Expand Down Expand Up @@ -56,5 +57,12 @@ public static CommentResponseDTO.WinnerResponseDTO toWinnerResponseDTO(List<Comm
.winners(winnerDetails)
.build();
}

public static CrawlResponseDTO.CrawlResultDTO toCrawlResultDTO(LocalDateTime crawlingDate, int totalCommentCount) {
return CrawlResponseDTO.CrawlResultDTO.builder()
.crawlingDate(crawlingDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.totalCommentCount(totalCommentCount)
.build();
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cmc.suppin.event.crawl.service;

import com.cmc.suppin.event.crawl.controller.dto.CrawlResponseDTO;
import com.cmc.suppin.event.crawl.converter.CommentConverter;
import com.cmc.suppin.event.crawl.converter.DateConverter;
import com.cmc.suppin.event.crawl.domain.Comment;
Expand Down Expand Up @@ -52,7 +53,7 @@ public String checkExistingComments(String url, String userId) {
return null;
}

public void crawlYoutubeComments(String url, Long eventId, String userId, boolean forceUpdate) {
public CrawlResponseDTO.CrawlResultDTO crawlYoutubeComments(String url, Long eventId, String userId, boolean forceUpdate) {
Member member = memberRepository.findByUserIdAndStatusNot(userId, UserStatus.DELETED)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));

Expand Down Expand Up @@ -144,6 +145,7 @@ public void crawlYoutubeComments(String url, Long eventId, String userId, boolea
} finally {
driver.quit();
}
return CommentConverter.toCrawlResultDTO(LocalDateTime.now(), uniqueComments.size());
}
}

0 comments on commit db3407e

Please sign in to comment.