mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Cleanup page handling + fix channel continuations.
This commit is contained in:
parent
dd26643ba8
commit
2eb424dcae
7 changed files with 51 additions and 40 deletions
|
@ -100,10 +100,8 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
||||||
}
|
}
|
||||||
})).map("/nextpage/channels/:channelId", AsyncServlet.ofBlocking(executor, request -> {
|
})).map("/nextpage/channels/:channelId", AsyncServlet.ofBlocking(executor, request -> {
|
||||||
try {
|
try {
|
||||||
return getJsonResponse(
|
return getJsonResponse(ResponseHelper.channelPageResponse(request.getPathParameter("channelId"),
|
||||||
ResponseHelper.channelPageResponse(request.getPathParameter("channelId"),
|
request.getQueryParameter("nextpage")), "public, max-age=3600");
|
||||||
request.getQueryParameter("url"), request.getQueryParameter("id")),
|
|
||||||
"public, max-age=3600");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return getErrorResponse(e);
|
return getErrorResponse(e);
|
||||||
}
|
}
|
||||||
|
@ -116,10 +114,8 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
||||||
}
|
}
|
||||||
})).map("/nextpage/playlists/:playlistId", AsyncServlet.ofBlocking(executor, request -> {
|
})).map("/nextpage/playlists/:playlistId", AsyncServlet.ofBlocking(executor, request -> {
|
||||||
try {
|
try {
|
||||||
return getJsonResponse(
|
return getJsonResponse(ResponseHelper.playlistPageResponse(request.getPathParameter("playlistId"),
|
||||||
ResponseHelper.playlistPageResponse(request.getPathParameter("playlistId"),
|
request.getQueryParameter("nextpage")), "public, max-age=3600");
|
||||||
request.getQueryParameter("url"), request.getQueryParameter("id")),
|
|
||||||
"public, max-age=3600");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return getErrorResponse(e);
|
return getErrorResponse(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,14 @@ import java.net.http.HttpClient.Version;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
|
import org.schabi.newpipe.extractor.Page;
|
||||||
import org.schabi.newpipe.extractor.StreamingService;
|
import org.schabi.newpipe.extractor.StreamingService;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.mongodb.client.MongoClient;
|
import com.mongodb.client.MongoClient;
|
||||||
|
|
||||||
|
import me.kavin.piped.utils.PageMixin;
|
||||||
|
|
||||||
public class Constants {
|
public class Constants {
|
||||||
|
|
||||||
public static final boolean debug = false;
|
public static final boolean debug = false;
|
||||||
|
@ -35,7 +38,7 @@ public class Constants {
|
||||||
|
|
||||||
public static final MongoClient mongoClient;
|
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 {
|
static {
|
||||||
Properties prop = new Properties();
|
Properties prop = new Properties();
|
||||||
|
|
16
src/main/java/me/kavin/piped/utils/PageMixin.java
Normal file
16
src/main/java/me/kavin/piped/utils/PageMixin.java
Normal file
|
@ -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<String> ids, @JsonProperty("cookies") Map<String, String> cookies,
|
||||||
|
@JsonProperty("body") byte[] body) {
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,6 @@ import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
@ -179,15 +178,14 @@ public class ResponseHelper {
|
||||||
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
||||||
});
|
});
|
||||||
|
|
||||||
String nextpage = null, body = null;
|
String nextpage = null;
|
||||||
if (info.hasNextPage()) {
|
if (info.hasNextPage()) {
|
||||||
Page page = info.getNextPage();
|
Page page = info.getNextPage();
|
||||||
nextpage = page.getUrl();
|
nextpage = Constants.mapper.writeValueAsString(page);
|
||||||
body = Base64.encodeBase64String(info.getNextPage().getBody());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final Channel channel = new Channel(info.getId(), info.getName(), rewriteURL(info.getAvatarUrl()),
|
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);
|
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 {
|
throws IOException, ExtractionException, InterruptedException {
|
||||||
|
|
||||||
|
Page prevpage = Constants.mapper.readValue(prevpageStr, Page.class);
|
||||||
|
|
||||||
InfoItemsPage<StreamInfoItem> info = ChannelInfo.getMoreItems(Constants.YOUTUBE_SERVICE,
|
InfoItemsPage<StreamInfoItem> 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<StreamItem> relatedStreams = new ObjectArrayList<>();
|
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
||||||
|
|
||||||
|
@ -210,14 +210,13 @@ public class ResponseHelper {
|
||||||
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
||||||
});
|
});
|
||||||
|
|
||||||
String nextpage = null, body = null;
|
String nextpage = null;
|
||||||
if (info.hasNextPage()) {
|
if (info.hasNextPage()) {
|
||||||
Page page = info.getNextPage();
|
Page page = info.getNextPage();
|
||||||
nextpage = page.getUrl();
|
nextpage = Constants.mapper.writeValueAsString(page);
|
||||||
body = Base64.encodeBase64String(info.getNextPage().getBody());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final StreamsPage streamspage = new StreamsPage(nextpage, body, relatedStreams);
|
final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams);
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(streamspage);
|
return Constants.mapper.writeValueAsBytes(streamspage);
|
||||||
|
|
||||||
|
@ -255,15 +254,14 @@ public class ResponseHelper {
|
||||||
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
||||||
});
|
});
|
||||||
|
|
||||||
String nextpage = null, body = null;
|
String nextpage = null;
|
||||||
if (info.hasNextPage()) {
|
if (info.hasNextPage()) {
|
||||||
Page page = info.getNextPage();
|
Page page = info.getNextPage();
|
||||||
nextpage = page.getUrl();
|
nextpage = Constants.mapper.writeValueAsString(page);
|
||||||
body = Base64.encodeBase64String(info.getNextPage().getBody());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final Playlist playlist = new Playlist(info.getName(), rewriteURL(info.getThumbnailUrl()),
|
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.getUploaderName().isEmpty() ? null : info.getUploaderName(),
|
||||||
info.getUploaderUrl().isEmpty() ? null : info.getUploaderUrl().substring(23),
|
info.getUploaderUrl().isEmpty() ? null : info.getUploaderUrl().substring(23),
|
||||||
rewriteURL(info.getUploaderAvatarUrl()), (int) info.getStreamCount(), relatedStreams);
|
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 {
|
throws IOException, ExtractionException, InterruptedException {
|
||||||
|
|
||||||
|
Page prevpage = Constants.mapper.readValue(prevpageStr, Page.class);
|
||||||
|
|
||||||
InfoItemsPage<StreamInfoItem> info = PlaylistInfo.getMoreItems(Constants.YOUTUBE_SERVICE,
|
InfoItemsPage<StreamInfoItem> 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<StreamItem> relatedStreams = new ObjectArrayList<>();
|
final List<StreamItem> relatedStreams = new ObjectArrayList<>();
|
||||||
|
|
||||||
|
@ -287,14 +287,13 @@ public class ResponseHelper {
|
||||||
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
item.getTextualUploadDate(), item.getDuration(), item.getViewCount()));
|
||||||
});
|
});
|
||||||
|
|
||||||
String nextpage = null, body = null;
|
String nextpage = null;
|
||||||
if (info.hasNextPage()) {
|
if (info.hasNextPage()) {
|
||||||
Page page = info.getNextPage();
|
Page page = info.getNextPage();
|
||||||
nextpage = page.getUrl();
|
nextpage = Constants.mapper.writeValueAsString(page);
|
||||||
body = Base64.encodeBase64String(info.getNextPage().getBody());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final StreamsPage streamspage = new StreamsPage(nextpage, body, relatedStreams);
|
final StreamsPage streamspage = new StreamsPage(nextpage, relatedStreams);
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(streamspage);
|
return Constants.mapper.writeValueAsBytes(streamspage);
|
||||||
|
|
||||||
|
|
|
@ -4,18 +4,17 @@ import java.util.List;
|
||||||
|
|
||||||
public class Channel {
|
public class Channel {
|
||||||
|
|
||||||
public String id, name, avatarUrl, bannerUrl, description, nextpage, nextbody;
|
public String id, name, avatarUrl, bannerUrl, description, nextpage;
|
||||||
public List<StreamItem> relatedStreams;
|
public List<StreamItem> relatedStreams;
|
||||||
|
|
||||||
public Channel(String id, String name, String avatarUrl, String bannerUrl, String description, String nextpage,
|
public Channel(String id, String name, String avatarUrl, String bannerUrl, String description, String nextpage,
|
||||||
String nextbody, List<StreamItem> relatedStreams) {
|
List<StreamItem> relatedStreams) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.avatarUrl = avatarUrl;
|
this.avatarUrl = avatarUrl;
|
||||||
this.bannerUrl = bannerUrl;
|
this.bannerUrl = bannerUrl;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.nextpage = nextpage;
|
this.nextpage = nextpage;
|
||||||
this.nextbody = nextbody;
|
|
||||||
this.relatedStreams = relatedStreams;
|
this.relatedStreams = relatedStreams;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,16 @@ import java.util.List;
|
||||||
|
|
||||||
public class Playlist {
|
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 int videos;
|
||||||
public List<StreamItem> relatedStreams;
|
public List<StreamItem> relatedStreams;
|
||||||
|
|
||||||
public Playlist(String name, String thumbnailUrl, String bannerUrl, String nextpage, String nextbody,
|
public Playlist(String name, String thumbnailUrl, String bannerUrl, String nextpage, String uploader,
|
||||||
String uploader, String uploaderUrl, String uploaderAvatar, int videos, List<StreamItem> relatedStreams) {
|
String uploaderUrl, String uploaderAvatar, int videos, List<StreamItem> relatedStreams) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.thumbnailUrl = thumbnailUrl;
|
this.thumbnailUrl = thumbnailUrl;
|
||||||
this.bannerUrl = bannerUrl;
|
this.bannerUrl = bannerUrl;
|
||||||
this.nextpage = nextpage;
|
this.nextpage = nextpage;
|
||||||
this.nextbody = nextbody;
|
|
||||||
this.videos = videos;
|
this.videos = videos;
|
||||||
this.uploader = uploader;
|
this.uploader = uploader;
|
||||||
this.uploaderUrl = uploaderUrl;
|
this.uploaderUrl = uploaderUrl;
|
||||||
|
|
|
@ -4,12 +4,11 @@ import java.util.List;
|
||||||
|
|
||||||
public class StreamsPage {
|
public class StreamsPage {
|
||||||
|
|
||||||
public String nextpage, nextbody;
|
public String nextpage;
|
||||||
public List<StreamItem> relatedStreams;
|
public List<StreamItem> relatedStreams;
|
||||||
|
|
||||||
public StreamsPage(String nextpage, String nextbody, List<StreamItem> relatedStreams) {
|
public StreamsPage(String nextpage, List<StreamItem> relatedStreams) {
|
||||||
this.nextpage = nextpage;
|
this.nextpage = nextpage;
|
||||||
this.nextbody = nextbody;
|
|
||||||
this.relatedStreams = relatedStreams;
|
this.relatedStreams = relatedStreams;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue