2017-08-20 10:03:41 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.soundcloud;
|
|
|
|
|
2017-11-28 13:37:01 +01:00
|
|
|
import org.schabi.newpipe.extractor.Downloader;
|
2018-07-01 16:21:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.uih.ListUIHandler;
|
2017-08-20 10:03:41 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
|
2018-03-01 01:02:43 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
2018-02-24 22:20:50 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
|
2017-08-20 10:03:41 +02:00
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2018-03-01 01:02:43 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2017-11-25 01:10:04 +01:00
|
|
|
|
2017-08-20 10:03:41 +02:00
|
|
|
public class SoundcloudChartsExtractor extends KioskExtractor {
|
2018-02-26 15:55:27 +01:00
|
|
|
private StreamInfoItemsCollector collector = null;
|
|
|
|
private String nextPageUrl = null;
|
|
|
|
|
2018-07-01 16:21:40 +02:00
|
|
|
public SoundcloudChartsExtractor(StreamingService service, ListUIHandler urlIdHandler, String kioskId) {
|
2018-05-06 14:08:50 +02:00
|
|
|
super(service, urlIdHandler, kioskId);
|
2017-08-20 10:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-11-28 13:37:01 +01:00
|
|
|
public void onFetchPage(@Nonnull Downloader downloader) {
|
2017-08-20 10:03:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2017-08-20 10:03:41 +02:00
|
|
|
@Override
|
2018-02-24 22:20:50 +01:00
|
|
|
public String getName() {
|
2018-04-01 20:27:44 +02:00
|
|
|
return getId();
|
2017-08-20 10:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
|
2018-03-01 01:02:43 +01:00
|
|
|
if (pageUrl == null || pageUrl.isEmpty()) {
|
|
|
|
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
|
2017-08-20 10:03:41 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 22:20:50 +01:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2018-02-26 15:55:27 +01:00
|
|
|
String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, pageUrl, true);
|
2017-08-20 10:03:41 +02:00
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, nextPageUrl);
|
2017-08-20 10:03:41 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 15:55:27 +01:00
|
|
|
|
|
|
|
private void computNextPageAndStreams() throws IOException, ExtractionException {
|
|
|
|
collector = new StreamInfoItemsCollector(getServiceId());
|
2017-08-20 10:03:41 +02:00
|
|
|
|
|
|
|
String apiUrl = "https://api-v2.soundcloud.com/charts" +
|
|
|
|
"?genre=soundcloud:genres:all-music" +
|
|
|
|
"&client_id=" + SoundcloudParsingHelper.clientId();
|
|
|
|
|
2017-09-25 12:43:40 +02:00
|
|
|
if (getId().equals("Top 50")) {
|
2017-08-20 10:03:41 +02:00
|
|
|
apiUrl += "&kind=top";
|
|
|
|
} else {
|
2017-12-24 23:15:40 +01:00
|
|
|
apiUrl += "&kind=trending";
|
2017-08-20 10:03:41 +02:00
|
|
|
}
|
|
|
|
|
2017-08-22 14:47:51 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:20:50 +01:00
|
|
|
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl, true);
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNextPageUrl() throws IOException, ExtractionException {
|
|
|
|
if(nextPageUrl == null) {
|
|
|
|
computNextPageAndStreams();
|
|
|
|
}
|
|
|
|
return nextPageUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
|
2018-02-26 15:55:27 +01:00
|
|
|
if(collector == null) {
|
|
|
|
computNextPageAndStreams();
|
|
|
|
}
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrl());
|
2017-08-20 10:03:41 +02:00
|
|
|
}
|
|
|
|
}
|