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

116 lines
3.7 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;
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;
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
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;
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-06 22:20:15 +02:00
@Override
2017-11-28 13:37:01 +01:00
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
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-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
2017-08-06 22:20:15 +02:00
@Override
public String getCleanUrl() {
return playlist.isString("permalink_url") ? playlist.getString("permalink_url") : getOriginalUrl();
2017-08-06 22:20:15 +02:00
}
@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() {
return playlist.getString("artwork_url");
}
@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
2017-08-06 22:20:15 +02:00
public StreamInfoItemCollector getStreams() throws IOException, ExtractionException {
StreamInfoItemCollector collector = new StreamInfoItemCollector(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";
2017-08-06 22:20:15 +02:00
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, apiUrl);
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-06 22:20:15 +02:00
StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId());
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, nextStreamsUrl);
return new NextItemsResult(collector, nextStreamsUrl);
}
}