2018-05-08 21:19:03 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2017-08-16 04:40:03 +02: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.NewPipe;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2020-02-08 23:58:46 +01:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
2017-03-01 18:47:52 +01:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URLEncoder;
|
2021-04-07 12:25:59 +02:00
|
|
|
import java.util.*;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2021-04-07 12:25:59 +02:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.addCookieHeader;
|
2021-02-07 22:12:22 +01:00
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
|
|
|
|
|
2017-06-29 20:12:55 +02:00
|
|
|
/*
|
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 {
|
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
public YoutubeSuggestionExtractor(StreamingService service) {
|
|
|
|
super(service);
|
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 {
|
2021-04-07 12:25:59 +02:00
|
|
|
final Downloader dl = NewPipe.getDownloader();
|
|
|
|
final List<String> suggestions = new ArrayList<>();
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2021-04-07 12:25:59 +02:00
|
|
|
final String url = "https://suggestqueries.google.com/complete/search"
|
2017-09-15 23:49:54 +02:00
|
|
|
+ "?client=" + "youtube" //"firefox" for JSON, 'toolbar' for xml
|
|
|
|
+ "&jsonp=" + "JP"
|
2017-03-01 18:47:52 +01:00
|
|
|
+ "&ds=" + "yt"
|
2021-02-07 22:12:22 +01:00
|
|
|
+ "&gl=" + URLEncoder.encode(getExtractorContentCountry().getCountryCode(), UTF_8)
|
|
|
|
+ "&q=" + URLEncoder.encode(query, UTF_8);
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2021-04-07 12:25:59 +02:00
|
|
|
final Map<String, List<String>> headers = new HashMap<>();
|
|
|
|
addCookieHeader(headers);
|
|
|
|
|
|
|
|
String response = dl.get(url, headers, getExtractorLocalization()).responseBody();
|
2017-09-15 23:49:54 +02:00
|
|
|
// trim JSONP part "JP(...)"
|
2020-02-08 23:58:46 +01:00
|
|
|
response = response.substring(3, response.length() - 1);
|
2017-03-01 18:47:52 +01:00
|
|
|
try {
|
2020-04-16 16:08:14 +02:00
|
|
|
JsonArray collection = JsonParser.array().from(response).getArray(1);
|
2017-09-15 23:49:54 +02:00
|
|
|
for (Object suggestion : collection) {
|
|
|
|
if (!(suggestion instanceof JsonArray)) continue;
|
2020-02-08 23:58:46 +01:00
|
|
|
String suggestionStr = ((JsonArray) suggestion).getString(0);
|
2017-09-15 23:49:54 +02:00
|
|
|
if (suggestionStr == null) continue;
|
|
|
|
suggestions.add(suggestionStr);
|
|
|
|
}
|
2017-08-06 22:20:15 +02:00
|
|
|
|
2017-08-16 04:40:03 +02:00
|
|
|
return suggestions;
|
|
|
|
} catch (JsonParserException e) {
|
|
|
|
throw new ParsingException("Could not parse json response", e);
|
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|