Skip to content

Commit

Permalink
refactor: og 데이터가 없는 경우 name 속성 데이터 사용하도록 변경 (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
sangwonsheep authored Dec 29, 2024
1 parent dfe4eff commit 9942e78
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,23 @@ public Map<String, String> 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);
}
Expand Down

0 comments on commit 9942e78

Please sign in to comment.