mirror of
				https://github.com/TeamPiped/Piped-Backend.git
				synced 2024-08-14 23:51:41 +00:00 
			
		
		
		
	Merge pull request #398 from TeamPiped/sponsorblock
Add support to configure sponsorblock servers.
This commit is contained in:
		
						commit
						78bbfa7af4
					
				
					 4 changed files with 35 additions and 9 deletions
				
			
		| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<String> 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"));
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue