Skip to content

Commit

Permalink
[SoundCloud] Add test for hardcoded client id
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciocolli authored and TobiGr committed Oct 23, 2019
1 parent 4fc18a6 commit ddd563f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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() {
Expand All @@ -40,11 +41,8 @@ public static String clientId() throws ExtractionException, IOException {
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;
}

Expand Down Expand Up @@ -75,6 +73,12 @@ public static String clientId() throws ExtractionException, IOException {
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
Expand Down

0 comments on commit ddd563f

Please sign in to comment.