mirror of
				https://github.com/TeamPiped/Piped-Backend.git
				synced 2024-08-14 23:51:41 +00:00 
			
		
		
		
	Merge pull request #653 from TeamPiped/dearrow-random-time
Implement randomTime for videos that don't exist in dearrow.
This commit is contained in:
		
						commit
						63d1cd3cf4
					
				
					 2 changed files with 68 additions and 0 deletions
				
			
		
							
								
								
									
										50
									
								
								src/main/java/me/kavin/piped/utils/Alea.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/main/java/me/kavin/piped/utils/Alea.java
									
										
									
									
									
										Normal 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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue