diff --git a/config.properties b/config.properties index 63e5a27..6b2bbc4 100644 --- a/config.properties +++ b/config.properties @@ -23,6 +23,9 @@ FEED_RETENTION:30 DISABLE_TIMERS:false # RYD Proxy URL (see https://github.com/TeamPiped/RYD-Proxy) RYD_PROXY_URL:https://ryd-proxy.kavin.rocks +# SponsorBlock Servers(s) +# Comma separated list of SponsorBlock Servers to use +SPONSORBLOCK_SERVERS:https://sponsor.ajay.app,https://sponsorblock.kavin.rocks # Disable the usage of RYD DISABLE_RYD:false # Disable API server (node just runs timers if enabled) diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index 9ae50d6..3d93789 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -16,6 +16,7 @@ import java.io.File; import java.io.FileReader; import java.net.InetSocketAddress; import java.net.ProxySelector; +import java.util.List; import java.util.Properties; public class Constants { @@ -36,7 +37,7 @@ public class Constants { public static final String PUBLIC_URL; public static final String PUBSUB_URL; - + public static final String PUBSUB_HUB_URL; public static final String HTTP_PROXY; @@ -56,6 +57,8 @@ public class Constants { public static final String RYD_PROXY_URL; + public static final List SPONSORBLOCK_SERVERS; + public static final boolean DISABLE_RYD; public static final boolean DISABLE_SERVER; @@ -95,6 +98,8 @@ public class Constants { FEED_RETENTION = Integer.parseInt(getProperty(prop, "FEED_RETENTION", "30")); DISABLE_TIMERS = Boolean.parseBoolean(getProperty(prop, "DISABLE_TIMERS", "false")); RYD_PROXY_URL = getProperty(prop, "RYD_PROXY_URL", "https://ryd-proxy.kavin.rocks"); + SPONSORBLOCK_SERVERS = List.of(getProperty(prop, "SPONSORBLOCK_SERVERS", "https://sponsor.ajay.app,https://sponsorblock.kavin.rocks") + .split(",")); DISABLE_RYD = Boolean.parseBoolean(getProperty(prop, "DISABLE_RYD", "false")); DISABLE_SERVER = Boolean.parseBoolean(getProperty(prop, "DISABLE_SERVER", "false")); DISABLE_LBRY = Boolean.parseBoolean(getProperty(prop, "DISABLE_LBRY", "false")); diff --git a/src/main/java/me/kavin/piped/utils/RequestUtils.java b/src/main/java/me/kavin/piped/utils/RequestUtils.java index 3b2ca00..d663ecb 100644 --- a/src/main/java/me/kavin/piped/utils/RequestUtils.java +++ b/src/main/java/me/kavin/piped/utils/RequestUtils.java @@ -2,19 +2,28 @@ package me.kavin.piped.utils; import me.kavin.piped.consts.Constants; import okhttp3.Request; +import okhttp3.Response; import java.io.IOException; public class RequestUtils { + public static Response sendGetRaw(String url) throws IOException { + return sendGetRaw(url, Constants.USER_AGENT); + } + + public static Response sendGetRaw(String url, String ua) throws IOException { + var request = new Request.Builder().header("User-Agent", ua).url(url).build(); + return Constants.h2client.newCall(request).execute(); + } + public static String sendGet(String url) throws IOException { return sendGet(url, Constants.USER_AGENT); } public static String sendGet(String url, String ua) throws IOException { - var request = new Request.Builder().header("User-Agent", ua).url(url).build(); - var response = Constants.h2client.newCall(request).execute(); + var response = sendGetRaw(url, ua); var responseString = response.body().string(); response.close(); diff --git a/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java b/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java index cfe003a..010f49f 100644 --- a/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java +++ b/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java @@ -20,14 +20,23 @@ public class SponsorBlockUtils { String hash = toSha256(id); - JsonArray jArray = JsonParser.array().from( - RequestUtils.sendGet("https://sponsor.ajay.app/api/skipSegments/" + URLUtils.silentEncode(hash.substring(0, 4)) - + "?categories=" + URLUtils.silentEncode(categories)) - ); + for (String url : Constants.SPONSORBLOCK_SERVERS) { + try (var resp = RequestUtils.sendGetRaw(url + "/api/skipSegments/" + URLUtils.silentEncode(hash.substring(0, 4)) + + "?categories=" + URLUtils.silentEncode(categories))) { - jArray.removeIf(jObject -> !((JsonObject) jObject).getString("videoID").equalsIgnoreCase(id)); + if (resp.code() == 200) { + JsonArray jArray = JsonParser.array().from(resp.body().string()); - return JsonWriter.string(jArray.getObject(0)); + jArray.removeIf(jObject -> !((JsonObject) jObject).getString("videoID").equalsIgnoreCase(id)); + + return JsonWriter.string(jArray.getObject(0)); + } + } catch (Exception ignored) { + } + } + + return Constants.mapper.writeValueAsString(Constants.mapper.createObjectNode() + .put("error", "All SponsorBlock servers are down")); } private static String toSha256(final String videoId) throws NoSuchAlgorithmException {