Merge pull request #31 from dosvald/yt-search-suggestions

Change search suggestions to match what is seen at youtube.com
This commit is contained in:
Mauricio Colli 2017-09-15 19:27:09 -03:00 committed by GitHub
commit 4a91e29dcf
1 changed files with 11 additions and 3 deletions

View File

@ -48,15 +48,23 @@ public class YoutubeSuggestionExtractor extends SuggestionExtractor {
List<String> suggestions = new ArrayList<>(); List<String> suggestions = new ArrayList<>();
String url = "https://suggestqueries.google.com/complete/search" 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" + "&ds=" + "yt"
+ "&hl=" + URLEncoder.encode(contentCountry, CHARSET_UTF_8) + "&hl=" + URLEncoder.encode(contentCountry, CHARSET_UTF_8)
+ "&q=" + URLEncoder.encode(query, CHARSET_UTF_8); + "&q=" + URLEncoder.encode(query, CHARSET_UTF_8);
String response = dl.download(url); String response = dl.download(url);
// trim JSONP part "JP(...)"
response = response.substring(3, response.length()-1);
try { try {
JsonArray collection = JsonParser.array().from(response).getArray(1); JsonArray collection = JsonParser.array().from(response).getArray(1, new JsonArray());
for (Object suggestion : collection) suggestions.add(suggestion.toString()); for (Object suggestion : collection) {
if (!(suggestion instanceof JsonArray)) continue;
String suggestionStr = ((JsonArray)suggestion).getString(0);
if (suggestionStr == null) continue;
suggestions.add(suggestionStr);
}
return suggestions; return suggestions;
} catch (JsonParserException e) { } catch (JsonParserException e) {