2017-08-04 16:21:45 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.soundcloud;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.schabi.newpipe.extractor.Downloader;
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2017-08-06 22:20:15 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2017-08-04 16:21:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
@SuppressWarnings("WeakerAccess")
|
|
|
|
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
|
|
|
|
private String playlistId;
|
|
|
|
private JSONObject playlist;
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
public SoundcloudPlaylistExtractor(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException {
|
|
|
|
super(service, url, nextStreamsUrl);
|
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
@Override
|
|
|
|
public void fetchPage() throws IOException, ExtractionException {
|
2017-08-04 16:21:45 +02:00
|
|
|
Downloader dl = NewPipe.getDownloader();
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
playlistId = getUrlIdHandler().getId(getOriginalUrl());
|
|
|
|
String apiUrl = "https://api.soundcloud.com/playlists/" + playlistId +
|
|
|
|
"?client_id=" + SoundcloudParsingHelper.clientId() +
|
|
|
|
"&representation=compact";
|
2017-08-04 16:21:45 +02:00
|
|
|
|
|
|
|
String response = dl.download(apiUrl);
|
|
|
|
playlist = new JSONObject(response);
|
|
|
|
}
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
@Override
|
|
|
|
public String getCleanUrl() {
|
|
|
|
try {
|
|
|
|
return playlist.getString("permalink_url");
|
|
|
|
} catch (Exception e) {
|
|
|
|
return getOriginalUrl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
@Override
|
|
|
|
public String getPlaylistId() {
|
|
|
|
return playlistId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getPlaylistName() {
|
2017-08-10 19:50:59 +02:00
|
|
|
return playlist.optString("title");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-08 23:36:11 +02:00
|
|
|
public String getThumbnailUrl() {
|
2017-08-08 17:01:50 +02:00
|
|
|
return playlist.optString("artwork_url");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getBannerUrl() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() {
|
|
|
|
return playlist.getJSONObject("user").getString("permalink_url");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderName() {
|
|
|
|
return playlist.getJSONObject("user").getString("username");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderAvatarUrl() {
|
|
|
|
return playlist.getJSONObject("user").getString("avatar_url");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-06 22:20:15 +02:00
|
|
|
public long getStreamCount() {
|
2017-08-04 16:21:45 +02:00
|
|
|
return playlist.getLong("track_count");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-06 22:20:15 +02:00
|
|
|
public StreamInfoItemCollector getStreams() throws IOException, ExtractionException {
|
|
|
|
StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId());
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
// Note the "api", NOT "api-v2"
|
|
|
|
String apiUrl = "https://api.soundcloud.com/playlists/" + getPlaylistId() + "/tracks"
|
|
|
|
+ "?client_id=" + SoundcloudParsingHelper.clientId()
|
|
|
|
+ "&limit=20"
|
|
|
|
+ "&linked_partitioning=1";
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, apiUrl);
|
2017-08-04 16:21:45 +02:00
|
|
|
return collector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-06 22:20:15 +02:00
|
|
|
public NextItemsResult getNextStreams() throws IOException, ExtractionException {
|
|
|
|
if (!hasMoreStreams()) {
|
|
|
|
throw new ExtractionException("Playlist doesn't have more streams");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId());
|
|
|
|
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, nextStreamsUrl);
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
return new NextItemsResult(collector.getItemList(), nextStreamsUrl);
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
}
|