From 0c4856933b6245490cb6efe51a3bb32f24dc40fb Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Sun, 8 Aug 2021 16:47:58 +0530 Subject: [PATCH] Add lbryId to streams response. --- .../me/kavin/piped/utils/ResponseHelper.java | 25 +++++++++++++++---- .../me/kavin/piped/utils/obj/Streams.java | 5 ++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 8dd8c8e..c92e33b 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -18,6 +18,7 @@ import java.util.Date; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import javax.persistence.criteria.CriteriaBuilder; @@ -109,9 +110,18 @@ public class ResponseHelper { return null; }); + CompletableFuture futureLbryId = CompletableFuture.supplyAsync(() -> { + try { + return getLBRYId(videoId); + } catch (Exception e) { + ExceptionUtils.rethrow(e); + } + return null; + }); + CompletableFuture futureLBRY = CompletableFuture.supplyAsync(() -> { try { - return getLBRYStreamURL(videoId); + return getLBRYStreamURL(futureLbryId); } catch (Exception e) { ExceptionUtils.rethrow(e); } @@ -183,7 +193,7 @@ public class ResponseHelper { info.getTextualUploadDate(), info.getUploaderName(), info.getUploaderUrl().substring(23), rewriteURL(info.getUploaderAvatarUrl()), rewriteURL(info.getThumbnailUrl()), info.getDuration(), info.getViewCount(), info.getLikeCount(), info.getDislikeCount(), audioStreams, videoStreams, - relatedStreams, subtitles, livestream, hls); + relatedStreams, subtitles, livestream, hls, futureLbryId.get()); return Constants.mapper.writeValueAsBytes(streams); @@ -904,12 +914,17 @@ public class ResponseHelper { } - private static final String getLBRYStreamURL(String videoId) throws IOException, InterruptedException { - - String lbryId = new JSONObject(Constants.h2client.send(HttpRequest + private static final String getLBRYId(String videoId) throws IOException, InterruptedException { + return new JSONObject(Constants.h2client.send(HttpRequest .newBuilder(URI.create("https://api.lbry.com/yt/resolve?video_ids=" + URLUtils.silentEncode(videoId))) .setHeader("User-Agent", Constants.USER_AGENT).build(), BodyHandlers.ofString()).body()) .getJSONObject("data").getJSONObject("videos").optString(videoId); + } + + private static final String getLBRYStreamURL(CompletableFuture futureLbryId) + throws IOException, InterruptedException, ExecutionException { + + String lbryId = futureLbryId.get(); if (!lbryId.isEmpty()) return new JSONObject(Constants.h2client.send(HttpRequest diff --git a/src/main/java/me/kavin/piped/utils/obj/Streams.java b/src/main/java/me/kavin/piped/utils/obj/Streams.java index 2edfda2..b36819c 100644 --- a/src/main/java/me/kavin/piped/utils/obj/Streams.java +++ b/src/main/java/me/kavin/piped/utils/obj/Streams.java @@ -6,7 +6,7 @@ import me.kavin.piped.consts.Constants; public class Streams { - public String title, description, uploadDate, uploader, uploaderUrl, uploaderAvatar, thumbnailUrl, hls; + public String title, description, uploadDate, uploader, uploaderUrl, uploaderAvatar, thumbnailUrl, hls, lbryId; public long duration, views, likes, dislikes; @@ -23,7 +23,7 @@ public class Streams { public Streams(String title, String description, String uploadDate, String uploader, String uploaderUrl, String uploaderAvatar, String thumbnailUrl, long duration, long views, long likes, long dislikes, List audioStreams, List videoStreams, List relatedStreams, - List subtitles, boolean livestream, String hls) { + List subtitles, boolean livestream, String hls, String lbryId) { this.title = title; this.description = description; this.uploadDate = uploadDate; @@ -41,5 +41,6 @@ public class Streams { this.subtitles = subtitles; this.livestream = livestream; this.hls = hls; + this.lbryId = lbryId; } }