Skip to content

Commit

Permalink
Merge pull request #648 from TeamPiped/dearrow
Browse files Browse the repository at this point in the history
Implement batched fetching of dearrow content.
  • Loading branch information
FireMasterK authored Jul 18, 2023
2 parents 34efcb3 + 369569d commit 68bbf05
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/me/kavin/piped/server/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.io.ByteArrayInputStream;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -149,6 +150,23 @@ AsyncServlet mainServlet(Executor executor) {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/dearrow", AsyncServlet.ofBlocking(executor, request -> {
try {
var videoIds = getArray(request.getQueryParameter("videoIds"));

return getJsonResponse(
SponsorBlockUtils.getDeArrowedInfo(List.of(videoIds))
.thenApplyAsync(json -> {
try {
return mapper.writeValueAsBytes(json);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}).get(),
"public, max-age=3600");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/streams/:videoId", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(StreamHandlers.streamsResponse(request.getPathParameter("videoId")),
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package me.kavin.piped.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.resp.InvalidRequestResponse;
import me.kavin.piped.utils.resp.SimpleErrorMessage;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static me.kavin.piped.consts.Constants.mapper;

Expand Down Expand Up @@ -46,4 +54,46 @@ public static String getSponsors(String id, String categories)
return null;
}

public static CompletableFuture<ObjectNode> getDeArrowedInfo(List<String> videoIds) {
ObjectNode objectNode = mapper.createObjectNode();

var futures = videoIds.stream()
.map(id -> getDeArrowedInfo(id).thenAcceptAsync(jsonNode -> objectNode.set(id, jsonNode.orElse(NullNode.getInstance()))))
.toArray(CompletableFuture[]::new);

return CompletableFuture.allOf(futures)
.thenApplyAsync(v -> objectNode, Multithreading.getCachedExecutor());
}

private static CompletableFuture<Optional<JsonNode>> getDeArrowedInfo(String videoId) {

String hash = DigestUtils.sha256Hex(videoId);

CompletableFuture<Optional<JsonNode>> future = new CompletableFuture<>();

Multithreading.runAsync(() -> {
for (String url : Constants.SPONSORBLOCK_SERVERS)
try {
Optional<JsonNode> optional = RequestUtils.sendGetJson(url + "/api/branding/" + URLUtils.silentEncode(hash.substring(0, 4)))
.thenApplyAsync(json -> json.has(videoId) ? Optional.of(json.get(videoId)) : Optional.<JsonNode>empty())
.get();

optional.ifPresent(jsonNode -> {
ArrayNode nodes = (ArrayNode) jsonNode.get("thumbnails");
for (JsonNode node : nodes) {
((ObjectNode) node).set("thumbnail", new TextNode(URLUtils.rewriteURL("https://dearrow-thumb.ajay.app/api/v1/getThumbnail?videoID=" + videoId + "&time=" + node.get("timestamp").asText())));
}
});


future.complete(optional);
return;
} catch (Exception ignored) {
}
future.completeExceptionally(new Exception("All SponsorBlock servers are down"));
});

return future;

}
}

0 comments on commit 68bbf05

Please sign in to comment.