From 2eb424dcae2fc78246516658f84ac3c359ad8305 Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Sun, 6 Jun 2021 01:04:33 +0530 Subject: [PATCH] Cleanup page handling + fix channel continuations. --- .../java/me/kavin/piped/ServerLauncher.java | 12 ++---- .../java/me/kavin/piped/consts/Constants.java | 5 ++- .../java/me/kavin/piped/utils/PageMixin.java | 16 ++++++++ .../me/kavin/piped/utils/ResponseHelper.java | 41 +++++++++---------- .../me/kavin/piped/utils/obj/Channel.java | 5 +-- .../me/kavin/piped/utils/obj/Playlist.java | 7 ++-- .../me/kavin/piped/utils/obj/StreamsPage.java | 5 +-- 7 files changed, 51 insertions(+), 40 deletions(-) create mode 100644 src/main/java/me/kavin/piped/utils/PageMixin.java diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 20f572b..5b3147c 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -100,10 +100,8 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } })).map("/nextpage/channels/:channelId", AsyncServlet.ofBlocking(executor, request -> { try { - return getJsonResponse( - ResponseHelper.channelPageResponse(request.getPathParameter("channelId"), - request.getQueryParameter("url"), request.getQueryParameter("id")), - "public, max-age=3600"); + return getJsonResponse(ResponseHelper.channelPageResponse(request.getPathParameter("channelId"), + request.getQueryParameter("nextpage")), "public, max-age=3600"); } catch (Exception e) { return getErrorResponse(e); } @@ -116,10 +114,8 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } })).map("/nextpage/playlists/:playlistId", AsyncServlet.ofBlocking(executor, request -> { try { - return getJsonResponse( - ResponseHelper.playlistPageResponse(request.getPathParameter("playlistId"), - request.getQueryParameter("url"), request.getQueryParameter("id")), - "public, max-age=3600"); + return getJsonResponse(ResponseHelper.playlistPageResponse(request.getPathParameter("playlistId"), + request.getQueryParameter("nextpage")), "public, max-age=3600"); } catch (Exception e) { return getErrorResponse(e); } diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index d79544c..05c811a 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -7,11 +7,14 @@ import java.net.http.HttpClient.Version; import java.util.Properties; import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.StreamingService; import com.fasterxml.jackson.databind.ObjectMapper; import com.mongodb.client.MongoClient; +import me.kavin.piped.utils.PageMixin; + public class Constants { public static final boolean debug = false; @@ -35,7 +38,7 @@ public class Constants { public static final MongoClient mongoClient; - public static final ObjectMapper mapper = new ObjectMapper(); + public static final ObjectMapper mapper = new ObjectMapper().addMixIn(Page.class, PageMixin.class); static { Properties prop = new Properties(); diff --git a/src/main/java/me/kavin/piped/utils/PageMixin.java b/src/main/java/me/kavin/piped/utils/PageMixin.java new file mode 100644 index 0000000..6bf48ac --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/PageMixin.java @@ -0,0 +1,16 @@ +package me.kavin.piped.utils; + +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public abstract class PageMixin { + + @JsonCreator + public PageMixin(@JsonProperty("url") String url, @JsonProperty("id") String id, + @JsonProperty("ids") List ids, @JsonProperty("cookies") Map cookies, + @JsonProperty("body") byte[] body) { + } +} diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 29dd61f..902d3ad 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -11,7 +11,6 @@ import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; -import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.json.JSONObject; @@ -179,15 +178,14 @@ public class ResponseHelper { item.getTextualUploadDate(), item.getDuration(), item.getViewCount())); }); - String nextpage = null, body = null; + String nextpage = null; if (info.hasNextPage()) { Page page = info.getNextPage(); - nextpage = page.getUrl(); - body = Base64.encodeBase64String(info.getNextPage().getBody()); + nextpage = Constants.mapper.writeValueAsString(page); } final Channel channel = new Channel(info.getId(), info.getName(), rewriteURL(info.getAvatarUrl()), - rewriteURL(info.getBannerUrl()), info.getDescription(), nextpage, body, relatedStreams); + rewriteURL(info.getBannerUrl()), info.getDescription(), nextpage, relatedStreams); IPFS.publishData(channel); @@ -195,11 +193,13 @@ public class ResponseHelper { } - public static final byte[] channelPageResponse(String channelId, String url, String body_req) + public static final byte[] channelPageResponse(String channelId, String prevpageStr) throws IOException, ExtractionException, InterruptedException { + Page prevpage = Constants.mapper.readValue(prevpageStr, Page.class); + InfoItemsPage info = ChannelInfo.getMoreItems(Constants.YOUTUBE_SERVICE, - "https://youtube.com/channel/" + channelId, new Page(url, Base64.decodeBase64(body_req))); + "https://youtube.com/channel/" + channelId, prevpage); final List relatedStreams = new ObjectArrayList<>(); @@ -210,14 +210,13 @@ public class ResponseHelper { item.getTextualUploadDate(), item.getDuration(), item.getViewCount())); }); - String nextpage = null, body = null; + String nextpage = null; if (info.hasNextPage()) { Page page = info.getNextPage(); - nextpage = page.getUrl(); - body = Base64.encodeBase64String(info.getNextPage().getBody()); + nextpage = Constants.mapper.writeValueAsString(page); } - final StreamsPage streamspage = new StreamsPage(nextpage, body, relatedStreams); + final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams); return Constants.mapper.writeValueAsBytes(streamspage); @@ -255,15 +254,14 @@ public class ResponseHelper { item.getTextualUploadDate(), item.getDuration(), item.getViewCount())); }); - String nextpage = null, body = null; + String nextpage = null; if (info.hasNextPage()) { Page page = info.getNextPage(); - nextpage = page.getUrl(); - body = Base64.encodeBase64String(info.getNextPage().getBody()); + nextpage = Constants.mapper.writeValueAsString(page); } final Playlist playlist = new Playlist(info.getName(), rewriteURL(info.getThumbnailUrl()), - rewriteURL(info.getBannerUrl()), nextpage, body, + rewriteURL(info.getBannerUrl()), nextpage, info.getUploaderName().isEmpty() ? null : info.getUploaderName(), info.getUploaderUrl().isEmpty() ? null : info.getUploaderUrl().substring(23), rewriteURL(info.getUploaderAvatarUrl()), (int) info.getStreamCount(), relatedStreams); @@ -272,11 +270,13 @@ public class ResponseHelper { } - public static final byte[] playlistPageResponse(String playlistId, String url, String body_req) + public static final byte[] playlistPageResponse(String playlistId, String prevpageStr) throws IOException, ExtractionException, InterruptedException { + Page prevpage = Constants.mapper.readValue(prevpageStr, Page.class); + InfoItemsPage info = PlaylistInfo.getMoreItems(Constants.YOUTUBE_SERVICE, - "https://www.youtube.com/playlist?list=" + playlistId, new Page(url, Base64.decodeBase64(body_req))); + "https://www.youtube.com/playlist?list=" + playlistId, prevpage); final List relatedStreams = new ObjectArrayList<>(); @@ -287,14 +287,13 @@ public class ResponseHelper { item.getTextualUploadDate(), item.getDuration(), item.getViewCount())); }); - String nextpage = null, body = null; + String nextpage = null; if (info.hasNextPage()) { Page page = info.getNextPage(); - nextpage = page.getUrl(); - body = Base64.encodeBase64String(info.getNextPage().getBody()); + nextpage = Constants.mapper.writeValueAsString(page); } - final StreamsPage streamspage = new StreamsPage(nextpage, body, relatedStreams); + final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams); return Constants.mapper.writeValueAsBytes(streamspage); diff --git a/src/main/java/me/kavin/piped/utils/obj/Channel.java b/src/main/java/me/kavin/piped/utils/obj/Channel.java index 646ba02..2138bb7 100644 --- a/src/main/java/me/kavin/piped/utils/obj/Channel.java +++ b/src/main/java/me/kavin/piped/utils/obj/Channel.java @@ -4,18 +4,17 @@ import java.util.List; public class Channel { - public String id, name, avatarUrl, bannerUrl, description, nextpage, nextbody; + public String id, name, avatarUrl, bannerUrl, description, nextpage; public List relatedStreams; public Channel(String id, String name, String avatarUrl, String bannerUrl, String description, String nextpage, - String nextbody, List relatedStreams) { + List relatedStreams) { this.id = id; this.name = name; this.avatarUrl = avatarUrl; this.bannerUrl = bannerUrl; this.description = description; this.nextpage = nextpage; - this.nextbody = nextbody; this.relatedStreams = relatedStreams; } } diff --git a/src/main/java/me/kavin/piped/utils/obj/Playlist.java b/src/main/java/me/kavin/piped/utils/obj/Playlist.java index 6873fdb..7124eb8 100644 --- a/src/main/java/me/kavin/piped/utils/obj/Playlist.java +++ b/src/main/java/me/kavin/piped/utils/obj/Playlist.java @@ -4,17 +4,16 @@ import java.util.List; public class Playlist { - public String name, thumbnailUrl, bannerUrl, nextpage, nextbody, uploader, uploaderUrl, uploaderAvatar; + public String name, thumbnailUrl, bannerUrl, nextpage, uploader, uploaderUrl, uploaderAvatar; public int videos; public List relatedStreams; - public Playlist(String name, String thumbnailUrl, String bannerUrl, String nextpage, String nextbody, - String uploader, String uploaderUrl, String uploaderAvatar, int videos, List relatedStreams) { + public Playlist(String name, String thumbnailUrl, String bannerUrl, String nextpage, String uploader, + String uploaderUrl, String uploaderAvatar, int videos, List relatedStreams) { this.name = name; this.thumbnailUrl = thumbnailUrl; this.bannerUrl = bannerUrl; this.nextpage = nextpage; - this.nextbody = nextbody; this.videos = videos; this.uploader = uploader; this.uploaderUrl = uploaderUrl; diff --git a/src/main/java/me/kavin/piped/utils/obj/StreamsPage.java b/src/main/java/me/kavin/piped/utils/obj/StreamsPage.java index 059485d..e6d729c 100644 --- a/src/main/java/me/kavin/piped/utils/obj/StreamsPage.java +++ b/src/main/java/me/kavin/piped/utils/obj/StreamsPage.java @@ -4,12 +4,11 @@ import java.util.List; public class StreamsPage { - public String nextpage, nextbody; + public String nextpage; public List relatedStreams; - public StreamsPage(String nextpage, String nextbody, List relatedStreams) { + public StreamsPage(String nextpage, List relatedStreams) { this.nextpage = nextpage; - this.nextbody = nextbody; this.relatedStreams = relatedStreams; } }