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

87 lines
3.5 KiB
Java
Raw Normal View History

package org.schabi.newpipe.extractor.services.soundcloud.extractors;
2017-08-20 10:03:41 +02:00
2020-04-15 14:09:46 +02:00
import org.schabi.newpipe.extractor.Page;
2017-08-20 10:03:41 +02:00
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
2017-08-20 10:03:41 +02:00
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
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;
import java.io.IOException;
2020-03-29 11:13:39 +02:00
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2020-03-29 11:13:39 +02:00
2019-01-28 12:18:16 +01:00
public class SoundcloudChartsExtractor extends KioskExtractor<StreamInfoItem> {
public SoundcloudChartsExtractor(final StreamingService service,
final ListLinkHandler linkHandler,
final String kioskId) {
super(service, linkHandler, kioskId);
2017-08-20 10:03:41 +02:00
}
@Override
public void onFetchPage(@Nonnull final 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(final Page page) throws IOException,
ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL");
}
2020-06-13 20:25:38 +02:00
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector,
page.getUrl(), true);
2018-02-26 15:55:27 +01:00
2020-04-15 14:09:46 +02:00
return new InfoItemsPage<>(collector, new Page(nextPageUrl));
}
@Nonnull
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
2020-06-13 20:25:38 +02:00
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2017-08-20 10:03:41 +02:00
String apiUrl = SOUNDCLOUD_API_V2_URL + "charts" + "?genre=soundcloud:genres:all-music"
+ "&client_id=" + SoundcloudParsingHelper.clientId();
2017-08-20 10:03:41 +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
}
final ContentCountry contentCountry = SoundCloud.getContentCountry();
String apiUrlWithRegion = null;
if (getService().getSupportedCountries().contains(contentCountry)) {
apiUrlWithRegion = apiUrl + "&region=soundcloud:regions:"
+ contentCountry.getCountryCode();
}
String nextPageUrl;
try {
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector,
apiUrlWithRegion == null ? apiUrl : apiUrlWithRegion, true);
} catch (final IOException e) {
// Request to other region may be geo-restricted.
// See https://github.com/TeamNewPipe/NewPipeExtractor/issues/537.
// We retry without the specified region.
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl, true);
}
2018-02-26 15:55:27 +01:00
2020-04-15 14:09:46 +02:00
return new InfoItemsPage<>(collector, new Page(nextPageUrl));
2017-08-20 10:03:41 +02:00
}
}