Skip to content

Commit 02e14b8

Browse files
mjsir911TobiGr
andauthored
Add support for on.soundcloud.com urls (#1179)
Add support for on.soundcloud.com urls Fixes #1178 Co-authored-by: TobiGr <[email protected]>
1 parent 0e15f9a commit 02e14b8

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.List;
4646
import java.util.Map;
4747
import java.util.stream.Collectors;
48+
import java.util.regex.Pattern;
4849

4950
public final class SoundcloudParsingHelper {
5051
// CHECKSTYLE:OFF
@@ -88,6 +89,10 @@ public final class SoundcloudParsingHelper {
8889
private static String clientId;
8990
public static final String SOUNDCLOUD_API_V2_URL = "https://api-v2.soundcloud.com/";
9091

92+
private static final Pattern ON_URL_PATTERN = Pattern.compile(
93+
"^https?://on.soundcloud.com/[0-9a-zA-Z]+$"
94+
);
95+
9196
private SoundcloudParsingHelper() {
9297
}
9398

@@ -185,8 +190,21 @@ public static String resolveUrlWithEmbedPlayer(final String apiUrl) throws IOExc
185190
*/
186191
public static String resolveIdWithWidgetApi(final String urlString) throws IOException,
187192
ParsingException {
188-
// Remove the tailing slash from URLs due to issues with the SoundCloud API
189193
String fixedUrl = urlString;
194+
195+
// if URL is an on.soundcloud link, do a request to resolve the redirect
196+
197+
if (ON_URL_PATTERN.matcher(fixedUrl).find()) {
198+
try {
199+
fixedUrl = NewPipe.getDownloader().head(fixedUrl).latestUrl();
200+
// remove tracking params which are in the query string
201+
fixedUrl = fixedUrl.split("\\?")[0];
202+
} catch (final ExtractionException e) {
203+
throw new ParsingException("Could not follow on.soundcloud.com redirect", e);
204+
}
205+
}
206+
207+
// Remove the tailing slash from URLs due to issues with the SoundCloud API
190208
if (fixedUrl.charAt(fixedUrl.length() - 1) == '/') {
191209
fixedUrl = fixedUrl.substring(0, fixedUrl.length() - 1);
192210
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudStreamLinkHandlerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
public final class SoundcloudStreamLinkHandlerFactory extends LinkHandlerFactory {
1010
private static final SoundcloudStreamLinkHandlerFactory INSTANCE
1111
= new SoundcloudStreamLinkHandlerFactory();
12-
private static final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/[0-9a-z_-]+"
12+
private static final String URL_PATTERN = "^https?://(www\\.|m\\.|on\\.)?"
13+
+ "soundcloud.com/[0-9a-z_-]+"
1314
+ "/(?!(tracks|albums|sets|reposts|followers|following)/?$)[0-9a-z_-]+/?([#?].*)?$";
1415
private static final String API_URL_PATTERN = "^https?://api-v2\\.soundcloud.com"
1516
+ "/(tracks|albums|sets|reposts|followers|following)/([0-9a-z_-]+)/";

extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelperTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ void resolveUrlWithEmbedPlayerTest() throws Exception {
2525
void resolveIdWithWidgetApiTest() throws Exception {
2626
assertEquals("26057743", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://soundcloud.com/trapcity"));
2727
assertEquals("16069159", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://soundcloud.com/nocopyrightsounds"));
28+
29+
assertEquals("26057743", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://on.soundcloud.com/Rr2JyfFcYwbawpw49"));
30+
assertEquals("1818813498", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://on.soundcloud.com/a8QmYdMnmxnsSTEp9"));
31+
assertEquals("1468401502", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://on.soundcloud.com/rdt7e"));
2832
}
2933

3034
}

0 commit comments

Comments
 (0)