From 488e41d24ecdbca56dcce81116354bc854c03406 Mon Sep 17 00:00:00 2001 From: Thompson3142 <115718208+Thompson3142@users.noreply.github.com> Date: Sun, 8 Sep 2024 19:53:38 +0200 Subject: [PATCH] Cleanup + bug fix with ALL search --- .../extractors/YoutubeSearchExtractor.java | 14 +++++------ .../YoutubeSearchQueryHandlerFactory.java | 24 +++++++------------ 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java index 04a108f85..41607b8ac 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java @@ -64,7 +64,8 @@ import javax.annotation.Nullable; public class YoutubeSearchExtractor extends SearchExtractor { @Nullable - private String searchType; + private final String searchType; + public final boolean isExactSearch; private final boolean extractVideoResults; private final boolean extractChannelResults; private final boolean extractPlaylistResults; @@ -76,11 +77,11 @@ public class YoutubeSearchExtractor extends SearchExtractor { super(service, linkHandler); final List contentFilters = linkHandler.getContentFilters(); searchType = isNullOrEmpty(contentFilters) ? null : contentFilters.get(0); + // Save whether we should extract video, channel and playlist results depending on the // requested search type, as YouTube returns sometimes videos inside channel search results // If no search type is provided or ALL/EXACT (without another search type) filter // is requested, extract everything - extractVideoResults = searchType == null || ALL.equals(searchType) || VIDEOS.equals(searchType) || EXACT.equals(searchType); extractChannelResults = searchType == null || ALL.equals(searchType) @@ -88,11 +89,8 @@ public class YoutubeSearchExtractor extends SearchExtractor { extractPlaylistResults = searchType == null || ALL.equals(searchType) || PLAYLISTS.equals(searchType) || EXACT.equals(searchType); - // if EXACT is a content filter, it replaces the current search type - // this needs to happen AFTER the extract... params are set - if (!isNullOrEmpty(contentFilters) && contentFilters.contains(EXACT)) { - searchType = EXACT; - } + // If EXACT is NOT the search type this is needed to filter for a content filter + EXACT + isExactSearch = !isNullOrEmpty(contentFilters) && contentFilters.contains(EXACT); } @Override @@ -100,7 +98,7 @@ public class YoutubeSearchExtractor extends SearchExtractor { ExtractionException { final String query = super.getSearchString(); final Localization localization = getExtractorLocalization(); - final String params = getSearchParameter(searchType); + final String params = getSearchParameter(searchType, isExactSearch); final JsonBuilder jsonBody = prepareDesktopJsonBuilder(localization, getExtractorContentCountry()) .value("query", query); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeSearchQueryHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeSearchQueryHandlerFactory.java index ff938c765..94ac3267c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeSearchQueryHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeSearchQueryHandlerFactory.java @@ -39,24 +39,18 @@ public final class YoutubeSearchQueryHandlerFactory extends SearchQueryHandlerFa @Nonnull final List contentFilters, final String sortFilter) throws ParsingException, UnsupportedOperationException { - final String contentFilter; - - // EXACT takes precedence over other filters - if (contentFilters.contains(EXACT)) { - contentFilter = EXACT; - } else { - contentFilter = !contentFilters.isEmpty() ? contentFilters.get(0) : ""; - } + final String contentFilter = !contentFilters.isEmpty() ? contentFilters.get(0) : ""; + final boolean isExactSearch = !contentFilters.isEmpty() && contentFilter.contains(EXACT); switch (contentFilter) { case EXACT: return SEARCH_URL + encodeUrlUtf8(searchString) + "&sp=QgIIAQ%253D%253D"; case VIDEOS: - return SEARCH_URL + encodeUrlUtf8(searchString) + "&sp=EgIQAfABAQ%253D%253D"; + return SEARCH_URL + encodeUrlUtf8(searchString) + (isExactSearch ? "&sp=EgIQAUICCAE%253D" : "&sp=EgIQAfABAQ%253D%253D"); case CHANNELS: - return SEARCH_URL + encodeUrlUtf8(searchString) + "&sp=EgIQAvABAQ%253D%253D"; + return SEARCH_URL + encodeUrlUtf8(searchString) + (isExactSearch ? "&sp=EgIQAkICCAE%253D" : "&sp=EgIQAvABAQ%253D%253D"); case PLAYLISTS: - return SEARCH_URL + encodeUrlUtf8(searchString) + "&sp=EgIQA_ABAQ%253D%253D"; + return SEARCH_URL + encodeUrlUtf8(searchString) + (isExactSearch ? "&sp=EgIQA0ICCAE%253D" : "&sp=EgIQA_ABAQ%253D%253D"); case MUSIC_SONGS: case MUSIC_VIDEOS: case MUSIC_ALBUMS: @@ -86,7 +80,7 @@ public final class YoutubeSearchQueryHandlerFactory extends SearchQueryHandlerFa } @Nonnull - public static String getSearchParameter(final String contentFilter) { + public static String getSearchParameter(final String contentFilter, final boolean isExactSearch) { if (isNullOrEmpty(contentFilter)) { return "8AEB"; } @@ -95,11 +89,11 @@ public final class YoutubeSearchQueryHandlerFactory extends SearchQueryHandlerFa case EXACT: return "QgIIAQ%3D%3D"; case VIDEOS: - return "EgIQAfABAQ%3D%3D"; + return isExactSearch ? "EgIQAUICCAE%3D" : "EgIQAfABAQ%3D%3D"; case CHANNELS: - return "EgIQAvABAQ%3D%3D"; + return isExactSearch ? "EgIQAkICCAE%3D" : "EgIQAvABAQ%3D%3D"; case PLAYLISTS: - return "EgIQA_ABAQ%3D%3D"; + return isExactSearch ? "EgIQA0ICCAE%3D" : "EgIQA_ABAQ%3D%3D"; case MUSIC_SONGS: case MUSIC_VIDEOS: case MUSIC_ALBUMS: