From 86308d060384a296637b7205e27259a7824e5810 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 15 May 2021 19:25:17 +0200 Subject: [PATCH] Use a lightweight request to check if the client_id is valid Request the api-v2 host with the client_id instead of checking if the streams of a SoundCloud track are not empty: if it is valid, the API returns 404, otherwise it should return 401. --- .../soundcloud/SoundcloudParsingHelper.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java index 194756cfc..21983bfcc 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java @@ -44,6 +44,7 @@ public class SoundcloudParsingHelper { private static final String HARDCODED_CLIENT_ID = "NcIaRZItQCNQp3Vq9Plvzf7tvjmVJnF6"; // Updated on 26/04/21 private static String clientId; + public static final String SOUNDCLOUD_API_V2 = "https://api-v2.soundcloud.com/"; private SoundcloudParsingHelper() { } @@ -88,17 +89,12 @@ public class SoundcloudParsingHelper { throw new ExtractionException("Couldn't extract client id"); } - static boolean checkIfHardcodedClientIdIsValid() { - try { - SoundcloudStreamExtractor e = (SoundcloudStreamExtractor) SoundCloud - .getStreamExtractor( - "https://soundcloud.com/liluzivert/do-what-i-want-produced-by-maaly-raw-don-cannon"); - e.fetchPage(); - return !e.getAudioStreams().isEmpty(); - } catch (final Exception ignored) { - // No need to throw an exception here. If something went wrong, the client_id is wrong - return false; - } + static boolean checkIfHardcodedClientIdIsValid() throws IOException, ReCaptchaException { + final int responseCode = NewPipe.getDownloader().get(SOUNDCLOUD_API_V2 + "?client_id=" + + HARDCODED_CLIENT_ID).responseCode(); + // If the response code is 404, it means that the client_id is valid; otherwise, + // it should be not valid + return responseCode == 404; } public static OffsetDateTime parseDateFrom(final String textualUploadDate) @@ -123,7 +119,7 @@ public class SoundcloudParsingHelper { */ public static JsonObject resolveFor(@Nonnull final Downloader downloader, final String url) throws IOException, ExtractionException { - final String apiUrl = "https://api-v2.soundcloud.com/resolve" + "?url=" + final String apiUrl = SOUNDCLOUD_API_V2 + "resolve" + "?url=" + URLEncoder.encode(url, UTF_8) + "&client_id=" + clientId(); try {