|
1 | 1 | package org.schabi.newpipe.extractor.services.youtube.extractors; |
2 | 2 |
|
3 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER; |
4 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl; |
5 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; |
6 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint; |
7 | | -import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody; |
8 | | -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS; |
9 | | -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ARTISTS; |
10 | | -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS; |
11 | | -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_SONGS; |
12 | | -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS; |
13 | | -import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING; |
14 | | -import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; |
15 | | -import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; |
16 | | - |
17 | 3 | import com.grack.nanojson.JsonArray; |
18 | 4 | import com.grack.nanojson.JsonObject; |
19 | 5 | import com.grack.nanojson.JsonParser; |
20 | 6 | import com.grack.nanojson.JsonParserException; |
21 | 7 | import com.grack.nanojson.JsonWriter; |
22 | | - |
23 | 8 | import org.schabi.newpipe.extractor.InfoItem; |
24 | 9 | import org.schabi.newpipe.extractor.MetaInfo; |
| 10 | +import org.schabi.newpipe.extractor.MultiInfoItemsCollector; |
25 | 11 | import org.schabi.newpipe.extractor.Page; |
26 | 12 | import org.schabi.newpipe.extractor.StreamingService; |
27 | 13 | import org.schabi.newpipe.extractor.downloader.Downloader; |
|
31 | 17 | import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler; |
32 | 18 | import org.schabi.newpipe.extractor.localization.DateWrapper; |
33 | 19 | import org.schabi.newpipe.extractor.localization.TimeAgoParser; |
34 | | -import org.schabi.newpipe.extractor.MultiInfoItemsCollector; |
35 | 20 | import org.schabi.newpipe.extractor.search.SearchExtractor; |
36 | 21 | import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; |
37 | 22 | import org.schabi.newpipe.extractor.utils.JsonUtils; |
38 | 23 | import org.schabi.newpipe.extractor.utils.Parser; |
39 | 24 | import org.schabi.newpipe.extractor.utils.Utils; |
40 | 25 |
|
| 26 | +import javax.annotation.Nonnull; |
| 27 | +import javax.annotation.Nullable; |
41 | 28 | import java.io.IOException; |
42 | 29 | import java.util.Collections; |
43 | 30 | import java.util.HashMap; |
44 | 31 | import java.util.List; |
45 | 32 | import java.util.Map; |
| 33 | +import java.util.stream.Collectors; |
46 | 34 |
|
47 | | -import javax.annotation.Nonnull; |
48 | | -import javax.annotation.Nullable; |
| 35 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER; |
| 36 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl; |
| 37 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; |
| 38 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint; |
| 39 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody; |
| 40 | +import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS; |
| 41 | +import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ARTISTS; |
| 42 | +import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS; |
| 43 | +import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_SONGS; |
| 44 | +import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS; |
| 45 | +import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING; |
| 46 | +import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; |
| 47 | +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; |
49 | 48 |
|
50 | 49 | public class YoutubeMusicSearchExtractor extends SearchExtractor { |
51 | 50 | private JsonObject initialData; |
@@ -133,55 +132,52 @@ public void onFetchPage(@Nonnull final Downloader downloader) |
133 | 132 | } |
134 | 133 | } |
135 | 134 |
|
136 | | - @Nonnull |
137 | | - @Override |
138 | | - public String getUrl() throws ParsingException { |
139 | | - return super.getUrl(); |
| 135 | + private List<JsonObject> getItemSectionRendererContents() { |
| 136 | + return initialData |
| 137 | + .getObject("contents") |
| 138 | + .getObject("tabbedSearchResultsRenderer") |
| 139 | + .getArray("tabs") |
| 140 | + .getObject(0) |
| 141 | + .getObject("tabRenderer") |
| 142 | + .getObject("content") |
| 143 | + .getObject("sectionListRenderer") |
| 144 | + .getArray("contents") |
| 145 | + .stream() |
| 146 | + .filter(JsonObject.class::isInstance) |
| 147 | + .map(JsonObject.class::cast) |
| 148 | + .map(c -> c.getObject("itemSectionRenderer")) |
| 149 | + .filter(isr -> !isr.isEmpty()) |
| 150 | + .map(isr -> isr |
| 151 | + .getArray("contents") |
| 152 | + .getObject(0)) |
| 153 | + .collect(Collectors.toList()); |
140 | 154 | } |
141 | 155 |
|
142 | 156 | @Nonnull |
143 | 157 | @Override |
144 | 158 | public String getSearchSuggestion() throws ParsingException { |
145 | | - final JsonObject itemSectionRenderer = JsonUtils.getArray(JsonUtils.getArray(initialData, |
146 | | - "contents.tabbedSearchResultsRenderer.tabs").getObject(0), |
147 | | - "tabRenderer.content.sectionListRenderer.contents") |
148 | | - .getObject(0) |
149 | | - .getObject("itemSectionRenderer"); |
150 | | - if (itemSectionRenderer.isEmpty()) { |
151 | | - return ""; |
| 159 | + for (final JsonObject obj : getItemSectionRendererContents()) { |
| 160 | + final JsonObject didYouMeanRenderer = obj |
| 161 | + .getObject("didYouMeanRenderer"); |
| 162 | + final JsonObject showingResultsForRenderer = obj |
| 163 | + .getObject("showingResultsForRenderer"); |
| 164 | + |
| 165 | + if (!didYouMeanRenderer.isEmpty()) { |
| 166 | + return getTextFromObject(didYouMeanRenderer.getObject("correctedQuery")); |
| 167 | + } else if (!showingResultsForRenderer.isEmpty()) { |
| 168 | + return JsonUtils.getString(showingResultsForRenderer, |
| 169 | + "correctedQueryEndpoint.searchEndpoint.query"); |
| 170 | + } |
152 | 171 | } |
153 | 172 |
|
154 | | - final JsonObject didYouMeanRenderer = itemSectionRenderer.getArray("contents") |
155 | | - .getObject(0).getObject("didYouMeanRenderer"); |
156 | | - final JsonObject showingResultsForRenderer = itemSectionRenderer.getArray("contents") |
157 | | - .getObject(0) |
158 | | - .getObject("showingResultsForRenderer"); |
159 | | - |
160 | | - if (!didYouMeanRenderer.isEmpty()) { |
161 | | - return getTextFromObject(didYouMeanRenderer.getObject("correctedQuery")); |
162 | | - } else if (!showingResultsForRenderer.isEmpty()) { |
163 | | - return JsonUtils.getString(showingResultsForRenderer, |
164 | | - "correctedQueryEndpoint.searchEndpoint.query"); |
165 | | - } else { |
166 | | - return ""; |
167 | | - } |
| 173 | + return ""; |
168 | 174 | } |
169 | 175 |
|
170 | 176 | @Override |
171 | 177 | public boolean isCorrectedSearch() throws ParsingException { |
172 | | - final JsonObject itemSectionRenderer = JsonUtils.getArray(JsonUtils.getArray(initialData, |
173 | | - "contents.tabbedSearchResultsRenderer.tabs").getObject(0), |
174 | | - "tabRenderer.content.sectionListRenderer.contents") |
175 | | - .getObject(0) |
176 | | - .getObject("itemSectionRenderer"); |
177 | | - if (itemSectionRenderer.isEmpty()) { |
178 | | - return false; |
179 | | - } |
180 | | - |
181 | | - final JsonObject firstContent = itemSectionRenderer.getArray("contents").getObject(0); |
182 | | - |
183 | | - return firstContent.has("didYouMeanRenderer") |
184 | | - || firstContent.has("showingResultsForRenderer"); |
| 178 | + return getItemSectionRendererContents() |
| 179 | + .stream() |
| 180 | + .anyMatch(obj -> obj.has("showingResultsForRenderer")); |
185 | 181 | } |
186 | 182 |
|
187 | 183 | @Nonnull |
|
0 commit comments