2020-04-10 10:51:05 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.soundcloud.extractors;
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2022-08-15 05:49:40 +02:00
|
|
|
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
|
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
|
|
|
|
2020-03-17 15:12:13 +01:00
|
|
|
import com.grack.nanojson.JsonArray;
|
2017-08-16 04:40:03 +02:00
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2020-03-17 15:12:13 +01:00
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2020-04-15 14:09:46 +02:00
|
|
|
import org.schabi.newpipe.extractor.Page;
|
2017-08-06 22:20:15 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2020-02-08 23:58:46 +01:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
2017-08-04 16:21:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
2017-08-16 04:40:03 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2017-08-04 16:21:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
|
2020-04-10 10:51:05 +02:00
|
|
|
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-04 16:21:45 +02:00
|
|
|
|
2020-03-17 20:31:01 +01:00
|
|
|
import java.io.IOException;
|
2020-04-15 14:09:46 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2020-03-17 20:31:01 +01:00
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2020-03-17 15:12:13 +01:00
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
|
2020-04-15 14:09:46 +02:00
|
|
|
private static final int STREAMS_PER_REQUESTED_PAGE = 15;
|
2020-03-17 15:12:13 +01:00
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
private String playlistId;
|
2017-08-16 04:40:03 +02:00
|
|
|
private JsonObject playlist;
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2021-05-15 17:51:43 +02:00
|
|
|
public SoundcloudPlaylistExtractor(final StreamingService service,
|
|
|
|
final ListLinkHandler linkHandler) {
|
2019-04-28 22:03:16 +02:00
|
|
|
super(service, linkHandler);
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
@Override
|
2021-05-15 17:51:43 +02:00
|
|
|
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException,
|
|
|
|
ExtractionException {
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2018-09-15 20:12:52 +02:00
|
|
|
playlistId = getLinkHandler().getId();
|
2021-05-15 19:42:29 +02:00
|
|
|
final String apiUrl = SOUNDCLOUD_API_V2_URL + "playlists/" + playlistId + "?client_id="
|
|
|
|
+ SoundcloudParsingHelper.clientId() + "&representation=compact";
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2021-05-15 17:51:43 +02:00
|
|
|
final String response = downloader.get(apiUrl, getExtractorLocalization()).responseBody();
|
2017-08-16 04:40:03 +02:00
|
|
|
try {
|
|
|
|
playlist = JsonParser.object().from(response);
|
2021-05-15 17:51:43 +02:00
|
|
|
} catch (final JsonParserException e) {
|
2017-08-16 04:40:03 +02:00
|
|
|
throw new ParsingException("Could not parse json response", e);
|
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2017-08-04 16:21:45 +02:00
|
|
|
@Override
|
2017-08-11 03:23:09 +02:00
|
|
|
public String getId() {
|
2017-08-04 16:21:45 +02:00
|
|
|
return playlistId;
|
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2017-08-04 16:21:45 +02:00
|
|
|
@Override
|
2017-08-11 03:23:09 +02:00
|
|
|
public String getName() {
|
2017-08-16 04:40:03 +02:00
|
|
|
return playlist.getString("title");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-12 13:16:00 +01:00
|
|
|
@Nonnull
|
2017-08-04 16:21:45 +02:00
|
|
|
@Override
|
2017-08-08 23:36:11 +02:00
|
|
|
public String getThumbnailUrl() {
|
2018-03-04 21:30:31 +01:00
|
|
|
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 {
|
2018-03-11 21:54:41 +01:00
|
|
|
final InfoItemsPage<StreamInfoItem> infoItems = getInitialPage();
|
2018-03-04 21:30:31 +01:00
|
|
|
|
2021-05-15 17:51:43 +02:00
|
|
|
for (final StreamInfoItem item : infoItems.getItems()) {
|
2020-03-17 15:13:28 +01:00
|
|
|
artworkUrl = item.getThumbnailUrl();
|
2022-03-12 13:16:00 +01:00
|
|
|
if (!isNullOrEmpty(artworkUrl)) {
|
|
|
|
break;
|
|
|
|
}
|
2018-03-04 21:30:31 +01:00
|
|
|
}
|
2021-05-15 17:51:43 +02:00
|
|
|
} catch (final Exception ignored) {
|
2018-03-04 21:30:31 +01:00
|
|
|
}
|
2020-03-17 15:13:28 +01:00
|
|
|
|
|
|
|
if (artworkUrl == null) {
|
2022-08-15 05:49:40 +02:00
|
|
|
return "";
|
2020-03-17 15:13:28 +01:00
|
|
|
}
|
2018-03-04 21:30:31 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:13:28 +01:00
|
|
|
return artworkUrl.replace("large.jpg", "crop.jpg");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() {
|
2018-02-21 09:23:57 +01:00
|
|
|
return SoundcloudParsingHelper.getUploaderUrl(playlist);
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderName() {
|
2018-02-21 09:23:57 +01:00
|
|
|
return SoundcloudParsingHelper.getUploaderName(playlist);
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderAvatarUrl() {
|
2018-02-21 09:23:57 +01:00
|
|
|
return SoundcloudParsingHelper.getAvatarUrl(playlist);
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 01:44:58 +01:00
|
|
|
@Override
|
|
|
|
public boolean isUploaderVerified() throws ParsingException {
|
|
|
|
return playlist.getObject("user").getBoolean("verified");
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
@Override
|
2017-08-06 22:20:15 +02:00
|
|
|
public long getStreamCount() {
|
2020-06-13 20:25:38 +02:00
|
|
|
return playlist.getLong("track_count");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 17:51:43 +02:00
|
|
|
@Nonnull
|
|
|
|
@Override
|
2020-04-15 14:09:46 +02:00
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() {
|
2021-05-15 17:51:43 +02:00
|
|
|
final StreamInfoItemsCollector streamInfoItemsCollector =
|
|
|
|
new StreamInfoItemsCollector(getServiceId());
|
2020-04-15 14:09:46 +02:00
|
|
|
final List<String> ids = new ArrayList<>();
|
2020-03-17 15:12:13 +01:00
|
|
|
|
2022-03-16 20:14:08 +01:00
|
|
|
playlist.getArray("tracks")
|
|
|
|
.stream()
|
|
|
|
.filter(JsonObject.class::isInstance)
|
2022-03-12 13:16:00 +01:00
|
|
|
.map(JsonObject.class::cast)
|
|
|
|
.forEachOrdered(track -> {
|
2022-03-16 20:14:08 +01:00
|
|
|
// i.e. if full info is available
|
|
|
|
if (track.has("title")) {
|
2022-03-12 13:16:00 +01:00
|
|
|
streamInfoItemsCollector.commit(
|
|
|
|
new SoundcloudStreamInfoItemExtractor(track));
|
|
|
|
} else {
|
|
|
|
// %09d would be enough, but a 0 before the number does not create
|
|
|
|
// problems, so let's be sure
|
|
|
|
ids.add(String.format("%010d", track.getInt("id")));
|
|
|
|
}
|
|
|
|
});
|
2020-03-17 15:12:13 +01:00
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
return new InfoItemsPage<>(streamInfoItemsCollector, new Page(ids));
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-05-15 17:51:43 +02:00
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException,
|
|
|
|
ExtractionException {
|
2020-05-11 15:25:18 +02:00
|
|
|
if (page == null || isNullOrEmpty(page.getIds())) {
|
|
|
|
throw new IllegalArgumentException("Page doesn't contain IDs");
|
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
final List<String> currentIds;
|
|
|
|
final List<String> nextIds;
|
|
|
|
if (page.getIds().size() <= STREAMS_PER_REQUESTED_PAGE) {
|
|
|
|
// Fetch every remaining stream, there are less than the max
|
|
|
|
currentIds = page.getIds();
|
|
|
|
nextIds = null;
|
2020-03-17 18:04:40 +01:00
|
|
|
} else {
|
2020-04-15 14:09:46 +02:00
|
|
|
currentIds = page.getIds().subList(0, STREAMS_PER_REQUESTED_PAGE);
|
|
|
|
nextIds = page.getIds().subList(STREAMS_PER_REQUESTED_PAGE, page.getIds().size());
|
2020-03-17 18:04:40 +01:00
|
|
|
}
|
|
|
|
|
2021-05-15 19:42:29 +02:00
|
|
|
final String currentPageUrl = SOUNDCLOUD_API_V2_URL + "tracks?client_id="
|
2022-07-28 04:08:28 +02:00
|
|
|
+ SoundcloudParsingHelper.clientId() + "&ids=" + String.join(",", currentIds);
|
2020-04-15 14:09:46 +02:00
|
|
|
|
|
|
|
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2021-05-15 17:51:43 +02:00
|
|
|
final String response = NewPipe.getDownloader().get(currentPageUrl,
|
|
|
|
getExtractorLocalization()).responseBody();
|
2020-03-17 15:12:13 +01:00
|
|
|
|
|
|
|
try {
|
2020-04-15 14:09:46 +02:00
|
|
|
final JsonArray tracks = JsonParser.array().from(response);
|
2021-05-15 17:51:43 +02:00
|
|
|
for (final Object track : tracks) {
|
2020-03-17 15:12:13 +01:00
|
|
|
if (track instanceof JsonObject) {
|
|
|
|
collector.commit(new SoundcloudStreamInfoItemExtractor((JsonObject) track));
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 17:51:43 +02:00
|
|
|
} catch (final JsonParserException e) {
|
2020-03-17 15:12:13 +01:00
|
|
|
throw new ParsingException("Could not parse json response", e);
|
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
return new InfoItemsPage<>(collector, new Page(nextIds));
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
}
|