117 lines
3.8 KiB
Java
117 lines
3.8 KiB
Java
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.NewPipe;
|
|
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;
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import java.io.IOException;
|
|
|
|
@SuppressWarnings("WeakerAccess")
|
|
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
|
|
private String playlistId;
|
|
private JsonObject playlist;
|
|
|
|
public SoundcloudPlaylistExtractor(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException {
|
|
super(service, url, nextStreamsUrl);
|
|
}
|
|
|
|
@Override
|
|
public void fetchPage() throws IOException, ExtractionException {
|
|
Downloader dl = NewPipe.getDownloader();
|
|
|
|
playlistId = getUrlIdHandler().getId(getOriginalUrl());
|
|
String apiUrl = "https://api.soundcloud.com/playlists/" + playlistId +
|
|
"?client_id=" + SoundcloudParsingHelper.clientId() +
|
|
"&representation=compact";
|
|
|
|
String response = dl.download(apiUrl);
|
|
try {
|
|
playlist = JsonParser.object().from(response);
|
|
} catch (JsonParserException e) {
|
|
throw new ParsingException("Could not parse json response", e);
|
|
}
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public String getCleanUrl() {
|
|
return playlist.isString("permalink_url") ? playlist.getString("permalink_url") : getOriginalUrl();
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public String getId() {
|
|
return playlistId;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public String getName() {
|
|
return playlist.getString("title");
|
|
}
|
|
|
|
@Override
|
|
public String getThumbnailUrl() {
|
|
return playlist.getString("artwork_url");
|
|
}
|
|
|
|
@Override
|
|
public String getBannerUrl() {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public String getUploaderUrl() {
|
|
return playlist.getObject("user").getString("permalink_url", "");
|
|
}
|
|
|
|
@Override
|
|
public String getUploaderName() {
|
|
return playlist.getObject("user").getString("username", "");
|
|
}
|
|
|
|
@Override
|
|
public String getUploaderAvatarUrl() {
|
|
return playlist.getObject("user", new JsonObject()).getString("avatar_url", "");
|
|
}
|
|
|
|
@Override
|
|
public long getStreamCount() {
|
|
return playlist.getNumber("track_count", 0).longValue();
|
|
}
|
|
|
|
@Override
|
|
public StreamInfoItemCollector getStreams() throws IOException, ExtractionException {
|
|
StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId());
|
|
|
|
// Note the "api", NOT "api-v2"
|
|
String apiUrl = "https://api.soundcloud.com/playlists/" + getId() + "/tracks"
|
|
+ "?client_id=" + SoundcloudParsingHelper.clientId()
|
|
+ "&limit=20"
|
|
+ "&linked_partitioning=1";
|
|
|
|
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, apiUrl);
|
|
return collector;
|
|
}
|
|
|
|
@Override
|
|
public NextItemsResult getNextStreams() throws IOException, ExtractionException {
|
|
if (!hasMoreStreams()) {
|
|
throw new ExtractionException("Playlist doesn't have more streams");
|
|
}
|
|
|
|
StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId());
|
|
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, nextStreamsUrl);
|
|
|
|
return new NextItemsResult(collector, nextStreamsUrl);
|
|
}
|
|
}
|