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 f0d39bf0e..96b7fcea7 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 @@ -31,6 +31,7 @@ import java.util.HashMap; import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; public class SoundcloudParsingHelper { + private static final String HARDCODED_CLIENT_ID = "LHzSAKe8eP9Yy3FgBugfBapRPLncO6Ng"; // Updated on 22/10/19 private static String clientId; private SoundcloudParsingHelper() { @@ -40,11 +41,8 @@ public class SoundcloudParsingHelper { if (clientId != null && !clientId.isEmpty()) return clientId; Downloader dl = NewPipe.getDownloader(); - clientId = "LHzSAKe8eP9Yy3FgBugfBapRPLncO6Ng"; // Updated on 22/10/19 - final String apiUrl = "https://api.soundcloud.com/connect?client_id=" + clientId; - // Should return 200 to indicate that the client id is valid, a 401 is returned otherwise. - // In that case, the fallback method is used. - if (dl.head(apiUrl).getResponseCode() == 200) { + clientId = HARDCODED_CLIENT_ID; + if (checkIfHardcodedClientIdIsValid(dl)) { return clientId; } @@ -75,6 +73,12 @@ public class SoundcloudParsingHelper { throw new ExtractionException("Couldn't extract client id"); } + static boolean checkIfHardcodedClientIdIsValid(Downloader dl) throws IOException, ReCaptchaException { + final String apiUrl = "https://api.soundcloud.com/connect?client_id=" + HARDCODED_CLIENT_ID; + // Should return 200 to indicate that the client id is valid, a 401 is returned otherwise. + return dl.head(apiUrl).getResponseCode() == 200; + } + public static String toDateString(String time) throws ParsingException { try { Date date; diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelperTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelperTest.java index b1771f068..435ae446c 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelperTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelperTest.java @@ -1,18 +1,24 @@ package org.schabi.newpipe.extractor.services.soundcloud; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import org.schabi.newpipe.Downloader; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.utils.Localization; +import static org.junit.Assert.*; + public class SoundcloudParsingHelperTest { @BeforeClass public static void setUp() { NewPipe.init(Downloader.getInstance(), new Localization("GB", "en")); } + @Test + public void assertThatHardcodedClientIdIsValid() throws Exception { + assertTrue("Hardcoded client id is not valid anymore", + SoundcloudParsingHelper.checkIfHardcodedClientIdIsValid(Downloader.getInstance())); + } + @Test public void resolveUrlWithEmbedPlayerTest() throws Exception { Assert.assertEquals("https://soundcloud.com/trapcity", SoundcloudParsingHelper.resolveUrlWithEmbedPlayer("https://api.soundcloud.com/users/26057743"));