From 24a8df7c4987b571e88364ac6778b1f66aefbcf7 Mon Sep 17 00:00:00 2001 From: dosvald Date: Fri, 15 Sep 2017 23:49:54 +0200 Subject: [PATCH] Change search suggestions to match what is seen at youtube.com --- .../youtube/YoutubeSuggestionExtractor.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractor.java b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractor.java index 79b61dc6b..cd98f25b7 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractor.java @@ -48,15 +48,23 @@ public class YoutubeSuggestionExtractor extends SuggestionExtractor { List suggestions = new ArrayList<>(); String url = "https://suggestqueries.google.com/complete/search" - + "?client=" + "firefox" // 'toolbar' for xml + + "?client=" + "youtube" //"firefox" for JSON, 'toolbar' for xml + + "&jsonp=" + "JP" + "&ds=" + "yt" + "&hl=" + URLEncoder.encode(contentCountry, CHARSET_UTF_8) + "&q=" + URLEncoder.encode(query, CHARSET_UTF_8); String response = dl.download(url); + // trim JSONP part "JP(...)" + response = response.substring(3, response.length()-1); try { - JsonArray collection = JsonParser.array().from(response).getArray(1); - for (Object suggestion : collection) suggestions.add(suggestion.toString()); + JsonArray collection = JsonParser.array().from(response).getArray(1, new JsonArray()); + for (Object suggestion : collection) { + if (!(suggestion instanceof JsonArray)) continue; + String suggestionStr = ((JsonArray)suggestion).getString(0); + if (suggestionStr == null) continue; + suggestions.add(suggestionStr); + } return suggestions; } catch (JsonParserException e) {