Remove org.json and fix occasional json exception.

This commit is contained in:
Kavin 2022-10-31 10:43:34 +00:00
parent 12bcc6bc0f
commit d4d9c71279
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
2 changed files with 29 additions and 21 deletions

View File

@ -21,7 +21,6 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.4' implementation 'com.fasterxml.jackson.core:jackson-core:2.13.4'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.4' implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.4'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4.2' implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4.2'
implementation 'org.json:json:20220924'
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1' implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'
implementation 'com.rometools:rome:1.18.0' implementation 'com.rometools:rome:1.18.0'
implementation 'com.github.ipfs:java-ipfs-http-client:v1.3.3' implementation 'com.github.ipfs:java-ipfs-http-client:v1.3.3'

View File

@ -5,10 +5,14 @@ import okhttp3.MediaType;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import static me.kavin.piped.consts.Constants.h2client;
import static me.kavin.piped.consts.Constants.mapper;
import static me.kavin.piped.utils.RequestUtils.sendGet;
import static me.kavin.piped.utils.URLUtils.silentEncode;
public class LbryHelper { public class LbryHelper {
public static String getLBRYId(String videoId) throws IOException { public static String getLBRYId(String videoId) throws IOException {
@ -16,9 +20,10 @@ public class LbryHelper {
if (Constants.DISABLE_LBRY) if (Constants.DISABLE_LBRY)
return null; return null;
return new JSONObject( return mapper.readTree(sendGet("https://api.lbry.com/yt/resolve?video_ids=" + silentEncode(videoId)))
RequestUtils.sendGet("https://api.lbry.com/yt/resolve?video_ids=" + URLUtils.silentEncode(videoId)) .at("/data/videos")
).getJSONObject("data").getJSONObject("videos").optString(videoId, null); .path(videoId)
.asText(null);
} }
public static String getLBRYStreamURL(String lbryId) public static String getLBRYStreamURL(String lbryId)
@ -29,24 +34,28 @@ public class LbryHelper {
var request = new Request.Builder() var request = new Request.Builder()
.url("https://api.lbry.tv/api/v1/proxy?m=get") .url("https://api.lbry.tv/api/v1/proxy?m=get")
.post(RequestBody.create(String.valueOf( .post(RequestBody.create(mapper.writeValueAsBytes(
new JSONObject().put("id", System.currentTimeMillis()) mapper.createObjectNode()
.put("id", System.currentTimeMillis())
.put("id", System.currentTimeMillis())
.put("jsonrpc", "2.0") .put("jsonrpc", "2.0")
.put("method", "get") .put("method", "get")
.put("params", .set("params",
new JSONObject() mapper.createObjectNode()
.put("uri", "lbry://" + lbryId) .put("uri", "lbry://" + lbryId)
.put("save_file", true))) .put("save_file", true)
, MediaType.get("application/json"))) )
), MediaType.get("application/json")))
.build(); .build();
var resp = Constants.h2client.newCall(request).execute(); try (var resp = h2client.newCall(request).execute()) {
if (resp.isSuccessful()) {
var json = new JSONObject(resp.body().string()); return mapper.readTree(resp.body().byteStream())
.at("/result/streaming_url")
resp.close(); .asText(null);
}
return json.getJSONObject("result").getString("streaming_url"); }
return null;
} }
} }