Skip to content

Commit

Permalink
optimize hd urls
Browse files Browse the repository at this point in the history
  • Loading branch information
pidoubleyou committed Feb 15, 2023
1 parent bdd2b3e commit c54fdc5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ public class ZdfVideoUrlOptimizer {
private static final String NORMAL_2360_35_17 = "2360k_p35v17.mp4";

private static final String HD_3256 = "3256k_p15v12.mp4";
private static final String HD_3296 = "3296k_p15v13.mp4";
private static final String HD_3296_15_13 = "3296k_p15v13.mp4";
private static final String HD_3296_15_14 = "3296k_p15v14.mp4";
private static final String HD_3328_15_15 = "3328k_p15v15.mp4";
private static final String HD_3328_12 = "3328k_p36v12.mp4";
private static final String HD_3328_13 = "3328k_p36v13.mp4";
private static final String HD_3328_14 = "3328k_p36v14.mp4";
private static final String HD_3328_35_14 = "3328k_p35v14.mp4";
private static final String HD_3328_36_13 = "3328k_p36v13.mp4";
private static final String HD_3328_36_14 = "3328k_p36v14.mp4";
private static final String HD_3360_36_15 = "3360k_p36v15.mp4";
private static final String HD_3360_36_17 = "3360k_p36v17.mp4";
private static final String HD_6628_61_17 = "6628k_p61v17.mp4";
private static final String HD_6660_37_17 = "6660k_p37v17.mp4";

private static final Map<String, String[]> NORMAL_OPTIMIZE = new HashMap<>();
private static final Map<String, String[]> NORMAL_TO_HD = new HashMap<>();
private static final Map<String, String[]> HD_OPTIMIZE = new HashMap<>();

static {
Expand All @@ -57,20 +62,26 @@ public class ZdfVideoUrlOptimizer {
NORMAL_OPTIMIZE.put(NORMAL_1496_13_14, new String[] {NORMAL_2328_35_14, NORMAL_2296_14_14});
NORMAL_OPTIMIZE.put(NORMAL_1628_13_15, new String[] {NORMAL_2360_35_15});


HD_OPTIMIZE.put(NORMAL_2360_35_17, new String[] {HD_6660_37_17, HD_6628_61_17, HD_3360_36_17});
HD_OPTIMIZE.put(NORMAL_1628_13_17, new String[] {HD_6660_37_17, HD_6628_61_17, HD_3360_36_17});
HD_OPTIMIZE.put(NORMAL_1456_13_12, new String[] {HD_3328_12, HD_3256});
HD_OPTIMIZE.put(NORMAL_2256_14_12, new String[] {HD_3328_12, HD_3256});
HD_OPTIMIZE.put(NORMAL_2328_35_12, new String[] {HD_3328_12, HD_3256});
HD_OPTIMIZE.put(NORMAL_1496_13_13, new String[] {HD_3328_13, HD_3296});
HD_OPTIMIZE.put(NORMAL_2296_14_13, new String[] {HD_3328_13, HD_3296});
HD_OPTIMIZE.put(NORMAL_2328_35_13, new String[] {HD_3328_13, HD_3296});
HD_OPTIMIZE.put(NORMAL_1496_13_14, new String[] {HD_3328_14, HD_3328_35_14});
HD_OPTIMIZE.put(NORMAL_2296_14_14, new String[] {HD_3328_14, HD_3328_35_14});
HD_OPTIMIZE.put(NORMAL_2328_35_14, new String[] {HD_3328_14, HD_3328_35_14});
HD_OPTIMIZE.put(NORMAL_1628_13_15, new String[] {HD_3360_36_15});
HD_OPTIMIZE.put(NORMAL_2360_35_15, new String[] {HD_3360_36_15});
NORMAL_TO_HD.put(NORMAL_2360_35_17, new String[] {HD_6660_37_17, HD_6628_61_17, HD_3360_36_17});
NORMAL_TO_HD.put(NORMAL_1628_13_17, new String[] {HD_6660_37_17, HD_6628_61_17, HD_3360_36_17});
NORMAL_TO_HD.put(NORMAL_1456_13_12, new String[] {HD_3328_12, HD_3256});
NORMAL_TO_HD.put(NORMAL_2256_14_12, new String[] {HD_3328_12, HD_3256});
NORMAL_TO_HD.put(NORMAL_2328_35_12, new String[] {HD_3328_12, HD_3256});
NORMAL_TO_HD.put(NORMAL_1496_13_13, new String[] {HD_3328_13, HD_3296_15_14, HD_3296_15_13});
NORMAL_TO_HD.put(NORMAL_2296_14_13, new String[] {HD_3328_13, HD_3296_15_14, HD_3296_15_13});
NORMAL_TO_HD.put(NORMAL_2328_35_13, new String[] {HD_3328_13, HD_3296_15_14, HD_3296_15_13});
NORMAL_TO_HD.put(NORMAL_1496_13_14, new String[] {HD_3328_14, HD_3328_35_14});
NORMAL_TO_HD.put(NORMAL_2296_14_14, new String[] {HD_3328_14, HD_3328_35_14});
NORMAL_TO_HD.put(NORMAL_2328_35_14, new String[] {HD_3328_14, HD_3328_35_14});
NORMAL_TO_HD.put(NORMAL_1628_13_15, new String[] {HD_3360_36_15});
NORMAL_TO_HD.put(NORMAL_2360_35_15, new String[] {HD_3360_36_15});

HD_OPTIMIZE.put(HD_3360_36_17, new String[] {HD_6660_37_17, HD_6628_61_17});
HD_OPTIMIZE.put(HD_6628_61_17, new String[] {HD_6660_37_17});
HD_OPTIMIZE.put(HD_3328_15_15, new String[] {HD_3360_36_15});
HD_OPTIMIZE.put(HD_3256, new String[] {HD_3328_12});
HD_OPTIMIZE.put(HD_3296_15_14, new String[] {HD_3328_36_14});
HD_OPTIMIZE.put(HD_3296_15_13, new String[] {HD_3328_36_13});
}

protected AbstractCrawler crawler;
Expand All @@ -89,6 +100,15 @@ public String getOptimizedUrlNormal(final String aUrl) {
return optimize(aUrl, NORMAL_OPTIMIZE);
}

/**
* optimizes the hd url
* @param aUrl the hd url
* @return the optimized hd url
*/
public String getOptimizedUrlHd(final String aUrl) {
return optimize(aUrl, HD_OPTIMIZE);
}

/**
* tries to find the hd url depending on the normal url.
*
Expand All @@ -100,7 +120,7 @@ public Optional<String> determineUrlHd(final String aNormalUrl) {
return Optional.empty();
}

final String url = optimize(aNormalUrl, HD_OPTIMIZE);
final String url = optimize(aNormalUrl, NORMAL_TO_HD);
if (url.equalsIgnoreCase(aNormalUrl)) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ private Map<Resolution, FilmUrl> getOptimizedUrls(Map<Resolution, String> urls)

if (qualitiesEntry.getKey() == Resolution.NORMAL) {
url = optimizer.getOptimizedUrlNormal(url);
} else if (qualitiesEntry.getKey() == Resolution.HD) {
url = optimizer.getOptimizedUrlHd(url);
}

result.put(qualitiesEntry.getKey(), new FilmUrl(url, crawler.determineFileSizeInKB(url)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,39 @@ public void getOptimizedUrlNormalTestBetterUrlNotExists() {
assertGetOptimizedUrlNormal(url, url);
}

@Test
public void getOptimizedUrlHdTestAlreadyBestQuality() {
final String url = getWireMockBaseUrlSafe() + "/video_6660k_p37v17.mp4";

assertGetOptimizedUrlHd(url, url);
}

@Test
public void getOptimizedUrlHdTestBetterUrlExists() {
final String url = getWireMockBaseUrlSafe() + "/video_3360k_p36v17.mp4";
final String expectedUrl = getWireMockBaseUrlSafe() + "/video_6660k_p37v17.mp4";

setupHeadResponse("/video_6660k_p37v17.mp4", 200);
assertGetOptimizedUrlHd(expectedUrl, url);
}

@Test
public void getOptimizedUrlHdTestBetterUrlExists2() {
final String url = getWireMockBaseUrlSafe() + "/video_3360k_p36v17.mp4";
final String expectedUrl = getWireMockBaseUrlSafe() + "/video_6628k_p61v17.mp4";

setupHeadResponse("/video_6628k_p61v17.mp4", 200);
assertGetOptimizedUrlHd(expectedUrl, url);
}

@Test
public void getOptimizedUrlHdTestBetterUrlNotExists() {
final String url = getWireMockBaseUrlSafe() + "/video_3360k_p36v17.mp4";

setupHeadResponse("/video_6660k_p37v17.mp4", 404);
assertGetOptimizedUrlHd(url, url);
}

@Test
public void determineUrlHdTestFirstUrlExists() {
final String url = getWireMockBaseUrlSafe() + "/video_1456k_p13v12.mp4";
Expand Down Expand Up @@ -125,6 +158,11 @@ private void assertGetOptimizedUrlNormal(final String aExpectedUrl, final String
assertThat(actual, equalTo(aExpectedUrl));
}

private void assertGetOptimizedUrlHd(final String aExpectedUrl, final String aUrlToCheck) {
final String actual = target.getOptimizedUrlHd(aUrlToCheck);
assertThat(actual, equalTo(aExpectedUrl));
}

private void assertDetermineUrlHd(final Optional<String> aExpectedUrl, final String aUrlToCheck) {
final Optional<String> actual = target.determineUrlHd(aUrlToCheck);
assertThat(actual, equalTo(aExpectedUrl));
Expand Down

0 comments on commit c54fdc5

Please sign in to comment.