Skip to content

Commit

Permalink
optimize wdr hd urls
Browse files Browse the repository at this point in the history
  • Loading branch information
pidoubleyou committed Nov 21, 2023
1 parent 71ef280 commit 99b4dd1
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/main/java/mServer/crawler/sender/ard/ArdUrlOptimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class ArdUrlOptimizer {

Expand Down Expand Up @@ -32,17 +33,42 @@ public class ArdUrlOptimizer {
}

public String optimizeHdUrl(final String url) {
for (Map.Entry<String, String[]> entry : HD_OPTIMIZE.entrySet()) {
if (url.contains(entry.getKey())) {
for (String optimizeFragment : entry.getValue()) {
final String optimizedUrl = url.replace(entry.getKey(), optimizeFragment);
if (UrlUtils.existsUrl(optimizedUrl)) {
return optimizedUrl;
String fullHdUrl = "";
if (url.contains("wdrmedien")) {
fullHdUrl = determineWdrFullHdUrl(url);
} else {
for (Map.Entry<String, String[]> entry : HD_OPTIMIZE.entrySet()) {
if (url.contains(entry.getKey())) {
for (String optimizeFragment : entry.getValue()) {
fullHdUrl = url.replace(entry.getKey(), optimizeFragment);
}
}
}
}

if (!fullHdUrl.isEmpty() && UrlUtils.existsUrl(fullHdUrl)) {
return fullHdUrl;
}

return url;
}

/**
* wdr urls uses the following pattern: the last part of the filename determines the quality this
* is the actual order: 1920, 480, 640, 960, 1280 to determine the 1920-url by the 1280-url
* substract 4
* example:
* 1280: https://wdrmedien-a.akamaihd.net/.../2625725_54085881.mp4
* 1920: https://wdrmedien-a.akamaihd.net/.../2625725_54085877.mp4
*/
private String determineWdrFullHdUrl(String url) {
final Optional<String> fileName = UrlUtils.getFileName(url);
if (fileName.isPresent()) {
final String s = fileName.get();
final String substring = s.substring(s.indexOf("_") + 1).replace(".mp4", "");
final int hdInt = Integer.parseInt(substring) - 4;
return url.replace(substring, Integer.toString(hdInt));
}
return url;
}
}

0 comments on commit 99b4dd1

Please sign in to comment.