-
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.
Merge pull request #25 from Central-MakeUs/feature/15
Feature/15: 댓글수집 이벤트 관련 API 구현-1
- Loading branch information
Showing
40 changed files
with
471 additions
and
19 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
9 changes: 8 additions & 1 deletion
9
src/main/java/com/cmc/suppin/answer/domain/AnonymousParticipant.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
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
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
Empty file.
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions
19
src/main/java/com/cmc/suppin/event/crawl/controller/CommentApi.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,19 @@ | ||
package com.cmc.suppin.event.crawl.controller; | ||
|
||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Validated | ||
@Tag(name = "Event-Comments", description = "Crawling Comments 관련 API") | ||
@RequestMapping("/api/v1/comments") | ||
public class CommentApi { | ||
|
||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/cmc/suppin/event/crawl/controller/CrawlApi.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,34 @@ | ||
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; | ||
import com.cmc.suppin.global.security.reslover.Account; | ||
import com.cmc.suppin.global.security.reslover.CurrentAccount; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Validated | ||
@Tag(name = "Crawling", description = "Crawling 관련 API") | ||
@RequestMapping("/api/v1/event") | ||
public class CrawlApi { | ||
|
||
private final CrawlService crawlService; | ||
|
||
@GetMapping("/crawling/youtube/comments") | ||
public ResponseEntity<ApiResponse<CrawlResponseDTO.CrawlResultDTO>> crawlYoutubeComments(@RequestParam String url, @CurrentAccount Account account) { | ||
crawlService.crawlYoutubeComments(url, account.userId()); | ||
return ResponseEntity.ok(ApiResponse.of(ResponseCode.SUCCESS)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/cmc/suppin/event/crawl/controller/dto/CrawlResponseDTO.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,19 @@ | ||
package com.cmc.suppin.event.crawl.controller.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class CrawlResponseDTO { | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class CrawlResultDTO { | ||
private String author; | ||
private String commentText; | ||
private String date; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/cmc/suppin/event/crawl/converter/CommentConverter.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,4 @@ | ||
package com.cmc.suppin.event.crawl.converter; | ||
|
||
public class CommentConverter { | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/cmc/suppin/event/crawl/domain/repository/CommentRepository.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,12 @@ | ||
package com.cmc.suppin.event.crawl.domain.repository; | ||
|
||
import com.cmc.suppin.event.crawl.domain.Comment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
List<Comment> findByEventId(Long eventId); | ||
|
||
List<Comment> findByVideoUrl(String videoUrl); | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.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,91 @@ | ||
package com.cmc.suppin.event.crawl.service; | ||
|
||
import com.cmc.suppin.event.crawl.controller.dto.CrawlResponseDTO; | ||
import com.cmc.suppin.event.crawl.domain.Comment; | ||
import com.cmc.suppin.event.crawl.domain.repository.CommentRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
import org.openqa.selenium.JavascriptExecutor; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.chrome.ChromeOptions; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class CrawlService { | ||
|
||
private final CommentRepository commentRepository; | ||
|
||
public List<CrawlResponseDTO.CrawlResultDTO> crawlYoutubeComments(String url, String userId) { | ||
System.setProperty("webdriver.chrome.driver", "src/main/resources/drivers/chromedriver"); | ||
|
||
ChromeOptions options = new ChromeOptions(); | ||
options.addArguments("--disable-gpu"); | ||
options.addArguments("--no-sandbox"); | ||
options.addArguments("--disable-dev-shm-usage"); | ||
options.addArguments("--remote-allow-origins=*"); | ||
|
||
WebDriver driver = new ChromeDriver(options); | ||
driver.get(url); | ||
|
||
List<CrawlResponseDTO.CrawlResultDTO> commentList = new ArrayList<>(); | ||
Set<String> uniqueComments = new HashSet<>(); | ||
|
||
try { | ||
Thread.sleep(5000); | ||
|
||
long endTime = System.currentTimeMillis() + 120000; | ||
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; | ||
|
||
while (System.currentTimeMillis() < endTime) { | ||
jsExecutor.executeScript("window.scrollTo(0, document.documentElement.scrollHeight);"); | ||
Thread.sleep(1000); | ||
|
||
String pageSource = driver.getPageSource(); | ||
Document doc = Jsoup.parse(pageSource); | ||
Elements comments = doc.select("ytd-comment-thread-renderer"); | ||
|
||
for (Element commentElement : comments) { | ||
String author = commentElement.select("#author-text span").text(); | ||
String text = commentElement.select("#content yt-attributed-string#content-text").text(); | ||
String time = commentElement.select("#header-author #published-time-text").text().replace("(수정됨)", ""); | ||
|
||
if (!uniqueComments.contains(text)) { | ||
uniqueComments.add(text); | ||
commentList.add(new CrawlResponseDTO.CrawlResultDTO(author, text, time)); | ||
|
||
// 엔티티 저장 | ||
Comment comment = Comment.builder() | ||
.author(author) | ||
.commentText(text) | ||
.commentDate(time) | ||
.url(url) | ||
.build(); | ||
commentRepository.save(comment); | ||
} | ||
} | ||
} | ||
|
||
return commentList; | ||
|
||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
return new ArrayList<>(); | ||
} finally { | ||
driver.quit(); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/cmc/suppin/event/events/controller/EventApi.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,37 @@ | ||
package com.cmc.suppin.event.events.controller; | ||
|
||
import com.cmc.suppin.event.events.controller.dto.EventRequestDTO; | ||
import com.cmc.suppin.event.events.service.EventService; | ||
import com.cmc.suppin.global.response.ApiResponse; | ||
import com.cmc.suppin.global.response.ResponseCode; | ||
import com.cmc.suppin.global.security.reslover.Account; | ||
import com.cmc.suppin.global.security.reslover.CurrentAccount; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Validated | ||
@Tag(name = "Event", description = "Event 관련 API") | ||
@RequestMapping("/api/v1/event") | ||
public class EventApi { | ||
|
||
private final EventService eventService; | ||
|
||
@PostMapping("/new") | ||
@Operation(summary = "댓글 이벤트 생성 API", description = "request : eventType, title, url, startDate, endDate") | ||
public ResponseEntity<ApiResponse<Void>> createEvent(@RequestBody @Valid EventRequestDTO.CommentEventCreateDTO request, @CurrentAccount Account account) { | ||
eventService.createEvent(request, account.userId()); | ||
return ResponseEntity.ok(ApiResponse.of(ResponseCode.SUCCESS)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/cmc/suppin/event/events/controller/dto/EventRequestDTO.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,23 @@ | ||
package com.cmc.suppin.event.events.controller.dto; | ||
|
||
import com.cmc.suppin.global.enums.EventType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class EventRequestDTO { | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public static class CommentEventCreateDTO { | ||
private EventType type; | ||
private String title; | ||
private String url; | ||
private String startDate; | ||
private String endDate; | ||
private String announcementDate; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/cmc/suppin/event/events/controller/dto/EventResponseDTO.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,24 @@ | ||
package com.cmc.suppin.event.events.controller.dto; | ||
|
||
import com.cmc.suppin.global.enums.EventType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class EventResponseDTO { | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class CommentEventDetailDTO { | ||
|
||
private EventType type; | ||
private String title; | ||
private String url; | ||
private String startDate; | ||
private String endDate; | ||
private String announcementDate; | ||
} | ||
} |
Oops, something went wrong.