diff --git a/backend/baguni-api/src/main/java/baguni/api/domain/link/service/LinkService.java b/backend/baguni-api/src/main/java/baguni/api/domain/link/service/LinkService.java index d5fd019c7..b5a40f841 100644 --- a/backend/baguni-api/src/main/java/baguni/api/domain/link/service/LinkService.java +++ b/backend/baguni-api/src/main/java/baguni/api/domain/link/service/LinkService.java @@ -54,12 +54,21 @@ public LinkInfo saveLinkAndUpdateOgTag(String url) { } } + /** + * property 속성에 og 데이터가 없는 경우, name 속성에 있는 데이터 활용 + */ private Link updateOpengraph(String url, Link link) throws OpenGraphException { var openGraph = new OpenGraph(url); link.updateMetadata( - openGraph.getTag(Metadata.TITLE).orElse(""), - openGraph.getTag(Metadata.DESCRIPTION).orElse(""), - correctImageUrl(url, openGraph.getTag(Metadata.IMAGE).orElse("")) + openGraph.getTag(Metadata.OG_TITLE) + .orElse(openGraph.getTag(Metadata.TITLE) + .orElse("")), + openGraph.getTag(Metadata.OG_DESCRIPTION) + .orElse(openGraph.getTag(Metadata.DESCRIPTION) + .orElse("")), + correctImageUrl(url, openGraph.getTag(Metadata.OG_IMAGE) + .orElse(openGraph.getTag(Metadata.IMAGE) + .orElse(""))) ); return link; } diff --git a/backend/baguni-core/src/main/java/baguni/core/lib/opengraph/Metadata.java b/backend/baguni-core/src/main/java/baguni/core/lib/opengraph/Metadata.java index fd86c2954..3eb7e2e57 100644 --- a/backend/baguni-core/src/main/java/baguni/core/lib/opengraph/Metadata.java +++ b/backend/baguni-core/src/main/java/baguni/core/lib/opengraph/Metadata.java @@ -12,14 +12,18 @@ public class Metadata { ***************************************/ // The title of your object as it should appear within the graph, e.g., "The Rock". - public static final MetadataTag TITLE = MetadataTag.of("og:title"); + public static final MetadataTag TITLE = MetadataTag.of("title"); + + public static final MetadataTag OG_TITLE = MetadataTag.of("og:title"); // The type of your object, e.g., "video.movie". // Depending on the type you specify, other properties may also be required. public static final MetadataTag TYPE = MetadataTag.of("og:type"); // An image URL which should represent your object within the graph. - public static final MetadataTag IMAGE = MetadataTag.of("og:image"); + public static final MetadataTag IMAGE = MetadataTag.of("image"); + + public static final MetadataTag OG_IMAGE = MetadataTag.of("og:image"); // The canonical URL of your object that will be used as its permanent ID in the graph, e.g., "https://www.imdb.com/title/tt0117500/". public static final MetadataTag URL = MetadataTag.of("og:url"); @@ -32,7 +36,9 @@ public class Metadata { public static final MetadataTag AUDIO = MetadataTag.of("og:audio"); // A one to two sentence description of your object. - public static final MetadataTag DESCRIPTION = MetadataTag.of("og:description"); + public static final MetadataTag DESCRIPTION = MetadataTag.of("description"); + + public static final MetadataTag OG_DESCRIPTION = MetadataTag.of("og:description"); // The word that appears before this object's title in a sentence. An enum of (a, an, the, "", auto). If auto is // chosen, the consumer of your data should chose between "a" or "an". Default is "" (blank). diff --git a/backend/baguni-core/src/main/java/baguni/core/lib/opengraph/OpenGraphReader.java b/backend/baguni-core/src/main/java/baguni/core/lib/opengraph/OpenGraphReader.java index 537e0266b..77169abc7 100644 --- a/backend/baguni-core/src/main/java/baguni/core/lib/opengraph/OpenGraphReader.java +++ b/backend/baguni-core/src/main/java/baguni/core/lib/opengraph/OpenGraphReader.java @@ -43,16 +43,23 @@ public Map read(URI uri) throws OpenGraphException { Charset.forName(openGraphOption.getHttpResponseDefaultCharsetName()))); Document htmlResponse = Jsoup.parse(response.body()); Element head = htmlResponse.head(); - Elements headClildElements = head.children(); - headClildElements - .stream() - .filter( - o -> "meta".equals(o.nodeName()) && o.hasAttr("property") && o.attr("property").startsWith("og:")) - .forEach(o -> { - String key = o.attr("property"); - String value = o.attr("content"); + + String title = head.select("title").text(); + result.put("title", title); + + Elements metaTags = head.select("meta"); + metaTags.forEach(meta -> { + // og 데이터가 없는 경우, meta name 속성 값(image, description)을 활용한다. + boolean isOgProperty = meta.hasAttr("property") && meta.attr("property").startsWith("og:"); + boolean isOgNameImage = meta.hasAttr("name") && meta.attr("name").contains("image"); + boolean isOgNameDescription = meta.hasAttr("name") && meta.attr("name").contains("description"); + + if (isOgProperty || isOgNameImage || isOgNameDescription) { + String key = isOgProperty ? meta.attr("property") : meta.attr("name"); + String value = meta.attr("content"); result.put(key, value); - }); + } + }); } catch (IOException | InterruptedException e) { throw new OpenGraphException("Error occurred when reading OG tags.", e); }