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

151 lines
5.2 KiB
Java
Raw Normal View History

package org.schabi.newpipe.extractor.services.soundcloud;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
2017-08-06 22:20:15 +02:00
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
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;
2018-09-15 21:47:53 +02:00
import org.schabi.newpipe.extractor.utils.Localization;
import javax.annotation.Nonnull;
2017-08-06 22:20:15 +02:00
import java.io.IOException;
@SuppressWarnings("WeakerAccess")
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
private String playlistId;
private JsonObject playlist;
2018-02-26 15:55:27 +01:00
private StreamInfoItemsCollector streamInfoItemsCollector = null;
private String nextPageUrl = null;
2018-09-15 21:47:53 +02:00
public SoundcloudPlaylistExtractor(StreamingService service, ListLinkHandler linkHandler, Localization localization) {
super(service, linkHandler, localization);
2017-08-06 22:20:15 +02:00
}
2017-08-06 22:20:15 +02:00
@Override
2017-11-28 13:37:01 +01:00
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
playlistId = getLinkHandler().getId();
2017-08-06 22:20:15 +02:00
String apiUrl = "https://api.soundcloud.com/playlists/" + playlistId +
"?client_id=" + SoundcloudParsingHelper.clientId() +
"&representation=compact";
2017-11-28 13:37:01 +01:00
String response = downloader.download(apiUrl);
try {
playlist = JsonParser.object().from(response);
} catch (JsonParserException e) {
throw new ParsingException("Could not parse json response", e);
}
}
@Nonnull
@Override
2017-08-11 03:23:09 +02:00
public String getId() {
return playlistId;
}
@Nonnull
@Override
2017-08-11 03:23:09 +02:00
public String getName() {
return playlist.getString("title");
}
@Override
2017-08-08 23:36:11 +02:00
public String getThumbnailUrl() {
String artworkUrl = playlist.getString("artwork_url");
if (artworkUrl == null) {
// If the thumbnail is null, traverse the items list and get a valid one,
// if it also fails, return null
try {
final InfoItemsPage<StreamInfoItem> infoItems = getInitialPage();
if (infoItems.getItems().isEmpty()) return null;
for (StreamInfoItem item : infoItems.getItems()) {
final String thumbnailUrl = item.getThumbnailUrl();
if (thumbnailUrl == null || thumbnailUrl.isEmpty()) continue;
String thumbnailUrlBetterResolution = thumbnailUrl.replace("large.jpg", "crop.jpg");
return thumbnailUrlBetterResolution;
}
} catch (Exception ignored) {
}
}
String artworkUrlBetterResolution = artworkUrl.replace("large.jpg", "crop.jpg");
return artworkUrlBetterResolution;
}
@Override
public String getBannerUrl() {
return null;
}
@Override
public String getUploaderUrl() {
return SoundcloudParsingHelper.getUploaderUrl(playlist);
}
@Override
public String getUploaderName() {
return SoundcloudParsingHelper.getUploaderName(playlist);
}
@Override
public String getUploaderAvatarUrl() {
return SoundcloudParsingHelper.getAvatarUrl(playlist);
}
@Override
2017-08-06 22:20:15 +02:00
public long getStreamCount() {
return playlist.getNumber("track_count", 0).longValue();
}
2017-11-25 02:03:30 +01:00
@Nonnull
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
if (streamInfoItemsCollector == null) {
2018-02-26 15:55:27 +01:00
computeStreamsAndNextPageUrl();
}
return new InfoItemsPage<>(streamInfoItemsCollector, getNextPageUrl());
2018-02-26 15:55:27 +01:00
}
private void computeStreamsAndNextPageUrl() throws ExtractionException, IOException {
streamInfoItemsCollector = new StreamInfoItemsCollector(getServiceId());
2017-08-06 22:20:15 +02:00
// Note the "api", NOT "api-v2"
2017-08-11 03:23:09 +02:00
String apiUrl = "https://api.soundcloud.com/playlists/" + getId() + "/tracks"
2017-08-06 22:20:15 +02:00
+ "?client_id=" + SoundcloudParsingHelper.clientId()
+ "&limit=20"
+ "&linked_partitioning=1";
2018-02-26 15:55:27 +01:00
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, streamInfoItemsCollector, apiUrl);
}
@Override
public String getNextPageUrl() throws IOException, ExtractionException {
if (nextPageUrl == null) {
2018-02-26 15:55:27 +01:00
computeStreamsAndNextPageUrl();
}
return nextPageUrl;
}
@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"));
}
2018-02-24 22:20:50 +01:00
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2018-02-26 15:55:27 +01:00
String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, pageUrl);
return new InfoItemsPage<>(collector, nextPageUrl);
}
}