From f803558d1d3c87f9cefcbec2819615721d71d05a Mon Sep 17 00:00:00 2001 From: Scratch Date: Sun, 11 Oct 2020 14:12:38 +1100 Subject: [PATCH] Fix parsing Soundcloud tracks that contain the term 'sets' --- .../soundcloud/SoundcloudParsingHelper.java | 15 ++++++++++++--- .../SoundcloudChannelLinkHandlerFactory.java | 2 ++ .../SoundcloudPlaylistLinkHandlerFactory.java | 2 ++ 3 files changed, 16 insertions(+), 3 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 29a625a905..78a49a61ed 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 @@ -25,6 +25,8 @@ import javax.annotation.Nonnull; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -148,12 +150,19 @@ public static String resolveUrlWithEmbedPlayer(String apiUrl) throws IOException * * @return the resolved id */ - public static String resolveIdWithEmbedPlayer(String url) throws IOException, ReCaptchaException, ParsingException { + public static String resolveIdWithEmbedPlayer(String urlString) throws IOException, ReCaptchaException, ParsingException { + URL url; + + try { + url = Utils.stringToURL(urlString); + } catch (MalformedURLException e){ + throw new IllegalArgumentException("The given URL is not valid"); + } String response = NewPipe.getDownloader().get("https://w.soundcloud.com/player/?url=" - + URLEncoder.encode(url, "UTF-8"), SoundCloud.getLocalization()).responseBody(); + + URLEncoder.encode(url.toString(), "UTF-8"), SoundCloud.getLocalization()).responseBody(); // handle playlists / sets different and get playlist id via uir field in JSON - if (url.contains("sets") && !url.endsWith("sets") && !url.endsWith("sets/")) + if (url.getPath().contains("/sets/") && !url.getPath().endsWith("/sets")) return Parser.matchGroup1("\"uri\":\\s*\"https:\\/\\/api\\.soundcloud\\.com\\/playlists\\/((\\d)*?)\"", response); return Parser.matchGroup1(",\"id\":(([^}\\n])*?),", response); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudChannelLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudChannelLinkHandlerFactory.java index 4ef513b873..addde00671 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudChannelLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudChannelLinkHandlerFactory.java @@ -21,6 +21,8 @@ public static SoundcloudChannelLinkHandlerFactory getInstance() { @Override public String getId(String url) throws ParsingException { Utils.checkUrl(URL_PATTERN, url); + // Remove the tailing slash from URLs due to issues with the SoundCloud API + if (url.charAt(url.length() -1) == '/') url = url.substring(0, url.length()-1); try { return SoundcloudParsingHelper.resolveIdWithEmbedPlayer(url); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudPlaylistLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudPlaylistLinkHandlerFactory.java index 53a70d3aa4..77000745cb 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudPlaylistLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudPlaylistLinkHandlerFactory.java @@ -20,6 +20,8 @@ public static SoundcloudPlaylistLinkHandlerFactory getInstance() { @Override public String getId(String url) throws ParsingException { Utils.checkUrl(URL_PATTERN, url); + // Remove the tailing slash from URLs due to issues with the SoundCloud API + if (url.charAt(url.length() -1) == '/') url = url.substring(0, url.length()-1); try { return SoundcloudParsingHelper.resolveIdWithEmbedPlayer(url);