From d4d9c712797c5ca8808754ceeb1943f1b3f82480 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:43:34 +0000 Subject: [PATCH 1/3] Remove org.json and fix occasional json exception. --- build.gradle | 1 - .../java/me/kavin/piped/utils/LbryHelper.java | 49 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/build.gradle b/build.gradle index 7708164..f52eb75 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,6 @@ dependencies { 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-databind:2.13.4.2' - implementation 'org.json:json:20220924' implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1' implementation 'com.rometools:rome:1.18.0' implementation 'com.github.ipfs:java-ipfs-http-client:v1.3.3' diff --git a/src/main/java/me/kavin/piped/utils/LbryHelper.java b/src/main/java/me/kavin/piped/utils/LbryHelper.java index 620eeeb..c13d938 100644 --- a/src/main/java/me/kavin/piped/utils/LbryHelper.java +++ b/src/main/java/me/kavin/piped/utils/LbryHelper.java @@ -5,10 +5,14 @@ import okhttp3.MediaType; import okhttp3.Request; import okhttp3.RequestBody; import org.apache.commons.lang3.StringUtils; -import org.json.JSONObject; 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 static String getLBRYId(String videoId) throws IOException { @@ -16,9 +20,10 @@ public class LbryHelper { if (Constants.DISABLE_LBRY) return null; - return new JSONObject( - RequestUtils.sendGet("https://api.lbry.com/yt/resolve?video_ids=" + URLUtils.silentEncode(videoId)) - ).getJSONObject("data").getJSONObject("videos").optString(videoId, null); + return mapper.readTree(sendGet("https://api.lbry.com/yt/resolve?video_ids=" + silentEncode(videoId))) + .at("/data/videos") + .path(videoId) + .asText(null); } public static String getLBRYStreamURL(String lbryId) @@ -29,24 +34,28 @@ public class LbryHelper { var request = new Request.Builder() .url("https://api.lbry.tv/api/v1/proxy?m=get") - .post(RequestBody.create(String.valueOf( - new JSONObject().put("id", System.currentTimeMillis()) - .put("jsonrpc", "2.0") - .put("method", "get") - .put("params", - new JSONObject() - .put("uri", "lbry://" + lbryId) - .put("save_file", true))) - , MediaType.get("application/json"))) + .post(RequestBody.create(mapper.writeValueAsBytes( + mapper.createObjectNode() + .put("id", System.currentTimeMillis()) + .put("id", System.currentTimeMillis()) + .put("jsonrpc", "2.0") + .put("method", "get") + .set("params", + mapper.createObjectNode() + .put("uri", "lbry://" + lbryId) + .put("save_file", true) + ) + ), MediaType.get("application/json"))) .build(); - var resp = Constants.h2client.newCall(request).execute(); - - var json = new JSONObject(resp.body().string()); - - resp.close(); - - return json.getJSONObject("result").getString("streaming_url"); + try (var resp = h2client.newCall(request).execute()) { + if (resp.isSuccessful()) { + return mapper.readTree(resp.body().byteStream()) + .at("/result/streaming_url") + .asText(null); + } + } + return null; } } From 322769b0e46949469696f391f8633b928eb327b5 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:44:00 +0000 Subject: [PATCH 2/3] Use streaming api directly from query. --- .../server/handlers/auth/FeedHandlers.java | 75 +++++++++---------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java b/src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java index 2162a4b..23db0be 100644 --- a/src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java @@ -119,16 +119,15 @@ public class FeedHandlers { ) .orderBy(cb.desc(root.get("uploaded"))); - List feedItems = new ObjectArrayList<>(); + List feedItems = s.createQuery(criteria).setTimeout(20).stream() + .parallel().map(video -> { + var channel = video.getChannel(); - for (Video video : s.createQuery(criteria).setTimeout(20).list()) { - var channel = video.getChannel(); - - feedItems.add(new StreamItem("/watch?v=" + video.getId(), video.getTitle(), - rewriteURL(video.getThumbnail()), channel.getUploader(), "/channel/" + channel.getUploaderId(), - rewriteURL(channel.getUploaderAvatar()), null, null, video.getDuration(), video.getViews(), - video.getUploaded(), channel.isVerified(), video.isShort())); - } + return new StreamItem("/watch?v=" + video.getId(), video.getTitle(), + rewriteURL(video.getThumbnail()), channel.getUploader(), "/channel/" + channel.getUploaderId(), + rewriteURL(channel.getUploaderAvatar()), null, null, video.getDuration(), video.getViews(), + video.getUploaded(), channel.isVerified(), video.isShort()); + }).toList(); return mapper.writeValueAsBytes(feedItems); } @@ -173,29 +172,27 @@ public class FeedHandlers { ) .orderBy(cb.desc(root.get("uploaded"))); - List