Merge pull request #556 from TeamPiped/sb-fix

Cleanup SponsorBlock utils to fix issues.
This commit is contained in:
Kavin 2023-03-18 18:18:09 +00:00 committed by GitHub
commit 5735622800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,5 @@
package me.kavin.piped.utils; 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.consts.Constants;
import me.kavin.piped.utils.resp.InvalidRequestResponse; import me.kavin.piped.utils.resp.InvalidRequestResponse;
import me.kavin.piped.utils.resp.SimpleErrorMessage; 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 org.apache.commons.lang3.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import static me.kavin.piped.consts.Constants.mapper;
public class SponsorBlockUtils { public class SponsorBlockUtils {
public static String getSponsors(String id, String categories) public static String getSponsors(String id, String categories)
throws IOException, NoSuchAlgorithmException { throws IOException {
if (StringUtils.isEmpty(categories)) if (StringUtils.isEmpty(categories))
return Constants.mapper.writeValueAsString(new InvalidRequestResponse()); return mapper.writeValueAsString(new InvalidRequestResponse());
String hash = DigestUtils.sha256Hex(id); String hash = DigestUtils.sha256Hex(id);
@ -30,11 +27,15 @@ public class SponsorBlockUtils {
+ "?categories=" + URLUtils.silentEncode(categories)); + "?categories=" + URLUtils.silentEncode(categories));
if (resp.status() == 200) { 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) { } catch (Exception ignored) {
} }