Cleanup page handling + fix channel continuations.

This commit is contained in:
FireMasterK 2021-06-06 01:04:33 +05:30
parent dd26643ba8
commit 2eb424dcae
No known key found for this signature in database
GPG key ID: 8DFF5DD33E93DB58
7 changed files with 51 additions and 40 deletions

View file

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

View file

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

View 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) {
}
}

View file

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

View file

@ -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<StreamItem> relatedStreams;
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.name = name;
this.avatarUrl = avatarUrl;
this.bannerUrl = bannerUrl;
this.description = description;
this.nextpage = nextpage;
this.nextbody = nextbody;
this.relatedStreams = relatedStreams;
}
}

View file

@ -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<StreamItem> relatedStreams;
public Playlist(String name, String thumbnailUrl, String bannerUrl, String nextpage, String nextbody,
String uploader, String uploaderUrl, String uploaderAvatar, int videos, List<StreamItem> relatedStreams) {
public Playlist(String name, String thumbnailUrl, String bannerUrl, String nextpage, String uploader,
String uploaderUrl, String uploaderAvatar, int videos, List<StreamItem> 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;

View file

@ -4,12 +4,11 @@ import java.util.List;
public class StreamsPage {
public String nextpage, nextbody;
public String nextpage;
public List<StreamItem> relatedStreams;
public StreamsPage(String nextpage, String nextbody, List<StreamItem> relatedStreams) {
public StreamsPage(String nextpage, List<StreamItem> relatedStreams) {
this.nextpage = nextpage;
this.nextbody = nextbody;
this.relatedStreams = relatedStreams;
}
}