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

115 lines
3.6 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;
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;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
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")
2017-08-11 03:23:09 +02:00
public class SoundcloudChannelExtractor extends ChannelExtractor {
2017-08-07 18:12:51 +02:00
private String userId;
private JsonObject user;
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-06 22:20:15 +02:00
@Override
public void fetchPage() throws IOException, ExtractionException {
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();
String response = dl.download(apiUrl);
try {
user = 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 user.isString("permalink_url") ? user.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() {
2017-08-07 18:12:51 +02:00
return userId;
}
@Nonnull
@Override
2017-08-11 03:23:09 +02:00
public String getName() {
2017-08-07 18:12:51 +02:00
return user.getString("username");
}
@Override
public String getAvatarUrl() {
return user.getString("avatar_url");
}
@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-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;
}
@Override
public long getSubscriberCount() {
return user.getNumber("followers_count", 0).longValue();
}
@Override
2017-08-11 03:23:09 +02:00
public String getDescription() throws ParsingException {
return user.getString("description", "");
}
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-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"
+ "&linked_partitioning=1";
2017-08-06 22:20:15 +02:00
nextStreamsUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, apiUrl);
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);
return new NextItemsResult(collector, nextStreamsUrl);
2017-08-06 22:20:15 +02:00
}
}