86 lines
2.9 KiB
Java
86 lines
2.9 KiB
Java
package org.schabi.newpipe.extractor.services.soundcloud;
|
|
|
|
import org.schabi.newpipe.extractor.Downloader;
|
|
import org.schabi.newpipe.extractor.uih.ListUIHandler;
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import java.io.IOException;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class SoundcloudChartsExtractor extends KioskExtractor {
|
|
private StreamInfoItemsCollector collector = null;
|
|
private String nextPageUrl = null;
|
|
|
|
public SoundcloudChartsExtractor(StreamingService service, ListUIHandler urlIdHandler, String kioskId) {
|
|
super(service, urlIdHandler, kioskId);
|
|
}
|
|
|
|
@Override
|
|
public void onFetchPage(@Nonnull Downloader downloader) {
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public String getName() {
|
|
return getId();
|
|
}
|
|
|
|
@Override
|
|
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
|
|
if (pageUrl == null || pageUrl.isEmpty()) {
|
|
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
|
|
}
|
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
|
String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, pageUrl, true);
|
|
|
|
return new InfoItemsPage<>(collector, nextPageUrl);
|
|
}
|
|
|
|
|
|
private void computNextPageAndStreams() throws IOException, ExtractionException {
|
|
collector = new StreamInfoItemsCollector(getServiceId());
|
|
|
|
String apiUrl = "https://api-v2.soundcloud.com/charts" +
|
|
"?genre=soundcloud:genres:all-music" +
|
|
"&client_id=" + SoundcloudParsingHelper.clientId();
|
|
|
|
if (getId().equals("Top 50")) {
|
|
apiUrl += "&kind=top";
|
|
} else {
|
|
apiUrl += "&kind=trending";
|
|
}
|
|
|
|
List<String> supportedCountries = Arrays.asList("AU", "CA", "FR", "DE", "IE", "NL", "NZ", "GB", "US");
|
|
String contentCountry = getContentCountry();
|
|
if (supportedCountries.contains(contentCountry)) {
|
|
apiUrl += "®ion=soundcloud:regions:" + contentCountry;
|
|
}
|
|
|
|
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl, true);
|
|
}
|
|
|
|
@Override
|
|
public String getNextPageUrl() throws IOException, ExtractionException {
|
|
if(nextPageUrl == null) {
|
|
computNextPageAndStreams();
|
|
}
|
|
return nextPageUrl;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
|
|
if(collector == null) {
|
|
computNextPageAndStreams();
|
|
}
|
|
return new InfoItemsPage<>(collector, getNextPageUrl());
|
|
}
|
|
}
|