[SoundCloud] Add test for hardcoded client id

This commit is contained in:
Mauricio Colli 2019-10-22 21:41:52 -03:00 committed by Tobias Groza
parent 4fc18a6994
commit ddd563fe78
2 changed files with 18 additions and 8 deletions

View File

@ -31,6 +31,7 @@ import java.util.HashMap;
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
public class SoundcloudParsingHelper { public class SoundcloudParsingHelper {
private static final String HARDCODED_CLIENT_ID = "LHzSAKe8eP9Yy3FgBugfBapRPLncO6Ng"; // Updated on 22/10/19
private static String clientId; private static String clientId;
private SoundcloudParsingHelper() { private SoundcloudParsingHelper() {
@ -40,11 +41,8 @@ public class SoundcloudParsingHelper {
if (clientId != null && !clientId.isEmpty()) return clientId; if (clientId != null && !clientId.isEmpty()) return clientId;
Downloader dl = NewPipe.getDownloader(); Downloader dl = NewPipe.getDownloader();
clientId = "LHzSAKe8eP9Yy3FgBugfBapRPLncO6Ng"; // Updated on 22/10/19 clientId = HARDCODED_CLIENT_ID;
final String apiUrl = "https://api.soundcloud.com/connect?client_id=" + clientId; if (checkIfHardcodedClientIdIsValid(dl)) {
// 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) {
return clientId; return clientId;
} }
@ -75,6 +73,12 @@ public class SoundcloudParsingHelper {
throw new ExtractionException("Couldn't extract client id"); 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 { public static String toDateString(String time) throws ParsingException {
try { try {
Date date; Date date;

View File

@ -1,18 +1,24 @@
package org.schabi.newpipe.extractor.services.soundcloud; package org.schabi.newpipe.extractor.services.soundcloud;
import org.junit.Assert; import org.junit.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.Downloader; import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.utils.Localization; import org.schabi.newpipe.extractor.utils.Localization;
import static org.junit.Assert.*;
public class SoundcloudParsingHelperTest { public class SoundcloudParsingHelperTest {
@BeforeClass @BeforeClass
public static void setUp() { public static void setUp() {
NewPipe.init(Downloader.getInstance(), new Localization("GB", "en")); 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 @Test
public void resolveUrlWithEmbedPlayerTest() throws Exception { public void resolveUrlWithEmbedPlayerTest() throws Exception {
Assert.assertEquals("https://soundcloud.com/trapcity", SoundcloudParsingHelper.resolveUrlWithEmbedPlayer("https://api.soundcloud.com/users/26057743")); Assert.assertEquals("https://soundcloud.com/trapcity", SoundcloudParsingHelper.resolveUrlWithEmbedPlayer("https://api.soundcloud.com/users/26057743"));