From a0e81cb7e914611814c75829ae0d681573050bf0 Mon Sep 17 00:00:00 2001
From: CodingPF <j-doo@gmx.de>
Date: Wed, 8 May 2024 20:57:19 +0200
Subject: [PATCH] two digit season and episode

---
 .../java/mServer/crawler/sender/base/JsonUtils.java  |  7 +++++++
 .../sender/zdf/json/ZdfFilmDetailDeserializer.java   | 12 ++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/main/java/mServer/crawler/sender/base/JsonUtils.java b/src/main/java/mServer/crawler/sender/base/JsonUtils.java
index 296b87f5..3b0566f7 100644
--- a/src/main/java/mServer/crawler/sender/base/JsonUtils.java
+++ b/src/main/java/mServer/crawler/sender/base/JsonUtils.java
@@ -113,6 +113,13 @@ public static boolean hasElements(
     return true;
   }
 
+  public static Optional<Integer> getElementValueAsInteger(final JsonElement aJsonElement, final String... aElementIds) {
+    Optional<JsonElement> rs = JsonUtils.getElement(aJsonElement, aElementIds);
+    if (rs.isPresent()) {
+      return Optional.of(rs.get().getAsInt());
+    }
+    return Optional.empty();
+  }
   /**
    * Checks if the {@link JsonObject} has all given elements and if no element
    * is null or empty.
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 0a2eb1ab..3c63ec63 100644
--- a/src/main/java/mServer/crawler/sender/zdf/json/ZdfFilmDetailDeserializer.java
+++ b/src/main/java/mServer/crawler/sender/zdf/json/ZdfFilmDetailDeserializer.java
@@ -227,8 +227,8 @@ private Optional<String> parseTitle(final JsonObject aRootNode, final JsonObject
       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<Integer> season = JsonUtils.getElementValueAsInteger(aTarget, SEASONNUMBER);
+      final Optional<Integer> episode = JsonUtils.getElementValueAsInteger(aTarget, EPISODENUMBER);
       final Optional<String> seasonEpisodeTitle = formatEpisodeTitle(season, episode);
       final Optional<String> title = cleanupTitle((resultingTitle.get() + " " + seasonEpisodeTitle.orElse("")).trim());
       return title;
@@ -244,23 +244,23 @@ private Optional<String> formatTitle(Optional<String> title, Optional<String> su
     if (title.isEmpty()) {
       return Optional.empty();
     }
-    if (sub.isPresent()) {
+    if (sub.isPresent() && !sub.get().isBlank()) {
       return Optional.of(title.get().trim() + " - " + sub.get().trim());
     } else {
       return Optional.of(title.get().trim());
     }
   }
   
-  private Optional<String> formatEpisodeTitle(Optional<String> season, Optional<String> episode) {
+  private Optional<String> formatEpisodeTitle(Optional<Integer> season, Optional<Integer> episode) {
     if (season.isEmpty() && episode.isEmpty()) {
       return Optional.empty();
     }
     String result = "";
     if (season.isPresent()) {
-      result += "S"+season.get();
+      result += String.format("S%02d", season.get());
     }
     if (episode.isPresent()) {
-      result += "E"+episode.get();
+      result += String.format("E%02d", episode.get());
     }
     return Optional.of("("+result+")");
   }