Skip to content

Commit

Permalink
Merge pull request #976 from mediathekview/feature/942
Browse files Browse the repository at this point in the history
ZDF add episode to title
  • Loading branch information
codingPF authored May 11, 2024
2 parents 9630cb8 + c71545b commit 41af0de
Show file tree
Hide file tree
Showing 5 changed files with 1,425 additions and 41 deletions.
12 changes: 10 additions & 2 deletions src/main/java/de/mediathekview/mserver/base/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,18 @@ public static Optional<String> getElementValueAsString(final JsonElement aJsonEl
}
return Optional.empty();
}


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();
}

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 @@ -59,9 +59,12 @@ public class ZdfFilmDetailDeserializer implements JsonDeserializer<Optional<ZdfF
private static final DateTimeFormatter DATE_FORMATTER_AIRTIME =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"); // 2016-10-29T16:15:00+02:00

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

private final String apiUrlBase;
private final Sender sender;

public ZdfFilmDetailDeserializer(final String apiUrlBase, final Sender sender) {
this.apiUrlBase = apiUrlBase;
this.sender = sender;
Expand Down Expand Up @@ -98,9 +101,9 @@ public Optional<ZdfFilmDto> deserialize(
final Optional<String> website = parseWebsiteUrl(rootNode);
final Optional<LocalDateTime> time = parseAirtime(rootNode, programItemTarget);
final Optional<Duration> duration = parseDuration(mainVideoTarget);

final Map<String, String> downloadUrl = parseDownloadUrls(mainVideoTarget);

if (title.isPresent()) {
final Optional<Film> film =
createFilm(topic, title.get(), description, website, time, duration);
Expand Down Expand Up @@ -260,36 +263,50 @@ private Optional<String> parseDescription(final JsonObject aRootNode) {
}

private Optional<String> parseTitle(final JsonObject aRootNode, final JsonObject aTarget) {
final Optional<String> title = parseTitleValue(aRootNode, aTarget);
return title.map(s -> s.replaceAll("\\(CC.*\\) - .* Creative Commons.*", ""));
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<Integer> season = JsonUtils.getElementValueAsInteger(aTarget, SEASONNUMBER);
final Optional<Integer> episode = JsonUtils.getElementValueAsInteger(aTarget, EPISODENUMBER);
final Optional<String> seasonEpisodeTitle = formatEpisodeTitle(season, episode);
return cleanupTitle((resultingTitle.get() + " " + seasonEpisodeTitle.orElse("")).trim());
}
return Optional.empty();
}

private Optional<String> parseTitleValue(final JsonObject aRootNode, final JsonObject aTarget) {
// use property "title" if found
final JsonElement titleElement = aRootNode.get(JSON_ELEMENT_TITLE);
if (titleElement != null) {
final JsonElement subTitleElement = aRootNode.get(JSON_ELEMENT_SUBTITLE);
if (subTitleElement != null && !subTitleElement.getAsString().isBlank()) {
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() && !sub.get().trim().isEmpty()) {
return Optional.of(title.get().trim() + " - " + sub.get().trim());
} else {
// programmItem target required to determine title
if (aTarget != null && aTarget.has(JSON_ELEMENT_TITLE)) {
final String title = aTarget.get(JSON_ELEMENT_TITLE).getAsString();
final 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<Integer> season, Optional<Integer> episode) {
if (season.isEmpty() && episode.isEmpty()) {
return Optional.empty();
}
String result = "";
if (season.isPresent()) {
result += String.format("S%02d", season.get());
}
if (episode.isPresent()) {
result += String.format("E%02d", episode.get());
}
return Optional.of("("+result+")");
}

private Optional<String> parseTopic(final JsonObject aRootNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ private boolean isRelevant(Element teaserElement) {
if (teaserElement == null) {
return true;
}
return !("ARD".equalsIgnoreCase(teaserElement.text()));
return !("ARD".equalsIgnoreCase(teaserElement.text()) || "funk".equalsIgnoreCase(teaserElement.text()));
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package de.mediathekview.mserver.crawler.zdf.json;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import com.google.gson.JsonObject;
import de.mediathekview.mlib.daten.Film;
import de.mediathekview.mlib.daten.Sender;
import de.mediathekview.mserver.crawler.zdf.ZdfConstants;
import de.mediathekview.mserver.crawler.zdf.ZdfFilmDto;
import de.mediathekview.mserver.testhelper.AssertFilm;
import de.mediathekview.mserver.testhelper.JsonFileReader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ZdfFilmDetailDeserializerTest {
Expand Down Expand Up @@ -158,6 +157,18 @@ public static Collection<Object[]> data() {
"https://api.zdf.de/tmd/2/android_native_5/vod/ptmd/mediathek/220505_geliebt_geduldet_getoetet_inf/4",
Optional.of(
"https://api.zdf.de/tmd/2/android_native_5/vod/ptmd/mediathek/220505_geliebt_geduldet_getoetet_inf_dgs/2")
},
{
"/zdf/zdf_film_details_with_episodes.json",
Sender.ZDF,
"The Rookie",
"Der Prozess (S05E01)",
LocalDateTime.of(2024, 4, 11, 20, 15, 0),
Duration.ofMinutes(41).plusSeconds(6),
"Officer John Nolan sieht sich wieder mit der Serienmörderin Rosalind Dyer konfrontiert. Vor Beginn der Gerichtsverhandlung gelingt ihr die Flucht und sie muss erneut gefasst werden.",
"https://www.zdf.de/serien/the-rookie/der-prozess-110.html",
"https://api.zdf.de/tmd/2/android_native_5/vod/ptmd/mediathek/240411_2015_sendung_roo/2",
Optional.empty()
}
});
}
Expand Down
Loading

0 comments on commit 41af0de

Please sign in to comment.