Skip to content

Commit

Permalink
add episode to title
Browse files Browse the repository at this point in the history
  • Loading branch information
codingPF committed Apr 25, 2024
1 parent d2dbf7e commit 5972cac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mServer/crawler/sender/base/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static Optional<String> getElementValueAsString(final JsonElement aJsonEl

public static Optional<JsonElement> getElement(final JsonElement aJsonElement, final String... aElementIds) {
Optional<JsonElement> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public class ZdfFilmDetailDeserializer implements JsonDeserializer<Optional<ZdfF
private static final String JSON_ELEMENT_TITLE = "title";
private static final String JSON_ELEMENT_TEASER_TEXT = "teasertext";
private static final String JSON_ATTRIBUTE_TEMPLATE = "http://zdf.de/rels/streams/ptmd-template";

private static final String EPISODENUMBER = "episodeNumber";
private static final String[] SEASONNUMBER = {"http://zdf.de/rels/cmdm/season", "seasonNumber"};

private static final String PLACEHOLDER_PLAYER_ID = "{playerId}";
private static final String PLAYER_ID = "android_native_5";

Expand Down Expand Up @@ -215,38 +217,53 @@ private Optional<String> parseDescription(JsonObject aRootNode) {
return Optional.empty();
}

private Optional<String> parseTitle(JsonObject aRootNode, JsonObject aTarget) {
Optional<String> title = parseTitleValue(aRootNode, aTarget);
return title.map(s -> s.replaceAll("\\(CC.*\\) - .* Creative Commons.*", ""));
private Optional<String> parseTitle(final JsonObject aRootNode, final JsonObject aTarget) {
final Optional<String> programmTitle = JsonUtils.getElementValueAsString(aRootNode, JSON_ELEMENT_TITLE);
final Optional<String> programmSubtitle = JsonUtils.getElementValueAsString(aRootNode, JSON_ELEMENT_SUBTITLE);
Optional<String> resultingTitle = formatTitle(programmTitle, programmSubtitle);
if (resultingTitle.isEmpty()) {
final Optional<String> targetTitle = JsonUtils.getElementValueAsString(aTarget, JSON_ELEMENT_TITLE);
final Optional<String> targetSubtitle = JsonUtils.getElementValueAsString(aTarget, JSON_ELEMENT_SUBTITLE);
resultingTitle = formatTitle(targetTitle, targetSubtitle);
}
if (resultingTitle.isPresent()) {
final Optional<String> season = JsonUtils.getElementValueAsString(aTarget, SEASONNUMBER);
final Optional<String> episode = JsonUtils.getElementValueAsString(aTarget, EPISODENUMBER);
final Optional<String> seasonEpisodeTitle = formatEpisodeTitle(season, episode);
final Optional<String> title = cleanupTitle((resultingTitle.get() + " " + seasonEpisodeTitle.orElse("")).trim());
return title;
}
return Optional.empty();
}

private Optional<String> 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<String> cleanupTitle(String title) {
return Optional.of(title.replaceAll("\\(CC.*\\) - .* Creative Commons.*", ""));
}

private Optional<String> formatTitle(Optional<String> title, Optional<String> 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<String> formatEpisodeTitle(Optional<String> season, Optional<String> 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<String> parseTopic(JsonObject aRootNode) {
JsonObject brand = aRootNode.getAsJsonObject(JSON_ELEMENT_BRAND);
JsonObject category = aRootNode.getAsJsonObject(JSON_ELEMENT_CATEGORY);
Expand Down

0 comments on commit 5972cac

Please sign in to comment.