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

108 lines
3.4 KiB
Java
Raw Normal View History

package org.schabi.newpipe.extractor.services.soundcloud;
import com.github.openjson.JSONObject;
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;
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);
2017-08-07 18:12:51 +02:00
user = new JSONObject(response);
}
2017-08-06 22:20:15 +02:00
@Override
public String getCleanUrl() {
try {
2017-08-07 18:12:51 +02:00
return user.getString("permalink_url");
2017-08-06 22:20:15 +02:00
} catch (Exception e) {
return getOriginalUrl();
}
}
@Override
2017-08-11 03:23:09 +02:00
public String getId() {
2017-08-07 18:12:51 +02:00
return userId;
}
@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.optString("avatar_url");
}
@Override
public String getBannerUrl() throws ParsingException {
try {
2017-08-07 18:12:51 +02:00
return user.getJSONObject("visuals").getJSONArray("visuals").getJSONObject(0).getString("visual_url");
} catch (Exception e) {
throw new ParsingException("Could not get Banner", e);
}
}
2017-08-11 03:23:09 +02:00
@Override
public String getFeedUrl() throws ParsingException {
return null;
}
@Override
public long getSubscriberCount() {
2017-08-11 20:21:49 +02:00
return user.optLong("followers_count");
}
@Override
2017-08-11 03:23:09 +02:00
public String getDescription() throws ParsingException {
return user.optString("description");
}
@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.getItemList(), nextStreamsUrl);
}
}