2017-08-04 16:21:45 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.soundcloud;
|
|
|
|
|
2018-03-04 21:30:31 +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;
|
2017-08-04 16:21:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.Downloader;
|
2018-07-13 18:02:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2017-08-06 22:20:15 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2017-08-11 03:23:09 +02:00
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
|
2017-08-04 16:21:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
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;
|
2018-09-15 21:47:53 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Localization;
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2017-08-06 22:20:15 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
@SuppressWarnings("WeakerAccess")
|
2017-08-11 03:23:09 +02:00
|
|
|
public class SoundcloudChannelExtractor extends ChannelExtractor {
|
2017-08-07 18:12:51 +02:00
|
|
|
private String userId;
|
2017-08-16 04:40:03 +02:00
|
|
|
private JsonObject user;
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2018-02-26 15:55:27 +01:00
|
|
|
private StreamInfoItemsCollector streamInfoItemsCollector = null;
|
|
|
|
private String nextPageUrl = null;
|
|
|
|
|
2018-09-15 21:47:53 +02:00
|
|
|
public SoundcloudChannelExtractor(StreamingService service, ListLinkHandler linkHandler, Localization localization) {
|
|
|
|
super(service, linkHandler, localization);
|
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
|
|
|
userId = getLinkHandler().getId();
|
2018-03-04 21:30:31 +01:00
|
|
|
String apiUrl = "https://api-v2.soundcloud.com/users/" + userId +
|
2017-08-06 22:20:15 +02:00
|
|
|
"?client_id=" + SoundcloudParsingHelper.clientId();
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-11-28 13:37:01 +01:00
|
|
|
String response = downloader.download(apiUrl);
|
2017-08-16 04:40:03 +02:00
|
|
|
try {
|
|
|
|
user = 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-07 18:12:51 +02:00
|
|
|
return userId;
|
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 getName() {
|
2017-08-07 18:12:51 +02:00
|
|
|
return user.getString("username");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getAvatarUrl() {
|
2017-08-16 04:40:03 +02:00
|
|
|
return user.getString("avatar_url");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-16 10:19:07 +02:00
|
|
|
public String getBannerUrl() {
|
2018-03-04 21:30:31 +01:00
|
|
|
return user.getObject("visuals", new JsonObject())
|
|
|
|
.getArray("visuals", new JsonArray())
|
|
|
|
.getObject(0, new JsonObject())
|
|
|
|
.getString("visual_url");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 03:23:09 +02:00
|
|
|
@Override
|
2017-09-16 10:19:07 +02:00
|
|
|
public String getFeedUrl() {
|
2017-08-11 03:23:09 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
@Override
|
|
|
|
public long getSubscriberCount() {
|
2017-08-16 04:40:03 +02:00
|
|
|
return user.getNumber("followers_count", 0).longValue();
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-26 15:55:27 +01:00
|
|
|
public String getDescription() {
|
2017-08-16 04:40:03 +02:00
|
|
|
return user.getString("description", "");
|
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 ExtractionException {
|
2018-02-26 15:55:27 +01:00
|
|
|
if(streamInfoItemsCollector == null) {
|
|
|
|
computeNextPageAndGetStreams();
|
|
|
|
}
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(streamInfoItemsCollector, getNextPageUrl());
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2018-02-26 15:55:27 +01:00
|
|
|
@Override
|
|
|
|
public String getNextPageUrl() throws ExtractionException {
|
|
|
|
if(nextPageUrl == null) {
|
|
|
|
computeNextPageAndGetStreams();
|
|
|
|
}
|
|
|
|
return nextPageUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void computeNextPageAndGetStreams() throws ExtractionException {
|
|
|
|
try {
|
|
|
|
streamInfoItemsCollector = new StreamInfoItemsCollector(getServiceId());
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2018-02-26 15:55:27 +01:00
|
|
|
String apiUrl = "https://api-v2.soundcloud.com/users/" + getId() + "/tracks"
|
|
|
|
+ "?client_id=" + SoundcloudParsingHelper.clientId()
|
|
|
|
+ "&limit=20"
|
|
|
|
+ "&linked_partitioning=1";
|
|
|
|
|
|
|
|
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, streamInfoItemsCollector, apiUrl);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ExtractionException("Could not get next page", e);
|
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
2017-08-06 22:20:15 +02:00
|
|
|
|
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(final 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-06 22:20:15 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 22:20:50 +01:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2018-02-26 15:55:27 +01:00
|
|
|
String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, pageUrl);
|
2017-08-06 22:20:15 +02:00
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, nextPageUrl);
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|