Extract YouTube search suggestions

This commit is contained in:
wb9688 2020-03-20 16:11:28 +01:00
parent c852b13d5a
commit dc29d87962
2 changed files with 38 additions and 17 deletions

View File

@ -174,16 +174,19 @@ public class YoutubeSearchExtractor extends SearchExtractor {
@Override @Override
public String getSearchSuggestion() throws ParsingException { public String getSearchSuggestion() throws ParsingException {
if (isMusicSearch()) return ""; if (isMusicSearch()) {
final JsonObject itemSectionRenderer = initialData.getObject("contents").getObject("sectionListRenderer")
.getArray("contents").getObject(0).getObject("itemSectionRenderer");
if (itemSectionRenderer == null) return "";
return getTextFromObject(itemSectionRenderer.getArray("contents").getObject(0)
.getObject("didYouMeanRenderer").getObject("correctedQuery"));
} else {
JsonObject showingResultsForRenderer = initialData.getObject("contents") JsonObject showingResultsForRenderer = initialData.getObject("contents")
.getObject("twoColumnSearchResultsRenderer").getObject("primaryContents") .getObject("twoColumnSearchResultsRenderer").getObject("primaryContents")
.getObject("sectionListRenderer").getArray("contents").getObject(0) .getObject("sectionListRenderer").getArray("contents").getObject(0)
.getObject("itemSectionRenderer").getArray("contents").getObject(0) .getObject("itemSectionRenderer").getArray("contents").getObject(0)
.getObject("showingResultsForRenderer"); .getObject("showingResultsForRenderer");
if (showingResultsForRenderer == null) { if (showingResultsForRenderer == null) return "";
return "";
} else {
return getTextFromObject(showingResultsForRenderer.getObject("correctedQuery")); return getTextFromObject(showingResultsForRenderer.getObject("correctedQuery"));
} }
} }
@ -194,10 +197,13 @@ public class YoutubeSearchExtractor extends SearchExtractor {
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId()); final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
if (isMusicSearch()) { if (isMusicSearch()) {
JsonArray sections = initialData.getObject("contents").getObject("sectionListRenderer") final JsonArray contents = initialData.getObject("contents").getObject("sectionListRenderer").getArray("contents");
.getArray("contents").getObject(0).getObject("musicShelfRenderer").getArray("contents");
collectMusicStreamsFrom(collector, sections); for (Object content : contents) {
if (((JsonObject) content).getObject("musicShelfRenderer") != null) {
collectMusicStreamsFrom(collector, ((JsonObject) content).getObject("musicShelfRenderer").getArray("contents"));
}
}
} else { } else {
JsonArray sections = initialData.getObject("contents").getObject("twoColumnSearchResultsRenderer") JsonArray sections = initialData.getObject("contents").getObject("twoColumnSearchResultsRenderer")
.getObject("primaryContents").getObject("sectionListRenderer").getArray("contents"); .getObject("primaryContents").getObject("sectionListRenderer").getArray("contents");
@ -213,8 +219,15 @@ public class YoutubeSearchExtractor extends SearchExtractor {
@Override @Override
public String getNextPageUrl() throws ExtractionException, IOException { public String getNextPageUrl() throws ExtractionException, IOException {
if (isMusicSearch()) { if (isMusicSearch()) {
return getNextPageUrlFrom(initialData.getObject("contents").getObject("sectionListRenderer") final JsonArray contents = initialData.getObject("contents").getObject("sectionListRenderer").getArray("contents");
.getArray("contents").getObject(0).getObject("musicShelfRenderer").getArray("continuations"));
for (Object content : contents) {
if (((JsonObject) content).getObject("musicShelfRenderer") != null) {
return getNextPageUrlFrom(((JsonObject) content).getObject("musicShelfRenderer").getArray("continuations"));
}
}
return "";
} else { } else {
return getNextPageUrlFrom(initialData.getObject("contents").getObject("twoColumnSearchResultsRenderer") return getNextPageUrlFrom(initialData.getObject("contents").getObject("twoColumnSearchResultsRenderer")
.getObject("primaryContents").getObject("sectionListRenderer").getArray("contents") .getObject("primaryContents").getObject("sectionListRenderer").getArray("contents")
@ -517,9 +530,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
} }
private String getNextPageUrlFrom(JsonArray continuations) throws ParsingException, IOException, ReCaptchaException { private String getNextPageUrlFrom(JsonArray continuations) throws ParsingException, IOException, ReCaptchaException {
if (continuations == null) { if (continuations == null) return "";
return "";
}
JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData"); JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData");
String continuation = nextContinuationData.getString("continuation"); String continuation = nextContinuationData.getString("continuation");

View File

@ -76,4 +76,14 @@ public class YoutubeSearchExtractorMusicTest extends YoutubeSearchExtractorBaseT
assertEquals("json", queryPairs.get("alt")); assertEquals("json", queryPairs.get("alt"));
assertTrue(queryPairs.get("key").length() > 5); assertTrue(queryPairs.get("key").length() > 5);
} }
@Test
public void testSuggestions() throws Exception {
YoutubeSearchExtractor newExtractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("megaman x3",
asList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), null);
newExtractor.fetchPage();
assertTrue(newExtractor.getInitialPage().getItems().size() > 10);
assertEquals("mega man x3", newExtractor.getSearchSuggestion());
}
} }