Add comments API.

This commit is contained in:
FireMasterK 2021-04-03 20:38:32 +05:30
parent 009c776c71
commit 35fa684e4b
No known key found for this signature in database
GPG Key ID: 8DFF5DD33E93DB58
4 changed files with 109 additions and 11 deletions

View File

@ -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);

View File

@ -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<Comment> 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<CommentsInfoItem> info = CommentsInfo.getMoreItems(init, new Page(url));
List<Comment> 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;

View File

@ -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;
}
}

View File

@ -0,0 +1,14 @@
package me.kavin.piped.utils.obj;
import java.util.List;
public class CommentsPage {
public List<Comment> comments;
public String nextpage;
public CommentsPage(List<Comment> comments, String nextpage) {
this.comments = comments;
this.nextpage = nextpage;
}
}