Implement randomTime for videos that don't exist in dearrow.

This commit is contained in:
Kavin 2023-07-25 03:04:28 +01:00
parent 5046886ce3
commit 1aac9fed1a
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F
2 changed files with 68 additions and 0 deletions

View file

@ -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
}
}

View file

@ -85,6 +85,15 @@ public class SponsorBlockUtils {
}
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)))
@ -98,6 +107,15 @@ public class SponsorBlockUtils {
}
}));
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)