From 1aac9fed1afdd19af1afabce55bd965afa4bcf91 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Tue, 25 Jul 2023 03:04:28 +0100 Subject: [PATCH] Implement randomTime for videos that don't exist in dearrow. --- src/main/java/me/kavin/piped/utils/Alea.java | 50 +++++++++++++++++++ .../kavin/piped/utils/SponsorBlockUtils.java | 18 +++++++ 2 files changed, 68 insertions(+) create mode 100644 src/main/java/me/kavin/piped/utils/Alea.java diff --git a/src/main/java/me/kavin/piped/utils/Alea.java b/src/main/java/me/kavin/piped/utils/Alea.java new file mode 100644 index 00000000..4e5059fa --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/Alea.java @@ -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 + } +} diff --git a/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java b/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java index 1e9db55a..4151575e 100644 --- a/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java +++ b/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java @@ -85,6 +85,15 @@ private static CompletableFuture> 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> 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 @@ private static void fetchDeArrowedCf(CompletableFuture> 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)