125 lines
4.8 KiB
Java
125 lines
4.8 KiB
Java
package org.schabi.newpipe.extractor.services.soundcloud.extractors;
|
|
|
|
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.InfoItem;
|
|
import org.schabi.newpipe.extractor.InfoItemExtractor;
|
|
import org.schabi.newpipe.extractor.InfoItemsCollector;
|
|
import org.schabi.newpipe.extractor.Page;
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
|
|
import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector;
|
|
import org.schabi.newpipe.extractor.search.SearchExtractor;
|
|
import org.schabi.newpipe.extractor.utils.Parser;
|
|
|
|
import java.io.IOException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.ITEMS_PER_PAGE;
|
|
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
|
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
|
|
|
public class SoundcloudSearchExtractor extends SearchExtractor {
|
|
private JsonArray searchCollection;
|
|
|
|
public SoundcloudSearchExtractor(StreamingService service, SearchQueryHandler linkHandler) {
|
|
super(service, linkHandler);
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public String getSearchSuggestion() {
|
|
return "";
|
|
}
|
|
|
|
@Override
|
|
public boolean isCorrectedSearch() {
|
|
return false;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
|
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(getUrl()));
|
|
}
|
|
|
|
@Override
|
|
public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, ExtractionException {
|
|
if (page == null || isNullOrEmpty(page.getUrl())) {
|
|
throw new IllegalArgumentException("Page doesn't contain an URL");
|
|
}
|
|
|
|
final Downloader dl = getDownloader();
|
|
try {
|
|
final String response = dl.get(page.getUrl(), getExtractorLocalization()).responseBody();
|
|
searchCollection = JsonParser.object().from(response).getArray("collection");
|
|
} catch (JsonParserException e) {
|
|
throw new ParsingException("Could not parse json response", e);
|
|
}
|
|
|
|
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(page.getUrl()));
|
|
}
|
|
|
|
@Override
|
|
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
|
|
final Downloader dl = getDownloader();
|
|
final String url = getUrl();
|
|
try {
|
|
final String response = dl.get(url, getExtractorLocalization()).responseBody();
|
|
searchCollection = JsonParser.object().from(response).getArray("collection");
|
|
} catch (JsonParserException e) {
|
|
throw new ParsingException("Could not parse json response", e);
|
|
}
|
|
|
|
if (searchCollection.size() == 0) {
|
|
throw new SearchExtractor.NothingFoundException("Nothing found");
|
|
}
|
|
}
|
|
|
|
private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(JsonArray searchCollection) {
|
|
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
|
|
|
|
for (Object result : searchCollection) {
|
|
if (!(result instanceof JsonObject)) continue;
|
|
//noinspection ConstantConditions
|
|
JsonObject searchResult = (JsonObject) result;
|
|
String kind = searchResult.getString("kind", EMPTY_STRING);
|
|
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;
|
|
}
|
|
|
|
private Page getNextPageFromCurrentUrl(String currentUrl)
|
|
throws MalformedURLException, UnsupportedEncodingException {
|
|
final int pageOffset = Integer.parseInt(
|
|
Parser.compatParseMap(
|
|
new URL(currentUrl)
|
|
.getQuery())
|
|
.get("offset"));
|
|
|
|
return new Page(currentUrl.replace("&offset=" + pageOffset,
|
|
"&offset=" + (pageOffset + ITEMS_PER_PAGE)));
|
|
}
|
|
}
|