From ac3c43b1ed6f169ae2a319ea2b6aaf640abacfa5 Mon Sep 17 00:00:00 2001 From: bopol Date: Sun, 4 Jul 2021 19:53:33 +0200 Subject: [PATCH] Support region localization for trending (#79) * Support region localization for trending * Revert linting, remove defaulting to US, and fix warning. Co-authored-by: FireMasterK <20838718+FireMasterK@users.noreply.github.com> --- src/main/java/me/kavin/piped/ServerLauncher.java | 3 ++- .../java/me/kavin/piped/utils/ResponseHelper.java | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 5614128..1ebfbc4 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -147,7 +147,8 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { } })).map("/trending", AsyncServlet.ofBlocking(executor, request -> { try { - return getJsonResponse(ResponseHelper.trendingResponse(), "public, max-age=3600"); + return getJsonResponse(ResponseHelper.trendingResponse(request.getQueryParameter("region")), + "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 77c19bd..55e6c80 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -23,7 +23,10 @@ import org.schabi.newpipe.extractor.comments.CommentsInfo; import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.kiosk.KioskExtractor; import org.schabi.newpipe.extractor.kiosk.KioskInfo; +import org.schabi.newpipe.extractor.kiosk.KioskList; +import org.schabi.newpipe.extractor.localization.ContentCountry; import org.schabi.newpipe.extractor.playlist.PlaylistInfo; import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; import org.schabi.newpipe.extractor.search.SearchInfo; @@ -201,13 +204,16 @@ public class ResponseHelper { } - public static final byte[] trendingResponse() throws ParsingException, ExtractionException, IOException { + public static final byte[] trendingResponse(String region) + throws ParsingException, ExtractionException, IOException { final List relatedStreams = new ObjectArrayList<>(); - String url = Constants.YOUTUBE_SERVICE.getKioskList().getListLinkHandlerFactoryByType("Trending") - .getUrl("Trending"); - KioskInfo info = KioskInfo.getInfo(Constants.YOUTUBE_SERVICE, url); + KioskList kioskList = Constants.YOUTUBE_SERVICE.getKioskList(); + kioskList.forceContentCountry(new ContentCountry(region)); + KioskExtractor extractor = kioskList.getDefaultKioskExtractor(); + extractor.fetchPage(); + KioskInfo info = KioskInfo.getInfo(extractor); info.getRelatedItems().forEach(o -> { StreamInfoItem item = o;