Add support for on.soundcloud.com urls (#1179)

Add support for on.soundcloud.com urls

Fixes #1178

Co-authored-by: TobiGr <tobigr@users.noreply.github.com>
This commit is contained in:
Marco Sirabella 2024-07-22 02:22:47 -07:00 committed by GitHub
parent 0e15f9ac1b
commit 02e14b8931
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 2 deletions

View File

@ -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);
}

View File

@ -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_-]+)/";

View File

@ -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"));
}
}