66import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getKey ;
77import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getTextFromObject ;
88import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .prepareDesktopJsonBuilder ;
9+ import static org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubeSearchQueryHandlerFactory .ALL ;
10+ import static org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubeSearchQueryHandlerFactory .CHANNELS ;
11+ import static org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubeSearchQueryHandlerFactory .PLAYLISTS ;
12+ import static org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubeSearchQueryHandlerFactory .VIDEOS ;
913import static org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubeSearchQueryHandlerFactory .getSearchParameter ;
1014import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
1115
5761 */
5862
5963public class YoutubeSearchExtractor extends SearchExtractor {
64+
65+ @ Nullable
66+ private final String searchType ;
67+ private final boolean extractVideoResults ;
68+ private final boolean extractChannelResults ;
69+ private final boolean extractPlaylistResults ;
70+
6071 private JsonObject initialData ;
6172
6273 public YoutubeSearchExtractor (final StreamingService service ,
6374 final SearchQueryHandler linkHandler ) {
6475 super (service , linkHandler );
76+ final List <String > contentFilters = linkHandler .getContentFilters ();
77+ searchType = isNullOrEmpty (contentFilters ) ? null : contentFilters .get (0 );
78+ // Save whether we should extract video, channel and playlist results depending on the
79+ // requested search type, as YouTube returns sometimes videos inside channel search results
80+ // If no search type is provided or ALL filter is requested, extract everything
81+ extractVideoResults = searchType == null || ALL .equals (searchType )
82+ || VIDEOS .equals (searchType );
83+ extractChannelResults = searchType == null || ALL .equals (searchType )
84+ || CHANNELS .equals (searchType );
85+ extractPlaylistResults = searchType == null || ALL .equals (searchType )
86+ || PLAYLISTS .equals (searchType );
6587 }
6688
6789 @ Override
6890 public void onFetchPage (@ Nonnull final Downloader downloader ) throws IOException ,
6991 ExtractionException {
7092 final String query = super .getSearchString ();
7193 final Localization localization = getExtractorLocalization ();
72-
73- // Get the search parameter of the request
74- final List <String > contentFilters = super .getLinkHandler ().getContentFilters ();
75- final String params ;
76- if (!isNullOrEmpty (contentFilters )) {
77- final String searchType = contentFilters .get (0 );
78- params = getSearchParameter (searchType );
79- } else {
80- params = "" ;
81- }
94+ final String params = getSearchParameter (searchType );
8295
8396 final JsonBuilder <JsonObject > jsonBody = prepareDesktopJsonBuilder (localization ,
8497 getExtractorContentCountry ())
@@ -211,7 +224,7 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException,
211224
212225 private void collectStreamsFrom (final MultiInfoItemsCollector collector ,
213226 @ Nonnull final JsonArray contents )
214- throws NothingFoundException , ParsingException {
227+ throws NothingFoundException {
215228 final TimeAgoParser timeAgoParser = getTimeAgoParser ();
216229
217230 for (final Object content : contents ) {
@@ -220,13 +233,13 @@ private void collectStreamsFrom(final MultiInfoItemsCollector collector,
220233 throw new NothingFoundException (
221234 getTextFromObject (item .getObject ("backgroundPromoRenderer" )
222235 .getObject ("bodyText" )));
223- } else if (item .has ("videoRenderer" )) {
236+ } else if (extractVideoResults && item .has ("videoRenderer" )) {
224237 collector .commit (new YoutubeStreamInfoItemExtractor (
225238 item .getObject ("videoRenderer" ), timeAgoParser ));
226- } else if (item .has ("channelRenderer" )) {
239+ } else if (extractChannelResults && item .has ("channelRenderer" )) {
227240 collector .commit (new YoutubeChannelInfoItemExtractor (
228241 item .getObject ("channelRenderer" )));
229- } else if (item .has ("playlistRenderer" )) {
242+ } else if (extractPlaylistResults && item .has ("playlistRenderer" )) {
230243 collector .commit (new YoutubePlaylistInfoItemExtractor (
231244 item .getObject ("playlistRenderer" )));
232245 }
0 commit comments