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

142 lines
4.6 KiB
Java
Raw Normal View History

package org.schabi.newpipe.extractor.services.soundcloud.extractors;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
2020-04-15 14:09:46 +02:00
import org.schabi.newpipe.extractor.Page;
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.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
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;
import javax.annotation.Nonnull;
2021-02-07 22:42:21 +01:00
import java.io.IOException;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
2021-02-07 22:42:21 +01:00
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2020-04-16 16:08:14 +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;
private JsonObject user;
private static final String USERS_ENDPOINT = SOUNDCLOUD_API_V2_URL + "users/";
public SoundcloudChannelExtractor(final StreamingService service,
final ListLinkHandler linkHandler) {
super(service, linkHandler);
2017-08-06 22:20:15 +02:00
}
2017-08-06 22:20:15 +02:00
@Override
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException,
ExtractionException {
userId = getLinkHandler().getId();
final String apiUrl = USERS_ENDPOINT + userId + "?client_id="
+ SoundcloudParsingHelper.clientId();
2020-07-06 20:23:41 +02:00
final String response = downloader.get(apiUrl, getExtractorLocalization()).responseBody();
try {
user = JsonParser.object().from(response);
} catch (final JsonParserException e) {
throw new ParsingException("Could not parse json response", e);
}
}
@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() {
return user.getObject("visuals").getArray("visuals").getObject(0)
.getString("visual_url");
}
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() {
2020-06-13 20:25:38 +02:00
return user.getLong("followers_count", 0);
}
@Override
2018-02-26 15:55:27 +01:00
public String getDescription() {
2020-04-16 16:08:14 +02:00
return user.getString("description", EMPTY_STRING);
}
@Override
2020-07-06 20:23:41 +02:00
public String getParentChannelName() {
return "";
}
@Override
2020-07-06 20:23:41 +02:00
public String getParentChannelUrl() {
return "";
}
@Override
2020-07-06 20:23:41 +02:00
public String getParentChannelAvatarUrl() {
return "";
}
@Override
public boolean isVerified() throws ParsingException {
return user.getBoolean("verified");
}
2017-11-25 02:03:30 +01:00
@Nonnull
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
2018-02-26 15:55:27 +01:00
try {
final StreamInfoItemsCollector streamInfoItemsCollector =
new StreamInfoItemsCollector(getServiceId());
final String apiUrl = USERS_ENDPOINT + getId() + "/tracks" + "?client_id="
+ SoundcloudParsingHelper.clientId() + "&limit=20" + "&linked_partitioning=1";
2018-02-26 15:55:27 +01:00
final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15,
streamInfoItemsCollector, apiUrl);
2020-04-15 14:09:46 +02:00
return new InfoItemsPage<>(streamInfoItemsCollector, new Page(nextPageUrl));
} catch (final Exception e) {
2018-02-26 15:55:27 +01:00
throw new ExtractionException("Could not get next page", e);
}
}
2017-08-06 22:20:15 +02:00
@Override
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException,
ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL");
}
2020-07-06 20:23:41 +02:00
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector,
page.getUrl());
2017-08-06 22:20:15 +02:00
2020-04-15 14:09:46 +02:00
return new InfoItemsPage<>(collector, new Page(nextPageUrl));
2017-08-06 22:20:15 +02:00
}
}