Use the SOUNDCLOUD_API_V2_URL constant in all the SoundCloud package

This commit is contained in:
TiA4f8R 2021-05-15 19:42:29 +02:00
parent 86308d0603
commit 4552ea9c9f
No known key found for this signature in database
GPG key ID: E6D3E7F5949450DD
8 changed files with 27 additions and 22 deletions

View file

@ -16,7 +16,6 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudChannelInfoItemExtractor; import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudChannelInfoItemExtractor;
import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudStreamExtractor;
import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudStreamInfoItemExtractor; import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudStreamInfoItemExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.JsonUtils;
@ -44,7 +43,7 @@ public class SoundcloudParsingHelper {
private static final String HARDCODED_CLIENT_ID = private static final String HARDCODED_CLIENT_ID =
"NcIaRZItQCNQp3Vq9Plvzf7tvjmVJnF6"; // Updated on 26/04/21 "NcIaRZItQCNQp3Vq9Plvzf7tvjmVJnF6"; // Updated on 26/04/21
private static String clientId; private static String clientId;
public static final String SOUNDCLOUD_API_V2 = "https://api-v2.soundcloud.com/"; public static final String SOUNDCLOUD_API_V2_URL = "https://api-v2.soundcloud.com/";
private SoundcloudParsingHelper() { private SoundcloudParsingHelper() {
} }
@ -77,8 +76,9 @@ public class SoundcloudParsingHelper {
final String srcUrl = element.attr("src"); final String srcUrl = element.attr("src");
if (!isNullOrEmpty(srcUrl)) { if (!isNullOrEmpty(srcUrl)) {
try { try {
return clientId = Parser.matchGroup1(clientIdPattern, dl.get(srcUrl, headers) clientId = Parser.matchGroup1(clientIdPattern, dl.get(srcUrl, headers)
.responseBody()); .responseBody());
return clientId;
} catch (final RegexException ignored) { } catch (final RegexException ignored) {
// Ignore it and proceed to try searching other script // Ignore it and proceed to try searching other script
} }
@ -90,7 +90,7 @@ public class SoundcloudParsingHelper {
} }
static boolean checkIfHardcodedClientIdIsValid() throws IOException, ReCaptchaException { static boolean checkIfHardcodedClientIdIsValid() throws IOException, ReCaptchaException {
final int responseCode = NewPipe.getDownloader().get(SOUNDCLOUD_API_V2 + "?client_id=" final int responseCode = NewPipe.getDownloader().get(SOUNDCLOUD_API_V2_URL + "?client_id="
+ HARDCODED_CLIENT_ID).responseCode(); + HARDCODED_CLIENT_ID).responseCode();
// If the response code is 404, it means that the client_id is valid; otherwise, // If the response code is 404, it means that the client_id is valid; otherwise,
// it should be not valid // it should be not valid
@ -119,7 +119,7 @@ public class SoundcloudParsingHelper {
*/ */
public static JsonObject resolveFor(@Nonnull final Downloader downloader, final String url) public static JsonObject resolveFor(@Nonnull final Downloader downloader, final String url)
throws IOException, ExtractionException { throws IOException, ExtractionException {
final String apiUrl = SOUNDCLOUD_API_V2 + "resolve" + "?url=" final String apiUrl = SOUNDCLOUD_API_V2_URL + "resolve" + "?url="
+ URLEncoder.encode(url, UTF_8) + "&client_id=" + clientId(); + URLEncoder.encode(url, UTF_8) + "&client_id=" + clientId();
try { try {

View file

@ -17,6 +17,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING; import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
@ -24,6 +25,7 @@ import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class SoundcloudChannelExtractor extends ChannelExtractor { public class SoundcloudChannelExtractor extends ChannelExtractor {
private String userId; private String userId;
private JsonObject user; private JsonObject user;
private static final String USERS_ENDPOINT = SOUNDCLOUD_API_V2_URL + "users/";
public SoundcloudChannelExtractor(final StreamingService service, public SoundcloudChannelExtractor(final StreamingService service,
final ListLinkHandler linkHandler) { final ListLinkHandler linkHandler) {
@ -35,8 +37,8 @@ public class SoundcloudChannelExtractor extends ChannelExtractor {
ExtractionException { ExtractionException {
userId = getLinkHandler().getId(); userId = getLinkHandler().getId();
final String apiUrl = "https://api-v2.soundcloud.com/users/" + userId + final String apiUrl = USERS_ENDPOINT + userId + "?client_id="
"?client_id=" + SoundcloudParsingHelper.clientId(); + SoundcloudParsingHelper.clientId();
final String response = downloader.get(apiUrl, getExtractorLocalization()).responseBody(); final String response = downloader.get(apiUrl, getExtractorLocalization()).responseBody();
try { try {
@ -111,9 +113,8 @@ public class SoundcloudChannelExtractor extends ChannelExtractor {
final StreamInfoItemsCollector streamInfoItemsCollector = final StreamInfoItemsCollector streamInfoItemsCollector =
new StreamInfoItemsCollector(getServiceId()); new StreamInfoItemsCollector(getServiceId());
final String apiUrl = "https://api-v2.soundcloud.com/users/" + getId() + "/tracks" final String apiUrl = USERS_ENDPOINT + getId() + "/tracks" + "?client_id="
+ "?client_id=" + SoundcloudParsingHelper.clientId() + "&limit=20" + SoundcloudParsingHelper.clientId() + "&limit=20" + "&linked_partitioning=1";
+ "&linked_partitioning=1";
final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15,
streamInfoItemsCollector, apiUrl); streamInfoItemsCollector, apiUrl);

View file

@ -15,6 +15,7 @@ import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class SoundcloudChartsExtractor extends KioskExtractor<StreamInfoItem> { public class SoundcloudChartsExtractor extends KioskExtractor<StreamInfoItem> {
@ -53,9 +54,8 @@ public class SoundcloudChartsExtractor extends KioskExtractor<StreamInfoItem> {
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
String apiUrl = "https://api-v2.soundcloud.com/charts" + String apiUrl = SOUNDCLOUD_API_V2_URL + "charts" + "?genre=soundcloud:genres:all-music"
"?genre=soundcloud:genres:all-music" + "&client_id=" + "&client_id=" + SoundcloudParsingHelper.clientId();
+ SoundcloudParsingHelper.clientId();
if (getId().equals("Top 50")) { if (getId().equals("Top 50")) {
apiUrl += "&kind=top"; apiUrl += "&kind=top";

View file

@ -25,6 +25,7 @@ import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class SoundcloudPlaylistExtractor extends PlaylistExtractor { public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
@ -43,8 +44,8 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
ExtractionException { ExtractionException {
playlistId = getLinkHandler().getId(); playlistId = getLinkHandler().getId();
final String apiUrl = "https://api-v2.soundcloud.com/playlists/" + playlistId + final String apiUrl = SOUNDCLOUD_API_V2_URL + "playlists/" + playlistId + "?client_id="
"?client_id=" + SoundcloudParsingHelper.clientId() + "&representation=compact"; + SoundcloudParsingHelper.clientId() + "&representation=compact";
final String response = downloader.get(apiUrl, getExtractorLocalization()).responseBody(); final String response = downloader.get(apiUrl, getExtractorLocalization()).responseBody();
try { try {
@ -182,7 +183,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
nextIds = page.getIds().subList(STREAMS_PER_REQUESTED_PAGE, page.getIds().size()); nextIds = page.getIds().subList(STREAMS_PER_REQUESTED_PAGE, page.getIds().size());
} }
final String currentPageUrl = "https://api-v2.soundcloud.com/tracks?client_id=" final String currentPageUrl = SOUNDCLOUD_API_V2_URL + "tracks?client_id="
+ SoundcloudParsingHelper.clientId() + "&ids=" + Utils.join(",", currentIds); + SoundcloudParsingHelper.clientId() + "&ids=" + Utils.join(",", currentIds);
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());

View file

@ -30,6 +30,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.*; import static org.schabi.newpipe.extractor.utils.Utils.*;
public class SoundcloudStreamExtractor extends StreamExtractor { public class SoundcloudStreamExtractor extends StreamExtractor {
@ -364,7 +365,7 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
public StreamInfoItemsCollector getRelatedItems() throws IOException, ExtractionException { public StreamInfoItemsCollector getRelatedItems() throws IOException, ExtractionException {
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
final String apiUrl = "https://api-v2.soundcloud.com/tracks/" + urlEncode(getId()) final String apiUrl = SOUNDCLOUD_API_V2_URL + "tracks/" + urlEncode(getId())
+ "/related?client_id=" + urlEncode(SoundcloudParsingHelper.clientId()); + "/related?client_id=" + urlEncode(SoundcloudParsingHelper.clientId());
SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl); SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl);

View file

@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS; import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
@ -42,9 +43,8 @@ public class SoundcloudSubscriptionExtractor extends SubscriptionExtractor {
throw new InvalidSourceException(e); throw new InvalidSourceException(e);
} }
final String apiUrl = "https://api-v2.soundcloud.com/users/" + id + "/followings" final String apiUrl = SOUNDCLOUD_API_V2_URL + "users/" + id + "/followings" + "?client_id="
+ "?client_id=" + SoundcloudParsingHelper.clientId() + SoundcloudParsingHelper.clientId() + "&limit=200";
+ "&limit=200";
final ChannelInfoItemsCollector collector = new ChannelInfoItemsCollector(service final ChannelInfoItemsCollector collector = new ChannelInfoItemsCollector(service
.getServiceId()); .getServiceId());
// ± 2000 is the limit of followings on SoundCloud, so this minimum should be enough // ± 2000 is the limit of followings on SoundCloud, so this minimum should be enough

View file

@ -17,6 +17,7 @@ import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public class SoundcloudSuggestionExtractor extends SuggestionExtractor { public class SoundcloudSuggestionExtractor extends SuggestionExtractor {
@ -30,7 +31,7 @@ public class SoundcloudSuggestionExtractor extends SuggestionExtractor {
ExtractionException { ExtractionException {
final List<String> suggestions = new ArrayList<>(); final List<String> suggestions = new ArrayList<>();
final Downloader dl = NewPipe.getDownloader(); final Downloader dl = NewPipe.getDownloader();
final String url = "https://api-v2.soundcloud.com/search/queries" + "?q=" final String url = SOUNDCLOUD_API_V2_URL + "search/queries" + "?q="
+ URLEncoder.encode(query, UTF_8) + "&client_id=" + URLEncoder.encode(query, UTF_8) + "&client_id="
+ SoundcloudParsingHelper.clientId() + "&limit=10"; + SoundcloudParsingHelper.clientId() + "&limit=10";
final String response = dl.get(url, getExtractorLocalization()).responseBody(); final String response = dl.get(url, getExtractorLocalization()).responseBody();

View file

@ -11,6 +11,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public class SoundcloudSearchQueryHandlerFactory extends SearchQueryHandlerFactory { public class SoundcloudSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
@ -28,7 +29,7 @@ public class SoundcloudSearchQueryHandlerFactory extends SearchQueryHandlerFacto
final String sortFilter) final String sortFilter)
throws ParsingException { throws ParsingException {
try { try {
String url = "https://api-v2.soundcloud.com/search"; String url = SOUNDCLOUD_API_V2_URL + "search";
if (!contentFilter.isEmpty()) { if (!contentFilter.isEmpty()) {
switch (contentFilter.get(0)) { switch (contentFilter.get(0)) {