From 35fa684e4b78d4e8c248734b433c1b1a51ba69cb Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Sat, 3 Apr 2021 20:38:32 +0530 Subject: [PATCH] Add comments API. --- .../java/me/kavin/piped/ServerLauncher.java | 14 ++++ .../me/kavin/piped/utils/ResponseHelper.java | 70 ++++++++++++++++--- .../me/kavin/piped/utils/obj/Comment.java | 22 ++++++ .../kavin/piped/utils/obj/CommentsPage.java | 14 ++++ 4 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 src/main/java/me/kavin/piped/utils/obj/Comment.java create mode 100644 src/main/java/me/kavin/piped/utils/obj/CommentsPage.java diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index c3fe532..434770a 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -137,6 +137,20 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } catch (Exception e) { return getErrorResponse(e); } + })).map("/comments/:videoId", AsyncServlet.ofBlocking(executor, request -> { + try { + return getJsonResponse(ResponseHelper.commentsResponse(request.getPathParameter("videoId")), + "public, s-maxage=1200"); + } catch (Exception e) { + return getErrorResponse(e); + } + })).map("/nextpage/comments/:videoId", AsyncServlet.ofBlocking(executor, request -> { + try { + return getJsonResponse(ResponseHelper.commentsPageResponse(request.getPathParameter("videoId"), + request.getQueryParameter("url")), "public, s-maxage=3600"); + } catch (Exception e) { + return getErrorResponse(e); + } })); return new CustomServletDecorator(router); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 7e25b0d..6e0720a 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -19,6 +19,7 @@ import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage; import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.channel.ChannelInfo; import org.schabi.newpipe.extractor.comments.CommentsInfo; +import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.kiosk.KioskInfo; @@ -36,6 +37,8 @@ import me.kavin.piped.consts.Constants; import me.kavin.piped.ipfs.IPFS; import me.kavin.piped.utils.obj.Channel; import me.kavin.piped.utils.obj.ChapterSegment; +import me.kavin.piped.utils.obj.Comment; +import me.kavin.piped.utils.obj.CommentsPage; import me.kavin.piped.utils.obj.PipedStream; import me.kavin.piped.utils.obj.Playlist; import me.kavin.piped.utils.obj.SearchResults; @@ -362,6 +365,56 @@ public class ResponseHelper { } + public static final byte[] commentsResponse(String videoId) throws Exception { + + CommentsInfo info = commentsCache.get(videoId); + + List comments = new ObjectArrayList<>(); + + info.getRelatedItems().forEach(comment -> { + comments.add(new Comment(comment.getUploaderName(), rewriteURL(comment.getUploaderAvatarUrl()), + comment.getCommentId(), comment.getCommentText(), comment.getTextualUploadDate(), + comment.getUploaderUrl().substring(23), comment.getLikeCount(), comment.isHeartedByUploader(), + comment.isPinned(), comment.isUploaderVerified())); + }); + + String nextpage = null; + + if (info.getNextPage() != null) + nextpage = info.getNextPage().getUrl(); + + CommentsPage commentsItem = new CommentsPage(comments, nextpage); + + return Constants.mapper.writeValueAsBytes(commentsItem); + + } + + public static final byte[] commentsPageResponse(String videoId, String url) throws Exception { + + CommentsInfo init = commentsCache.get(videoId); + + InfoItemsPage info = CommentsInfo.getMoreItems(init, new Page(url)); + + List comments = new ObjectArrayList<>(); + + info.getItems().forEach(comment -> { + comments.add(new Comment(comment.getUploaderName(), rewriteURL(comment.getUploaderAvatarUrl()), + comment.getCommentId(), comment.getCommentText(), comment.getTextualUploadDate(), + comment.getUploaderUrl().substring(23), comment.getLikeCount(), comment.isHeartedByUploader(), + comment.isPinned(), comment.isUploaderVerified())); + }); + + String nextpage = null; + + if (info.getNextPage() != null) + nextpage = info.getNextPage().getUrl(); + + CommentsPage commentsItem = new CommentsPage(comments, nextpage); + + return Constants.mapper.writeValueAsBytes(commentsItem); + + } + public static final byte[] registerResponse(String user, String pass) throws IOException { return Constants.mapper.writeValueAsBytes(null); @@ -376,17 +429,12 @@ public class ResponseHelper { .getJSONObject("data").getJSONObject("videos").optString(videoId); if (!lbryId.isEmpty()) - return rewriteURL( - new JSONObject( - Constants.h2client.send( - HttpRequest.newBuilder(URI.create("https://api.lbry.tv/api/v1/proxy?m=get")) - .POST(BodyPublishers.ofString(String.valueOf(new JSONObject() - .put("jsonrpc", "2.0").put("method", "get").put("params", - new JSONObject().put("uri", "lbry://" + lbryId) - .put("save_file", true))))) - .build(), - BodyHandlers.ofString()).body()).getJSONObject("result") - .getString("streaming_url")); + new JSONObject(Constants.h2client.send(HttpRequest + .newBuilder(URI.create("https://api.lbry.tv/api/v1/proxy?m=get")) + .POST(BodyPublishers.ofString( + String.valueOf(new JSONObject().put("jsonrpc", "2.0").put("method", "get").put("params", + new JSONObject().put("uri", "lbry://" + lbryId).put("save_file", true))))) + .build(), BodyHandlers.ofString()).body()).getJSONObject("result").getString("streaming_url"); return null; diff --git a/src/main/java/me/kavin/piped/utils/obj/Comment.java b/src/main/java/me/kavin/piped/utils/obj/Comment.java new file mode 100644 index 0000000..8fd9a12 --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/obj/Comment.java @@ -0,0 +1,22 @@ +package me.kavin.piped.utils.obj; + +public class Comment { + + public String author, thumbnail, commentId, commentText, commentedTime, commentorUrl; + public int likeCount; + public boolean hearted, pinned, verified; + + public Comment(String author, String thumbnail, String commentId, String commentText, String commentedTime, + String commentorUrl, int likeCount, boolean hearted, boolean pinned, boolean verified) { + this.author = author; + this.thumbnail = thumbnail; + this.commentId = commentId; + this.commentText = commentText; + this.commentedTime = commentedTime; + this.commentorUrl = commentorUrl; + this.likeCount = likeCount; + this.hearted = hearted; + this.pinned = pinned; + this.verified = verified; + } +} diff --git a/src/main/java/me/kavin/piped/utils/obj/CommentsPage.java b/src/main/java/me/kavin/piped/utils/obj/CommentsPage.java new file mode 100644 index 0000000..554ba48 --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/obj/CommentsPage.java @@ -0,0 +1,14 @@ +package me.kavin.piped.utils.obj; + +import java.util.List; + +public class CommentsPage { + + public List comments; + public String nextpage; + + public CommentsPage(List comments, String nextpage) { + this.comments = comments; + this.nextpage = nextpage; + } +}