From 02e14b8931c903062733ab6bbc89a2040145a4da Mon Sep 17 00:00:00 2001 From: Marco Sirabella Date: Mon, 22 Jul 2024 02:22:47 -0700 Subject: [PATCH] Add support for on.soundcloud.com urls (#1179) Add support for on.soundcloud.com urls Fixes #1178 Co-authored-by: TobiGr --- .../soundcloud/SoundcloudParsingHelper.java | 20 ++++++++++++++++++- .../SoundcloudStreamLinkHandlerFactory.java | 3 ++- .../SoundcloudParsingHelperTest.java | 4 ++++ 3 files changed, 25 insertions(+), 2 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 930d79f51..621bc360d 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 @@ -45,6 +45,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.regex.Pattern; public final class SoundcloudParsingHelper { // CHECKSTYLE:OFF @@ -88,6 +89,10 @@ public final class SoundcloudParsingHelper { private static String clientId; public static final String SOUNDCLOUD_API_V2_URL = "https://api-v2.soundcloud.com/"; + private static final Pattern ON_URL_PATTERN = Pattern.compile( + "^https?://on.soundcloud.com/[0-9a-zA-Z]+$" + ); + private SoundcloudParsingHelper() { } @@ -185,8 +190,21 @@ public final class SoundcloudParsingHelper { */ public static String resolveIdWithWidgetApi(final String urlString) throws IOException, ParsingException { - // Remove the tailing slash from URLs due to issues with the SoundCloud API String fixedUrl = urlString; + + // if URL is an on.soundcloud link, do a request to resolve the redirect + + if (ON_URL_PATTERN.matcher(fixedUrl).find()) { + try { + fixedUrl = NewPipe.getDownloader().head(fixedUrl).latestUrl(); + // remove tracking params which are in the query string + fixedUrl = fixedUrl.split("\\?")[0]; + } catch (final ExtractionException e) { + throw new ParsingException("Could not follow on.soundcloud.com redirect", e); + } + } + + // Remove the tailing slash from URLs due to issues with the SoundCloud API if (fixedUrl.charAt(fixedUrl.length() - 1) == '/') { fixedUrl = fixedUrl.substring(0, fixedUrl.length() - 1); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudStreamLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudStreamLinkHandlerFactory.java index 64b34ef7a..e7809c52a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudStreamLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudStreamLinkHandlerFactory.java @@ -9,7 +9,8 @@ import org.schabi.newpipe.extractor.utils.Utils; public final class SoundcloudStreamLinkHandlerFactory extends LinkHandlerFactory { private static final SoundcloudStreamLinkHandlerFactory INSTANCE = new SoundcloudStreamLinkHandlerFactory(); - private static final String URL_PATTERN = "^https?://(www\\.|m\\.)?soundcloud.com/[0-9a-z_-]+" + private static final String URL_PATTERN = "^https?://(www\\.|m\\.|on\\.)?" + + "soundcloud.com/[0-9a-z_-]+" + "/(?!(tracks|albums|sets|reposts|followers|following)/?$)[0-9a-z_-]+/?([#?].*)?$"; private static final String API_URL_PATTERN = "^https?://api-v2\\.soundcloud.com" + "/(tracks|albums|sets|reposts|followers|following)/([0-9a-z_-]+)/"; 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 e8fc62992..fe3b923ad 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 @@ -25,6 +25,10 @@ class SoundcloudParsingHelperTest { void resolveIdWithWidgetApiTest() throws Exception { assertEquals("26057743", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://soundcloud.com/trapcity")); assertEquals("16069159", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://soundcloud.com/nocopyrightsounds")); + + assertEquals("26057743", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://on.soundcloud.com/Rr2JyfFcYwbawpw49")); + assertEquals("1818813498", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://on.soundcloud.com/a8QmYdMnmxnsSTEp9")); + assertEquals("1468401502", SoundcloudParsingHelper.resolveIdWithWidgetApi("https://on.soundcloud.com/rdt7e")); } } \ No newline at end of file