2017-08-04 16:21:45 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.soundcloud;
|
|
|
|
|
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;
|
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;
|
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
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2020-03-17 15:12:13 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
import java.io.IOException;
|
2020-03-17 15:12:13 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2017-08-06 22:20:15 +02:00
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
@SuppressWarnings("WeakerAccess")
|
|
|
|
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
|
2020-03-17 15:12:13 +01:00
|
|
|
private static final int streamsPerRequestedPage = 15;
|
|
|
|
|
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
|
|
|
|
2020-03-17 15:12:13 +01:00
|
|
|
private StreamInfoItemsCollector streamInfoItemsCollector;
|
|
|
|
private String nextPageUrl;
|
2018-02-26 15:55:27 +01:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
public SoundcloudPlaylistExtractor(StreamingService service, ListLinkHandler linkHandler) {
|
|
|
|
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
|
2017-11-28 13:37:01 +01:00
|
|
|
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2018-09-15 20:12:52 +02:00
|
|
|
playlistId = getLinkHandler().getId();
|
2020-03-17 15:12:13 +01:00
|
|
|
String apiUrl = "https://api-v2.soundcloud.com/playlists/" + playlistId +
|
2017-08-06 22:20:15 +02:00
|
|
|
"?client_id=" + SoundcloudParsingHelper.clientId() +
|
|
|
|
"&representation=compact";
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
String response = downloader.get(apiUrl, getExtractorLocalization()).responseBody();
|
2017-08-16 04:40:03 +02:00
|
|
|
try {
|
|
|
|
playlist = JsonParser.object().from(response);
|
|
|
|
} catch (JsonParserException e) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-17 15:13:28 +01:00
|
|
|
@Nullable
|
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
|
|
|
|
2018-03-11 21:50:40 +01:00
|
|
|
for (StreamInfoItem item : infoItems.getItems()) {
|
2020-03-17 15:13:28 +01:00
|
|
|
artworkUrl = item.getThumbnailUrl();
|
|
|
|
if (artworkUrl != null && !artworkUrl.isEmpty()) break;
|
2018-03-04 21:30:31 +01:00
|
|
|
}
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2020-03-17 15:13:28 +01:00
|
|
|
|
|
|
|
if (artworkUrl == null) {
|
|
|
|
return null;
|
|
|
|
}
|
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 getBannerUrl() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-06 22:20:15 +02:00
|
|
|
public long getStreamCount() {
|
2017-08-16 04:40:03 +02:00
|
|
|
return playlist.getNumber("track_count", 0).longValue();
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 02:03:30 +01:00
|
|
|
@Nonnull
|
2017-08-04 16:21:45 +02:00
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
|
|
|
|
if (streamInfoItemsCollector == null) {
|
2020-03-17 18:04:40 +01:00
|
|
|
computeInitialTracksAndNextPageUrl();
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
2020-03-17 18:04:40 +01:00
|
|
|
return new InfoItemsPage<>(streamInfoItemsCollector, nextPageUrl);
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:04:40 +01:00
|
|
|
private void computeInitialTracksAndNextPageUrl() throws IOException, ExtractionException {
|
2018-02-26 15:55:27 +01:00
|
|
|
streamInfoItemsCollector = new StreamInfoItemsCollector(getServiceId());
|
2020-03-17 18:04:40 +01:00
|
|
|
StringBuilder nextPageUrlBuilder = new StringBuilder("https://api-v2.soundcloud.com/tracks?client_id=");
|
|
|
|
nextPageUrlBuilder.append(SoundcloudParsingHelper.clientId());
|
|
|
|
nextPageUrlBuilder.append("&ids=");
|
2020-03-17 15:12:13 +01:00
|
|
|
|
|
|
|
JsonArray tracks = playlist.getArray("tracks");
|
|
|
|
for (Object o : tracks) {
|
|
|
|
if (o instanceof JsonObject) {
|
|
|
|
JsonObject track = (JsonObject) o;
|
|
|
|
if (track.has("title")) { // i.e. if full info is available
|
|
|
|
streamInfoItemsCollector.commit(new SoundcloudStreamInfoItemExtractor(track));
|
|
|
|
} else {
|
2020-03-17 18:04:40 +01:00
|
|
|
// %09d would be enough, but a 0 before the number does not create problems, so let's be sure
|
|
|
|
nextPageUrlBuilder.append(String.format("%010d,", track.getInt("id")));
|
2020-03-17 15:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:04:40 +01:00
|
|
|
nextPageUrl = nextPageUrlBuilder.toString();
|
|
|
|
if (nextPageUrl.endsWith("&ids=")) {
|
|
|
|
// there are no other videos
|
|
|
|
nextPageUrl = "";
|
2020-03-17 15:12:13 +01:00
|
|
|
}
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNextPageUrl() throws IOException, ExtractionException {
|
2018-03-11 21:54:41 +01:00
|
|
|
if (nextPageUrl == null) {
|
2020-03-17 18:04:40 +01:00
|
|
|
computeInitialTracksAndNextPageUrl();
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
|
|
|
return nextPageUrl;
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
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"));
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:04:40 +01:00
|
|
|
// see computeInitialTracksAndNextPageUrl
|
|
|
|
final int lengthFirstPartOfUrl = ("https://api-v2.soundcloud.com/tracks?client_id="
|
|
|
|
+ SoundcloudParsingHelper.clientId()
|
|
|
|
+ "&ids=").length();
|
|
|
|
final int lengthOfEveryStream = 11;
|
|
|
|
|
|
|
|
String currentPageUrl;
|
|
|
|
int lengthMaxStreams = lengthFirstPartOfUrl + lengthOfEveryStream * streamsPerRequestedPage;
|
|
|
|
if (pageUrl.length() <= lengthMaxStreams) {
|
|
|
|
currentPageUrl = pageUrl; // fetch every remaining video, there are less than the max
|
|
|
|
nextPageUrl = ""; // afterwards the list is complete
|
|
|
|
} else {
|
|
|
|
currentPageUrl = pageUrl.substring(0, lengthMaxStreams);
|
|
|
|
nextPageUrl = pageUrl.substring(0, lengthFirstPartOfUrl) + pageUrl.substring(lengthMaxStreams);
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:20:50 +01:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2020-03-17 18:04:40 +01:00
|
|
|
String response = NewPipe.getDownloader().get(currentPageUrl, getExtractorLocalization()).responseBody();
|
2020-03-17 15:12:13 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
JsonArray tracks = JsonParser.array().from(response);
|
|
|
|
for (Object track : tracks) {
|
|
|
|
if (track instanceof JsonObject) {
|
|
|
|
collector.commit(new SoundcloudStreamInfoItemExtractor((JsonObject) track));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (JsonParserException e) {
|
|
|
|
throw new ParsingException("Could not parse json response", e);
|
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, nextPageUrl);
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
}
|