From 69814ddc30f611b7cb81b138096c2a12dce12b21 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Sat, 18 Mar 2023 18:16:39 +0000 Subject: [PATCH] Cleanup SponsorBlock utils to fix issues. --- .../kavin/piped/utils/SponsorBlockUtils.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java b/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java index 6a57b94..cd27a16 100644 --- a/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java +++ b/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java @@ -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) { }