Skip to content

Commit 24a8df7

Browse files
committed
Change search suggestions to match what is seen at youtube.com
1 parent e9156ee commit 24a8df7

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSuggestionExtractor.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,23 @@ public List<String> suggestionList(String query, String contentCountry) throws I
4848
List<String> suggestions = new ArrayList<>();
4949

5050
String url = "https://suggestqueries.google.com/complete/search"
51-
+ "?client=" + "firefox" // 'toolbar' for xml
51+
+ "?client=" + "youtube" //"firefox" for JSON, 'toolbar' for xml
52+
+ "&jsonp=" + "JP"
5253
+ "&ds=" + "yt"
5354
+ "&hl=" + URLEncoder.encode(contentCountry, CHARSET_UTF_8)
5455
+ "&q=" + URLEncoder.encode(query, CHARSET_UTF_8);
5556

5657
String response = dl.download(url);
58+
// trim JSONP part "JP(...)"
59+
response = response.substring(3, response.length()-1);
5760
try {
58-
JsonArray collection = JsonParser.array().from(response).getArray(1);
59-
for (Object suggestion : collection) suggestions.add(suggestion.toString());
61+
JsonArray collection = JsonParser.array().from(response).getArray(1, new JsonArray());
62+
for (Object suggestion : collection) {
63+
if (!(suggestion instanceof JsonArray)) continue;
64+
String suggestionStr = ((JsonArray)suggestion).getString(0);
65+
if (suggestionStr == null) continue;
66+
suggestions.add(suggestionStr);
67+
}
6068

6169
return suggestions;
6270
} catch (JsonParserException e) {

0 commit comments

Comments
 (0)