Skip to content

Commit

Permalink
Refactor: 불필요한 메서드 및 예외 처리 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
suminnnnn committed Oct 14, 2024
1 parent 49e4164 commit ca96f45
Showing 1 changed file with 12 additions and 35 deletions.
47 changes: 12 additions & 35 deletions src/main/java/cmc/blink/domain/link/business/LinkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,41 +431,18 @@ private String getOpenGraphContent(OpenGraph openGraph, String property) {
}

private LinkResponse.LinkInfo fetchLinkInfoWithJsoup(String url) throws IOException {
try {
Document doc = Jsoup.connect(url)
.userAgent(getRandomUserAgent())
.followRedirects(false)
.get();

String title = doc.title();
String type = doc.select("meta[name=type]").attr("content");
String contents = doc.select("meta[name=description]").attr("content");
String imageUrl = doc.select("meta[property=og:image]").attr("content");

return LinkMapper.toLinkInfo(title, type, contents, imageUrl);
} catch (UnsupportedMimeTypeException e) {
return fetchLinkInfoWithBinary(url);
}

}

private LinkResponse.LinkInfo fetchLinkInfoWithBinary(String url) throws IOException {
try {
Connection.Response response = Jsoup.connect(url)
.ignoreContentType(true)
.execute();

String htmlContent = new String(response.bodyAsBytes(), StandardCharsets.UTF_8);
Document doc = Jsoup.parse(htmlContent);

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

return LinkMapper.toLinkInfo(title, "", contents, imageUrl);
} catch (IOException e) {
throw new LinkException(ErrorCode.LINK_SCRAPED_FAILED);
}
Document doc = Jsoup.connect(url)
.userAgent(getRandomUserAgent())
.ignoreContentType(true)
.followRedirects(false)
.get();

String title = doc.title();
String type = doc.select("meta[name=type]").attr("content");
String contents = doc.select("meta[name=description]").attr("content");
String imageUrl = doc.select("meta[property=og:image]").attr("content");

return LinkMapper.toLinkInfo(title, type, contents, imageUrl);
}

@Transactional
Expand Down

0 comments on commit ca96f45

Please sign in to comment.