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>
This commit is contained in:
bopol 2021-07-04 19:53:33 +02:00 committed by GitHub
parent 1af5754f18
commit ac3c43b1ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

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

View file

@ -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<StreamItem> 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;