package org.schabi.newpipe.extractor.services.soundcloud; import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParserException; import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; public class SoundcloudSuggestionExtractor extends SuggestionExtractor { public static final String CHARSET_UTF_8 = "UTF-8"; public SoundcloudSuggestionExtractor(StreamingService service) { super(service); } @Override public List suggestionList(String query) throws IOException, ExtractionException { List suggestions = new ArrayList<>(); Downloader dl = NewPipe.getDownloader(); String url = "https://api-v2.soundcloud.com/search/queries" + "?q=" + URLEncoder.encode(query, CHARSET_UTF_8) + "&client_id=" + SoundcloudParsingHelper.clientId() + "&limit=10"; String response = dl.get(url, getExtractorLocalization()).responseBody(); try { JsonArray collection = JsonParser.object().from(response).getArray("collection"); for (Object suggestion : collection) { if (suggestion instanceof JsonObject) suggestions.add(((JsonObject) suggestion).getString("query")); } return suggestions; } catch (JsonParserException e) { throw new ParsingException("Could not parse json response", e); } } }