Skip to content

Commit

Permalink
Implement randomTime for videos that don't exist in dearrow.
Browse files Browse the repository at this point in the history
  • Loading branch information
FireMasterK committed Jul 25, 2023
1 parent 5046886 commit 1aac9fe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/main/java/me/kavin/piped/utils/Alea.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.kavin.piped.utils;

public class Alea {

private static final double NORM32 = 2.3283064365386963e-10; // 2^-32
private double s0, s1, s2;
private int c = 1;

public double next() {
double t = 2091639.0 * s0 + c * NORM32; // 2^-32
s0 = s1;
s1 = s2;
return s2 = t - (c = (int) t);
}

public Alea(String seed) {
s0 = mash(" ");
s1 = mash(" ");
s2 = mash(" ");

s0 -= mash(seed);

if (s0 < 0)
s0 += 1;
s1 -= mash(seed);
if (s1 < 0)
s1 += 1;
s2 -= mash(seed);
if (s2 < 0)
s2 += 1;
}

private long n = 0xefc8249dL;

public double mash(String x) {
double h;

for (char c : x.toCharArray()) {
n += c;
h = 0.02519603282416938 * n;
n = (long) h;
h -= n;
h *= n;
n = (long) h;
h -= n;
n += h * 0x100000000L;
}
return n * 2.3283064365386963e-10; // 2^-32
}
}
18 changes: 18 additions & 0 deletions src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ private static CompletableFuture<Optional<JsonNode>> getDeArrowedInfo(String vid

}

private static final ObjectNode EMPTY_DEARROWED_INFO;

static {
EMPTY_DEARROWED_INFO = mapper.createObjectNode();
EMPTY_DEARROWED_INFO.putArray("titles");
EMPTY_DEARROWED_INFO.putArray("thumbnails");
EMPTY_DEARROWED_INFO.set("videoDuration", NullNode.getInstance());
}

private static void fetchDeArrowedCf(CompletableFuture<Optional<JsonNode>> future, String videoId, String hash, String[] servers) {

var completableFuture = RequestUtils.sendGetJson(servers[0] + "/api/branding/" + URLUtils.silentEncode(hash.substring(0, 4)))
Expand All @@ -98,6 +107,15 @@ private static void fetchDeArrowedCf(CompletableFuture<Optional<JsonNode>> futur
}
}));

completableFuture = completableFuture.thenApplyAsync(optional -> {
if (optional.isEmpty()) {
var clone = EMPTY_DEARROWED_INFO.deepCopy();
clone.put("randomTime", new Alea(videoId).next());
return Optional.of(clone);
} else
return optional;
});


completableFuture.whenComplete((optional, throwable) -> {
if (throwable == null)
Expand Down

0 comments on commit 1aac9fe

Please sign in to comment.