Skip to content

Commit

Permalink
Feat: 브런치 링크 스크래핑 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
suminnnnn committed Oct 14, 2024
1 parent f0c9453 commit 49e4164
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/cmc/blink/domain/link/business/LinkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public LinkResponse.LinkCreateDto saveLink(LinkRequest.LinkCreateDto createDto,
case "blog.naver.com" -> fetchNaverBlogLinkInfo(url);
case "cafe.naver.com" -> fetchNaverCafeLinkInfo(url);
case "x.com" -> fetchTwitterLinkInfo(url);
case "brunch.co.kr" -> fetchBrunchLinkInfo(url);
default -> fetchLinkInfo(url);
};

Expand Down Expand Up @@ -397,6 +398,32 @@ private LinkResponse.LinkInfo fetchTwitterLinkInfo(String url) {
}
}

private LinkResponse.LinkInfo fetchBrunchLinkInfo(String url) {
try {
Document doc = Jsoup.connect(url).get();

System.out.printf("<<<<brunch link scrapping result HTML>>>>\n\n\n %s\n\n\n%n", doc);

String title = doc.select("meta[property=og:title]").attr("content");

if (title.isEmpty()) {
title = doc.title();
}

String contents = doc.select("meta[property=og:description]").attr("content");
if (contents.isEmpty()) {
contents = doc.select("meta[name=description]").attr("content");
}

String imageUrl = doc.select("meta[property=og:image]").attr("content");

return LinkMapper.toLinkInfo(title, "Brunch", contents, imageUrl);

} catch (IOException e) {
throw new LinkException(ErrorCode.LINK_SCRAPED_FAILED);
}
}

private String getOpenGraphContent(OpenGraph openGraph, String property) {
return Optional.ofNullable(openGraph.getContent(property))
.map(HtmlUtils::htmlUnescape)
Expand Down

0 comments on commit 49e4164

Please sign in to comment.