2017-08-04 16:21:45 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.soundcloud;
|
|
|
|
|
|
|
|
import org.json.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-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-08-07 18:12:51 +02:00
|
|
|
import org.schabi.newpipe.extractor.user.UserExtractor;
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
@SuppressWarnings("WeakerAccess")
|
2017-08-07 18:12:51 +02:00
|
|
|
public class SoundcloudUserExtractor extends UserExtractor {
|
|
|
|
private String userId;
|
|
|
|
private JSONObject user;
|
2017-08-04 16:21:45 +02:00
|
|
|
|
2017-08-07 18:12:51 +02:00
|
|
|
public SoundcloudUserExtractor(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-07 18:12:51 +02:00
|
|
|
user = new JSONObject(response);
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:21:45 +02:00
|
|
|
@Override
|
2017-08-07 18:12:51 +02:00
|
|
|
public String getUserId() {
|
|
|
|
return userId;
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-07 18:12:51 +02:00
|
|
|
public String getUserName() {
|
|
|
|
return user.getString("username");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getAvatarUrl() {
|
2017-08-07 18:12:51 +02:00
|
|
|
return user.getString("avatar_url");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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");
|
2017-08-04 16:21:45 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get Banner", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getSubscriberCount() {
|
2017-08-07 18:12:51 +02:00
|
|
|
return user.getLong("followers_count");
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFeedUrl() throws ParsingException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-07 18:12:51 +02:00
|
|
|
String apiUrl = "https://api-v2.soundcloud.com/users/" + getUserId() + "/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-07 18:12:51 +02:00
|
|
|
throw new ExtractionException("User 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);
|
|
|
|
}
|
2017-08-07 19:00:36 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() throws ParsingException {
|
|
|
|
return user.getString("description");
|
|
|
|
}
|
2017-08-04 16:21:45 +02:00
|
|
|
}
|