private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(JsonArray searchCollection) { final InfoItemsSearchCollector collector = getInfoItemSearchCollector(); for (Object result : searchCollection) { if (!(result instanceof JsonObject)) continue; //noinspection ConstantConditions JsonObject searchResult = (JsonObject) result; String kind = searchResult.getString("kind", ""); switch (kind) { case "user": collector.commit(new SoundcloudChannelInfoItemExtractor(searchResult)); break; case "track": collector.commit(new SoundcloudStreamInfoItemExtractor(searchResult)); break; case "playlist": collector.commit(new SoundcloudPlaylistInfoItemExtractor(searchResult)); break; } } return collector; }
/** * Fetch the streams from the given api and commit each of them to the collector. * * @return the next streams url, empty if don't have */ public static String getStreamsFromApi(StreamInfoItemsCollector collector, String apiUrl, boolean charts) throws IOException, ReCaptchaException, ParsingException { String response = NewPipe.getDownloader().download(apiUrl); JsonObject responseObject; try { responseObject = JsonParser.object().from(response); } catch (JsonParserException e) { throw new ParsingException("Could not parse json response", e); } JsonArray responseCollection = responseObject.getArray("collection"); for (Object o : responseCollection) { if (o instanceof JsonObject) { JsonObject object = (JsonObject) o; collector.commit(new SoundcloudStreamInfoItemExtractor(charts ? object.getObject("track") : object)); } } String nextPageUrl; try { nextPageUrl = responseObject.getString("next_href"); if (!nextPageUrl.contains("client_id=")) nextPageUrl += "&client_id=" + SoundcloudParsingHelper.clientId(); } catch (Exception ignored) { nextPageUrl = ""; } return nextPageUrl; }