NewPipeExtractor/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractor.java

86 lines
2.9 KiB
Java
Raw Normal View History

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
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-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) {
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
}
@Nonnull
2017-08-20 10:03:41 +02:00
@Override
2018-02-24 22:20:50 +01:00
public String getName() {
return getId();
2017-08-20 10:03:41 +02:00
}
@Override
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
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();
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
}
List<String> supportedCountries = Arrays.asList("AU", "CA", "FR", "DE", "IE", "NL", "NZ", "GB", "US");
String contentCountry = getContentCountry();
if (supportedCountries.contains(contentCountry)) {
apiUrl += "&region=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
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
2018-02-26 15:55:27 +01:00
if(collector == null) {
computNextPageAndStreams();
}
return new InfoItemsPage<>(collector, getNextPageUrl());
2017-08-20 10:03:41 +02:00
}
}