2020-11-12 21:19:45 +00:00
|
|
|
package me.kavin.piped.utils;
|
|
|
|
|
2022-10-30 21:40:31 +00:00
|
|
|
import com.grack.nanojson.JsonArray;
|
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonWriter;
|
2022-02-02 21:05:22 +00:00
|
|
|
import me.kavin.piped.consts.Constants;
|
|
|
|
import me.kavin.piped.utils.resp.InvalidRequestResponse;
|
2022-10-30 21:40:31 +00:00
|
|
|
import me.kavin.piped.utils.resp.SimpleErrorMessage;
|
2022-02-02 21:05:22 +00:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
2020-11-12 21:19:45 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
public class SponsorBlockUtils {
|
|
|
|
|
2022-02-02 21:05:22 +00:00
|
|
|
public static String getSponsors(String id, String categories)
|
2022-10-30 21:40:31 +00:00
|
|
|
throws IOException, NoSuchAlgorithmException {
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-12-24 14:17:02 +00:00
|
|
|
if (StringUtils.isEmpty(categories))
|
|
|
|
return Constants.mapper.writeValueAsString(new InvalidRequestResponse());
|
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
String hash = toSha256(id);
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2022-10-23 18:17:04 +00:00
|
|
|
for (String url : Constants.SPONSORBLOCK_SERVERS) {
|
2023-01-29 12:16:57 +00:00
|
|
|
try {
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2023-01-29 12:16:57 +00:00
|
|
|
var resp = RequestUtils.sendGetRaw(url + "/api/skipSegments/" + URLUtils.silentEncode(hash.substring(0, 4))
|
|
|
|
+ "?categories=" + URLUtils.silentEncode(categories));
|
|
|
|
|
|
|
|
if (resp.status() == 200) {
|
|
|
|
JsonArray jArray = JsonParser.array().from(new String(resp.body()));
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2022-10-23 18:17:04 +00:00
|
|
|
jArray.removeIf(jObject -> !((JsonObject) jObject).getString("videoID").equalsIgnoreCase(id));
|
|
|
|
|
|
|
|
return JsonWriter.string(jArray.getObject(0));
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 21:40:31 +00:00
|
|
|
ExceptionHandler.throwErrorResponse(new SimpleErrorMessage("All SponsorBlock servers are down"));
|
|
|
|
|
|
|
|
return null;
|
2020-11-12 21:19:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 21:05:22 +00:00
|
|
|
private static String toSha256(final String videoId) throws NoSuchAlgorithmException {
|
2021-02-24 09:52:29 +00:00
|
|
|
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
|
|
|
final byte[] bytes = digest.digest(videoId.getBytes(StandardCharsets.UTF_8));
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
for (final byte b : bytes) {
|
|
|
|
final String hex = Integer.toHexString(0xff & b);
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
if (hex.length() == 1) {
|
|
|
|
sb.append('0');
|
|
|
|
}
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
sb.append(hex);
|
|
|
|
}
|
2020-11-12 21:19:45 +00:00
|
|
|
|
2021-02-24 09:52:29 +00:00
|
|
|
return sb.toString();
|
2020-11-12 21:19:45 +00:00
|
|
|
}
|
|
|
|
}
|