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

76 lines
3.0 KiB
Java
Raw Normal View History

2018-05-08 21:19:03 +02:00
package org.schabi.newpipe.extractor.services.youtube.extractors;
2017-03-01 18:47:52 +01:00
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
2017-03-01 18:47:52 +01:00
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.SuggestionExtractor;
2017-03-01 18:47:52 +01:00
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
2018-09-15 21:47:53 +02:00
import org.schabi.newpipe.extractor.utils.Localization;
2017-03-01 18:47:52 +01:00
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/*
2017-03-01 18:47:52 +01:00
* Created by Christian Schabesberger on 28.09.16.
*
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
* YoutubeSuggestionExtractor.java is part of NewPipe.
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
public class YoutubeSuggestionExtractor extends SuggestionExtractor {
public static final String CHARSET_UTF_8 = "UTF-8";
2018-09-15 21:47:53 +02:00
public YoutubeSuggestionExtractor(int serviceId, Localization localization) {
super(serviceId, localization);
2017-03-01 18:47:52 +01:00
}
@Override
2018-09-15 21:47:53 +02:00
public List<String> suggestionList(String query) throws IOException, ExtractionException {
2017-03-01 18:47:52 +01:00
Downloader dl = NewPipe.getDownloader();
2017-08-06 22:20:15 +02:00
List<String> suggestions = new ArrayList<>();
2017-03-01 18:47:52 +01:00
String url = "https://suggestqueries.google.com/complete/search"
+ "?client=" + "youtube" //"firefox" for JSON, 'toolbar' for xml
+ "&jsonp=" + "JP"
2017-03-01 18:47:52 +01:00
+ "&ds=" + "yt"
2018-09-15 21:47:53 +02:00
+ "&hl=" + URLEncoder.encode(getLocalization().getCountry(), CHARSET_UTF_8)
2017-03-01 18:47:52 +01:00
+ "&q=" + URLEncoder.encode(query, CHARSET_UTF_8);
String response = dl.download(url);
// trim JSONP part "JP(...)"
response = response.substring(3, response.length()-1);
2017-03-01 18:47:52 +01:00
try {
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);
}
2017-08-06 22:20:15 +02:00
return suggestions;
} catch (JsonParserException e) {
throw new ParsingException("Could not parse json response", e);
}
2017-03-01 18:47:52 +01:00
}
}