From 5972cacdc901eddc84905acf9fa950a1c30c34b1 Mon Sep 17 00:00:00 2001 From: CodingPF Date: Thu, 25 Apr 2024 08:21:36 +0200 Subject: [PATCH] add episode to title --- .../crawler/sender/base/JsonUtils.java | 2 +- .../zdf/json/ZdfFilmDetailDeserializer.java | 75 ++++++++++++------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/main/java/mServer/crawler/sender/base/JsonUtils.java b/src/main/java/mServer/crawler/sender/base/JsonUtils.java index b21495e37..296b87f59 100644 --- a/src/main/java/mServer/crawler/sender/base/JsonUtils.java +++ b/src/main/java/mServer/crawler/sender/base/JsonUtils.java @@ -142,7 +142,7 @@ public static Optional getElementValueAsString(final JsonElement aJsonEl public static Optional getElement(final JsonElement aJsonElement, final String... aElementIds) { Optional rs = Optional.empty(); - if (aElementIds == null || aElementIds.length == 0) { + if (aElementIds == null || aElementIds.length == 0 || aJsonElement == null || !aJsonElement.isJsonObject()) { return rs; } JsonObject aJsonObject = aJsonElement.getAsJsonObject(); diff --git a/src/main/java/mServer/crawler/sender/zdf/json/ZdfFilmDetailDeserializer.java b/src/main/java/mServer/crawler/sender/zdf/json/ZdfFilmDetailDeserializer.java index c0e173561..0a2eb1abf 100644 --- a/src/main/java/mServer/crawler/sender/zdf/json/ZdfFilmDetailDeserializer.java +++ b/src/main/java/mServer/crawler/sender/zdf/json/ZdfFilmDetailDeserializer.java @@ -39,7 +39,9 @@ public class ZdfFilmDetailDeserializer implements JsonDeserializer parseDescription(JsonObject aRootNode) { return Optional.empty(); } - private Optional parseTitle(JsonObject aRootNode, JsonObject aTarget) { - Optional title = parseTitleValue(aRootNode, aTarget); - return title.map(s -> s.replaceAll("\\(CC.*\\) - .* Creative Commons.*", "")); + private Optional parseTitle(final JsonObject aRootNode, final JsonObject aTarget) { + final Optional programmTitle = JsonUtils.getElementValueAsString(aRootNode, JSON_ELEMENT_TITLE); + final Optional programmSubtitle = JsonUtils.getElementValueAsString(aRootNode, JSON_ELEMENT_SUBTITLE); + Optional resultingTitle = formatTitle(programmTitle, programmSubtitle); + if (resultingTitle.isEmpty()) { + final Optional targetTitle = JsonUtils.getElementValueAsString(aTarget, JSON_ELEMENT_TITLE); + final Optional targetSubtitle = JsonUtils.getElementValueAsString(aTarget, JSON_ELEMENT_SUBTITLE); + resultingTitle = formatTitle(targetTitle, targetSubtitle); + } + if (resultingTitle.isPresent()) { + final Optional season = JsonUtils.getElementValueAsString(aTarget, SEASONNUMBER); + final Optional episode = JsonUtils.getElementValueAsString(aTarget, EPISODENUMBER); + final Optional seasonEpisodeTitle = formatEpisodeTitle(season, episode); + final Optional title = cleanupTitle((resultingTitle.get() + " " + seasonEpisodeTitle.orElse("")).trim()); + return title; + } + return Optional.empty(); } - - private Optional parseTitleValue(JsonObject aRootNode, JsonObject aTarget) { - // use property "title" if found - JsonElement titleElement = aRootNode.get(JSON_ELEMENT_TITLE); - if (titleElement != null) { - JsonElement subTitleElement = aRootNode.get(JSON_ELEMENT_SUBTITLE); - if (subTitleElement != null) { - return Optional.of(titleElement.getAsString().trim() + " - " + subTitleElement.getAsString()); - } else { - return Optional.of(titleElement.getAsString()); - } + + private Optional cleanupTitle(String title) { + return Optional.of(title.replaceAll("\\(CC.*\\) - .* Creative Commons.*", "")); + } + + private Optional formatTitle(Optional title, Optional sub) { + if (title.isEmpty()) { + return Optional.empty(); + } + if (sub.isPresent()) { + return Optional.of(title.get().trim() + " - " + sub.get().trim()); } else { - // programmItem target required to determine title - if (aTarget != null && aTarget.has(JSON_ELEMENT_TITLE)) { - String title = aTarget.get(JSON_ELEMENT_TITLE).getAsString(); - String subTitle = aTarget.get(JSON_ELEMENT_SUBTITLE).getAsString(); - - if (subTitle.isEmpty()) { - return Optional.of(title); - } else { - return Optional.of(title.trim() + " - " + subTitle); - } - } + return Optional.of(title.get().trim()); } - - return Optional.empty(); } - + + private Optional formatEpisodeTitle(Optional season, Optional episode) { + if (season.isEmpty() && episode.isEmpty()) { + return Optional.empty(); + } + String result = ""; + if (season.isPresent()) { + result += "S"+season.get(); + } + if (episode.isPresent()) { + result += "E"+episode.get(); + } + return Optional.of("("+result+")"); + } private Optional parseTopic(JsonObject aRootNode) { JsonObject brand = aRootNode.getAsJsonObject(JSON_ELEMENT_BRAND); JsonObject category = aRootNode.getAsJsonObject(JSON_ELEMENT_CATEGORY);