diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 3497533..50ea1d7 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -131,8 +131,8 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } })).map("/search", AsyncServlet.ofBlocking(executor, request -> { try { - return getJsonResponse(ResponseHelper.searchResponse(request.getQueryParameter("q")), - "public, max-age=600"); + return getJsonResponse(ResponseHelper.searchResponse(request.getQueryParameter("q"), + request.getQueryParameter("filter")), "public, max-age=600"); } catch (Exception e) { return getErrorResponse(e); } @@ -140,7 +140,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { try { return getJsonResponse( ResponseHelper.searchPageResponse(request.getQueryParameter("q"), - request.getQueryParameter("url"), request.getQueryParameter("id")), + request.getQueryParameter("filter"), request.getQueryParameter("nextpage")), "public, max-age=3600"); } catch (Exception e) { return getErrorResponse(e); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index d4c80ca..d8bae0b 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -7,6 +7,7 @@ import java.net.URL; import java.net.http.HttpRequest; import java.net.http.HttpRequest.BodyPublishers; import java.net.http.HttpResponse.BodyHandlers; +import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -285,10 +286,11 @@ public class ResponseHelper { } - public static final byte[] searchResponse(String q) throws IOException, ExtractionException, InterruptedException { + public static final byte[] searchResponse(String q, String filter) + throws IOException, ExtractionException, InterruptedException { final SearchInfo info = SearchInfo.getInfo(Constants.YOUTUBE_SERVICE, - Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q)); + Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q, Collections.singletonList(filter), null)); ObjectArrayList items = new ObjectArrayList<>(); @@ -298,7 +300,7 @@ public class ResponseHelper { StreamInfoItem stream = (StreamInfoItem) item; items.add(new SearchStream(item.getName(), rewriteURL(item.getThumbnailUrl()), item.getUrl().substring(23), stream.getTextualUploadDate(), stream.getUploaderName(), - stream.getUploaderUrl().substring(23), stream.getViewCount(), stream.getDuration(), + optionalSubstring(stream.getUploaderUrl(), 23), stream.getViewCount(), stream.getDuration(), stream.isUploaderVerified())); break; case CHANNEL: @@ -319,17 +321,19 @@ public class ResponseHelper { Page nextpage = info.getNextPage(); - return nextpage != null - ? Constants.mapper.writeValueAsBytes(new SearchResults(nextpage.getUrl(), nextpage.getId(), items)) - : Constants.mapper.writeValueAsBytes(new SearchResults(null, null, items)); + return Constants.mapper + .writeValueAsBytes(new SearchResults(items, Constants.mapper.writeValueAsString(nextpage))); } - public static final byte[] searchPageResponse(String q, String url, String id) + public static final byte[] searchPageResponse(String q, String filter, String prevpageStr) throws IOException, ExtractionException, InterruptedException { + Page prevpage = Constants.mapper.readValue(prevpageStr, Page.class); + InfoItemsPage pages = SearchInfo.getMoreItems(Constants.YOUTUBE_SERVICE, - Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q), new Page(url, id)); + Constants.YOUTUBE_SERVICE.getSearchQHFactory().fromQuery(q, Collections.singletonList(filter), null), + prevpage); ObjectArrayList items = new ObjectArrayList<>(); @@ -360,9 +364,8 @@ public class ResponseHelper { Page nextpage = pages.getNextPage(); - return nextpage != null - ? Constants.mapper.writeValueAsBytes(new SearchResults(nextpage.getUrl(), nextpage.getId(), items)) - : Constants.mapper.writeValueAsBytes(new SearchResults(null, null, items)); + return Constants.mapper + .writeValueAsBytes(new SearchResults(items, Constants.mapper.writeValueAsString(nextpage))); } @@ -441,6 +444,10 @@ public class ResponseHelper { } + private static final String optionalSubstring(String s, int index) { + return s == null || s.isEmpty() ? null : s.substring(index); + } + private static String rewriteURL(final String old) { if (Constants.debug) diff --git a/src/main/java/me/kavin/piped/utils/obj/SearchResults.java b/src/main/java/me/kavin/piped/utils/obj/SearchResults.java index c698082..f346f5d 100644 --- a/src/main/java/me/kavin/piped/utils/obj/SearchResults.java +++ b/src/main/java/me/kavin/piped/utils/obj/SearchResults.java @@ -5,12 +5,11 @@ import me.kavin.piped.utils.obj.search.SearchItem; public class SearchResults { - public String nextpage, id; public ObjectArrayList items; + public String nextpage; - public SearchResults(String nextpage, String id, ObjectArrayList items) { + public SearchResults(ObjectArrayList items, String nextpage) { this.nextpage = nextpage; - this.id = id; this.items = items; } }