Cleanup SponsorBlock utils to fix issues.

This commit is contained in:
Kavin 2023-03-18 18:16:39 +00:00
parent 0c971e4d1c
commit 69814ddc30
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD

View file

@ -1,9 +1,5 @@
package me.kavin.piped.utils;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonWriter;
import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.resp.InvalidRequestResponse;
import me.kavin.piped.utils.resp.SimpleErrorMessage;
@ -11,15 +7,16 @@ import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import static me.kavin.piped.consts.Constants.mapper;
public class SponsorBlockUtils {
public static String getSponsors(String id, String categories)
throws IOException, NoSuchAlgorithmException {
throws IOException {
if (StringUtils.isEmpty(categories))
return Constants.mapper.writeValueAsString(new InvalidRequestResponse());
return mapper.writeValueAsString(new InvalidRequestResponse());
String hash = DigestUtils.sha256Hex(id);
@ -30,11 +27,15 @@ public class SponsorBlockUtils {
+ "?categories=" + URLUtils.silentEncode(categories));
if (resp.status() == 200) {
JsonArray jArray = JsonParser.array().from(new String(resp.body()));
var any = mapper.readTree(resp.body());
jArray.removeIf(jObject -> !((JsonObject) jObject).getString("videoID").equalsIgnoreCase(id));
for (var element : any) {
if (element.get("videoID").asText().equalsIgnoreCase(id)) {
return mapper.writeValueAsString(element);
}
}
return JsonWriter.string(jArray.getObject(0));
return "{}";
}
} catch (Exception ignored) {
}