Skip to content

Commit

Permalink
fallback to low resolution url in case of several playlist entries
Browse files Browse the repository at this point in the history
  • Loading branch information
codingPF committed Mar 20, 2024
1 parent cf3ad05 commit 381e7aa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class OrfOnEpisodeDeserializer implements JsonDeserializer<OrfOnVideoInfo
private static final String TAG_VIDEO_URL = "src";
private static final String TAG_VIDEO_CODEC = "delivery";
private static final String TAG_VIDEO_QUALITY = "quality";
private static final String TAG_VIDEO_FALLBACK = "sources";
private static final String TAG_VIDEO_FALLBACK_URL = "src";

private static final String[] TAG_SUBTITLE_SECTION = {"_embedded", "subtitle"};
private static final String TAG_SUBTITLE_SMI = "sami_url";
private static final String TAG_SUBTITLE_SRT = "srt_url";
Expand Down Expand Up @@ -154,6 +157,11 @@ private Optional<Map<Resolution, FilmUrl>> parseUrl(JsonElement jsonElement) {
if (videoPath1.isEmpty() || !videoPath1.get().isJsonArray() || videoPath1.get().getAsJsonArray().size() == 0) {
return Optional.empty();
}
// We need to fallback to episode.sources in case there are many elements in the playlist
if (videoPath1.get().getAsJsonArray().size() > 1) {
return parseFallbackVideo(jsonElement);
}

Optional<JsonElement> videoPath2 = JsonUtils.getElement(videoPath1.get().getAsJsonArray().get(0), TAG_VIDEO_PATH_2);
if (videoPath2.isEmpty() || !videoPath2.get().isJsonArray()) {
return Optional.empty();
Expand All @@ -168,13 +176,37 @@ private Optional<Map<Resolution, FilmUrl>> parseUrl(JsonElement jsonElement) {
return Optional.empty();
}

private Optional<Map<Resolution, FilmUrl>> parseFallbackVideo(JsonElement root) {
Optional<JsonElement> videoSources = JsonUtils.getElement(root, TAG_VIDEO_FALLBACK);
if (videoSources.isPresent()) {
Map<Resolution, FilmUrl> urls = new EnumMap<>(Resolution.class);
for (String key : PREFERED_CODEC) {
Optional<JsonElement> codecs = JsonUtils.getElement(videoSources.get(), key);
if (codecs.isPresent() && codecs.get().isJsonArray()) {
for (JsonElement singleVideo : codecs.get().getAsJsonArray()) {
Optional<String> tgtUrl = JsonUtils.getElementValueAsString(singleVideo, TAG_VIDEO_FALLBACK_URL);
if (tgtUrl.isPresent()) {
try {
urls.put(Resolution.NORMAL, new FilmUrl(tgtUrl.get(), 0L));
} catch (MalformedURLException e) {
LOG.warn("invalid video url {} error {}", tgtUrl, e );
}
return Optional.of(urls);
}
}
}
}
}
return Optional.empty();
}

private Optional<Map<Resolution, FilmUrl>> readVideoForTargetCodec(JsonElement urlArray, String targetCodec) {
Map<Resolution, FilmUrl> urls = new EnumMap<>(Resolution.class);
for (JsonElement videoElement : urlArray.getAsJsonArray()) {
Optional<String> codec = JsonUtils.getElementValueAsString(videoElement, TAG_VIDEO_CODEC);
Optional<String> quality = JsonUtils.getElementValueAsString(videoElement, TAG_VIDEO_QUALITY);
Optional<String> url = JsonUtils.getElementValueAsString(videoElement, TAG_VIDEO_URL);
if (url.isPresent() && codec.isPresent() && quality.isPresent() && targetCodec.equalsIgnoreCase(codec.get())) {
if (url.isPresent() && codec.isPresent() && quality.isPresent() && targetCodec.equalsIgnoreCase(codec.get()) && OrfOnEpisodeDeserializer.getQuality(quality.get()).isPresent()) {
try {
long fileSize = crawler.determineFileSizeInKB(url.get());
urls.put(
Expand Down Expand Up @@ -221,6 +253,7 @@ private Optional<Collection<GeoLocations>> parseGeoLocations(Optional<String> te
}
return Optional.empty();
}

private Optional<LocalDateTime> parseAiredDate(Optional<String> text) {
Optional<LocalDateTime> result = Optional.empty();
if (text.isPresent()) {
Expand Down Expand Up @@ -264,7 +297,7 @@ private static Optional<Resolution> getQuality(final String aQuality) {
case "QXB":
case "QXBDRM":
case "Q8A":
return Optional.of(Resolution.NORMAL);
return Optional.empty();
default:
LOG.debug("ORF: unknown quality: {}", aQuality);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.mediathekview.mserver.crawler.orfon;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -55,7 +56,7 @@ public void testNormal_1() {
).containsAll(actual.getSubtitles()));
assertEquals(Map.of(
Resolution.HD, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2024-01-04_0707_tl_01_Servus-Kasperl-_____14207792__o__6332192865__s15543049_9__ORF1HD_07081012P_07300711P_Q8C.mp4/playlist.m3u8", 0L),
Resolution.NORMAL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2024-01-04_0707_tl_01_Servus-Kasperl-_____14207792__o__6332192865__s15543049_9__ORF1HD_07081012P_07300711P_QXB.mp4/playlist.m3u8", 0L),
Resolution.NORMAL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2024-01-04_0707_tl_01_Servus-Kasperl-_____14207792__o__6332192865__s15543049_9__ORF1HD_07081012P_07300711P_Q6A.mp4/playlist.m3u8", 0L),
Resolution.SMALL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2024-01-04_0707_tl_01_Servus-Kasperl-_____14207792__o__6332192865__s15543049_9__ORF1HD_07081012P_07300711P_Q4A.mp4/playlist.m3u8", 0L),
Resolution.VERY_SMALL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2024-01-04_0707_tl_01_Servus-Kasperl-_____14207792__o__6332192865__s15543049_9__ORF1HD_07081012P_07300711P_Q1A.3gp/playlist.m3u8", 0L)
), actual.getUrls());
Expand Down Expand Up @@ -88,7 +89,7 @@ public void testNormal_2() {
).containsAll(actual.getSubtitles()));
assertEquals(Map.of(
Resolution.HD, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2024-01-04_0645_tl_00_ABC-Baer_____14207790__o__4346842346__s15542921_1__KIDS1_06363007P_06500003P_Q8C.mp4/playlist.m3u8", 0L),
Resolution.NORMAL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2024-01-04_0645_tl_00_ABC-Baer_____14207790__o__4346842346__s15542921_1__KIDS1_06363007P_06500003P_QXB.mp4/playlist.m3u8", 0L),
Resolution.NORMAL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2024-01-04_0645_tl_00_ABC-Baer_____14207790__o__4346842346__s15542921_1__KIDS1_06363007P_06500003P_Q6A.mp4/playlist.m3u8", 0L),
Resolution.SMALL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2024-01-04_0645_tl_00_ABC-Baer_____14207790__o__4346842346__s15542921_1__KIDS1_06363007P_06500003P_Q4A.mp4/playlist.m3u8", 0L),
Resolution.VERY_SMALL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2024-01-04_0645_tl_00_ABC-Baer_____14207790__o__4346842346__s15542921_1__KIDS1_06363007P_06500003P_Q1A.3gp/playlist.m3u8", 0L)
), actual.getUrls());
Expand Down Expand Up @@ -122,7 +123,7 @@ public void testAD() {
// [, , , , ]
assertEquals(Map.of(
Resolution.HD, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2023-12-06_2307_sd_01_AD---Vorstadtwe_____14204417__o__8792736916__s15524266_6__ORF1ADHD_23092909P_23560117P_Q8C.mp4/playlist.m3u8", 0L),
Resolution.NORMAL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2023-12-06_2307_sd_01_AD---Vorstadtwe_____14204417__o__8792736916__s15524266_6__ORF1ADHD_23092909P_23560117P_QXB.mp4/playlist.m3u8", 0L),
Resolution.NORMAL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2023-12-06_2307_sd_01_AD---Vorstadtwe_____14204417__o__8792736916__s15524266_6__ORF1ADHD_23092909P_23560117P_Q6A.mp4/playlist.m3u8", 0L),
Resolution.SMALL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2023-12-06_2307_sd_01_AD---Vorstadtwe_____14204417__o__8792736916__s15524266_6__ORF1ADHD_23092909P_23560117P_Q4A.mp4/playlist.m3u8", 0L),
Resolution.VERY_SMALL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-austria/2023-12-06_2307_sd_01_AD---Vorstadtwe_____14204417__o__8792736916__s15524266_6__ORF1ADHD_23092909P_23560117P_Q1A.3gp/playlist.m3u8", 0L)
), actual.getUrls());
Expand Down Expand Up @@ -156,7 +157,7 @@ public void testArchive() {
).containsAll(actual.getSubtitles()));
assertEquals(Map.of(
Resolution.HD, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2020-03-26_1310_sd_02_ORF-Mitarbeiter_____14046198__o__8302694905__s14668903_3__ORF3HD_13420114P_13453123P_Q8C.mp4/playlist.m3u8", 0L),
Resolution.NORMAL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2020-03-26_1310_sd_02_ORF-Mitarbeiter_____14046198__o__8302694905__s14668903_3__ORF3HD_13420114P_13453123P_QXB.mp4/playlist.m3u8", 0L),
Resolution.NORMAL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2020-03-26_1310_sd_02_ORF-Mitarbeiter_____14046198__o__8302694905__s14668903_3__ORF3HD_13420114P_13453123P_Q6A.mp4/playlist.m3u8", 0L),
Resolution.SMALL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2020-03-26_1310_sd_02_ORF-Mitarbeiter_____14046198__o__8302694905__s14668903_3__ORF3HD_13420114P_13453123P_Q4A.mp4/playlist.m3u8", 0L),
Resolution.VERY_SMALL, new FilmUrl("https://apasfiis.sf.apa.at/ipad/cms-worldwide/2020-03-26_1310_sd_02_ORF-Mitarbeiter_____14046198__o__8302694905__s14668903_3__ORF3HD_13420114P_13453123P_Q1A.3gp/playlist.m3u8", 0L)
), actual.getUrls());
Expand Down

0 comments on commit 381e7aa

Please sign in to comment.