2017-08-04 16:21:45 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.soundcloud;
|
|
|
|
|
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;
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
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;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
|
|
|
|
|
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
|
|
|
|
2017-08-11 03:23:09 +02:00
|
|
|
public SoundcloudChannelExtractor(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException {
|
2017-08-06 22:20:15 +02:00
|
|
|
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-07 18:12:51 +02:00
|
|
|
userId = getUrlIdHandler().getId(getOriginalUrl());
|
|
|
|
String apiUrl = "https://api.soundcloud.com/users/" + userId +
|
2017-08-06 22:20:15 +02:00
|
|
|
"?client_id=" + SoundcloudParsingHelper.clientId();
|
2017-08-04 16:21:45 +02:00
|
|
|
|
|
|
|
String response = dl.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-06 22:20:15 +02:00
|
|
|
@Override
|
|
|
|
public String getCleanUrl() {
|
2017-08-16 04:40:03 +02:00
|
|
|
return user.isString("permalink_url") ? user.getString("permalink_url") : getOriginalUrl();
|
2017-08-06 22:20:15 +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() {
|
|
|
|
try {
|
|
|
|
return user.getObject("visuals").getArray("visuals").getObject(0).getString("visual_url", "");
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
return null;
|
|
|
|
}
|
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
|
2017-08-11 03:23:09 +02:00
|
|
|
public String getDescription() throws ParsingException {
|
2017-08-16 04:40:03 +02:00
|
|
|
return user.getString("description", "");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-11 03:23:09 +02:00
|
|
|
String apiUrl = "https://api-v2.soundcloud.com/users/" + getId() + "/tracks"
|
2017-08-06 22:20:15 +02:00
|
|
|
+ "?client_id=" + SoundcloudParsingHelper.clientId()
|
|
|
|
+ "&limit=20"
|
2017-08-04 16:21:45 +02:00
|
|
|
+ "&linked_partitioning=1";
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, apiUrl);
|
2017-08-04 16:21:45 +02:00
|
|
|
return collector;
|
|
|
|
}
|
2017-08-06 22:20:15 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public NextItemsResult getNextStreams() throws IOException, ExtractionException {
|
|
|
|
if (!hasMoreStreams()) {
|
2017-08-11 03:23:09 +02:00
|
|
|
throw new ExtractionException("Channel doesn't have more streams");
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId());
|
|
|
|
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, nextStreamsUrl);
|
|
|
|
|
2017-08-12 22:19:56 +02:00
|
|
|
return new NextItemsResult(collector, nextStreamsUrl);
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|