Next page in search results.

This commit is contained in:
FireMasterK 2020-12-14 12:41:42 +05:30
parent 0151b6f2d8
commit 4986fe864c
No known key found for this signature in database
GPG Key ID: 8DFF5DD33E93DB58
3 changed files with 72 additions and 1 deletions

View File

@ -138,6 +138,22 @@ public class Main {
});
routes.get("/nextpage/search", (req, res) -> {
QueryStringDecoder query = new QueryStringDecoder(req.uri());
try {
return writeResponse(res,
ResponseHelper.searchPageResponse(query.parameters().get("q").get(0),
query.parameters().get("url").get(0), query.parameters().get("id").get(0)),
200, "public, max-age=3600");
} catch (Exception e) {
e.printStackTrace();
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private");
}
});
routes.get("/trending", (req, res) -> {
try {

View File

@ -14,6 +14,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.json.JSONObject;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.channel.ChannelInfo;
@ -35,6 +36,7 @@ import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.Channel;
import me.kavin.piped.utils.obj.ChannelPage;
import me.kavin.piped.utils.obj.PipedStream;
import me.kavin.piped.utils.obj.SearchResults;
import me.kavin.piped.utils.obj.StreamItem;
import me.kavin.piped.utils.obj.Streams;
import me.kavin.piped.utils.obj.Subtitle;
@ -231,7 +233,43 @@ public class ResponseHelper {
}
});
return Constants.mapper.writeValueAsString(items);
Page nextpage = info.getNextPage();
return nextpage != null
? Constants.mapper.writeValueAsString(new SearchResults(nextpage.getUrl(), nextpage.getId(), items))
: Constants.mapper.writeValueAsString(new SearchResults(null, null, items));
}
public static final String searchPageResponse(String q, String url, String id)
throws IOException, ExtractionException, InterruptedException {
InfoItemsPage<InfoItem> pages = SearchInfo.getMoreItems(Constants.YOUTUBE_SERVICE,
Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q), new Page(url, id));
ObjectArrayList<SearchItem> items = new ObjectArrayList<>();
pages.getItems().forEach(item -> {
switch (item.getInfoType()) {
case STREAM:
StreamInfoItem stream = (StreamInfoItem) item;
items.add(new SearchStream(item.getName(), rewriteURL(item.getThumbnailUrl()),
item.getUrl().substring(23), stream.getViewCount(), stream.getDuration()));
break;
case CHANNEL:
items.add(new SearchItem(item.getName(), rewriteURL(item.getThumbnailUrl()),
item.getUrl().substring(23)));
break;
default:
break;
}
});
Page nextpage = pages.getNextPage();
return nextpage != null
? Constants.mapper.writeValueAsString(new SearchResults(nextpage.getUrl(), nextpage.getId(), items))
: Constants.mapper.writeValueAsString(new SearchResults(null, null, items));
}
@ -288,6 +326,7 @@ public class ResponseHelper {
if (path.startsWith("/vi/") && !path.contains("_live")) {
path = path.replace("/vi/", "/vi_webp/").replace(".jpg", ".webp").replace("hq720", "mqdefault")
.replace("hqdefault", "mqdefault");
hasQuery = false;
}

View File

@ -0,0 +1,16 @@
package me.kavin.piped.utils.obj;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.kavin.piped.utils.obj.search.SearchItem;
public class SearchResults {
public String nextpage, id;
public ObjectArrayList<SearchItem> items;
public SearchResults(String nextpage, String id, ObjectArrayList<SearchItem> items) {
this.nextpage = nextpage;
this.id = id;
this.items = items;
}
}