mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Add comments API.
This commit is contained in:
parent
009c776c71
commit
35fa684e4b
4 changed files with 109 additions and 11 deletions
|
@ -137,6 +137,20 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return getErrorResponse(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);
|
return new CustomServletDecorator(router);
|
||||||
|
|
|
@ -19,6 +19,7 @@ import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
|
||||||
import org.schabi.newpipe.extractor.Page;
|
import org.schabi.newpipe.extractor.Page;
|
||||||
import org.schabi.newpipe.extractor.channel.ChannelInfo;
|
import org.schabi.newpipe.extractor.channel.ChannelInfo;
|
||||||
import org.schabi.newpipe.extractor.comments.CommentsInfo;
|
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.ExtractionException;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
import org.schabi.newpipe.extractor.kiosk.KioskInfo;
|
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.ipfs.IPFS;
|
||||||
import me.kavin.piped.utils.obj.Channel;
|
import me.kavin.piped.utils.obj.Channel;
|
||||||
import me.kavin.piped.utils.obj.ChapterSegment;
|
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.PipedStream;
|
||||||
import me.kavin.piped.utils.obj.Playlist;
|
import me.kavin.piped.utils.obj.Playlist;
|
||||||
import me.kavin.piped.utils.obj.SearchResults;
|
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 {
|
public static final byte[] registerResponse(String user, String pass) throws IOException {
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(null);
|
return Constants.mapper.writeValueAsBytes(null);
|
||||||
|
@ -376,17 +429,12 @@ public class ResponseHelper {
|
||||||
.getJSONObject("data").getJSONObject("videos").optString(videoId);
|
.getJSONObject("data").getJSONObject("videos").optString(videoId);
|
||||||
|
|
||||||
if (!lbryId.isEmpty())
|
if (!lbryId.isEmpty())
|
||||||
return rewriteURL(
|
new JSONObject(Constants.h2client.send(HttpRequest
|
||||||
new JSONObject(
|
.newBuilder(URI.create("https://api.lbry.tv/api/v1/proxy?m=get"))
|
||||||
Constants.h2client.send(
|
.POST(BodyPublishers.ofString(
|
||||||
HttpRequest.newBuilder(URI.create("https://api.lbry.tv/api/v1/proxy?m=get"))
|
String.valueOf(new JSONObject().put("jsonrpc", "2.0").put("method", "get").put("params",
|
||||||
.POST(BodyPublishers.ofString(String.valueOf(new JSONObject()
|
new JSONObject().put("uri", "lbry://" + lbryId).put("save_file", true)))))
|
||||||
.put("jsonrpc", "2.0").put("method", "get").put("params",
|
.build(), BodyHandlers.ofString()).body()).getJSONObject("result").getString("streaming_url");
|
||||||
new JSONObject().put("uri", "lbry://" + lbryId)
|
|
||||||
.put("save_file", true)))))
|
|
||||||
.build(),
|
|
||||||
BodyHandlers.ofString()).body()).getJSONObject("result")
|
|
||||||
.getString("streaming_url"));
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
|
22
src/main/java/me/kavin/piped/utils/obj/Comment.java
Normal file
22
src/main/java/me/kavin/piped/utils/obj/Comment.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
14
src/main/java/me/kavin/piped/utils/obj/CommentsPage.java
Normal file
14
src/main/java/me/kavin/piped/utils/obj/CommentsPage.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue