|
19 | 19 | import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudStreamExtractor; |
20 | 20 | import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudStreamInfoItemExtractor; |
21 | 21 | import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; |
| 22 | +import org.schabi.newpipe.extractor.utils.JsonUtils; |
22 | 23 | import org.schabi.newpipe.extractor.utils.Parser; |
23 | 24 | import org.schabi.newpipe.extractor.utils.Parser.RegexException; |
24 | 25 | import org.schabi.newpipe.extractor.utils.Utils; |
|
40 | 41 | import static org.schabi.newpipe.extractor.utils.Utils.*; |
41 | 42 |
|
42 | 43 | public class SoundcloudParsingHelper { |
43 | | - private static final String HARDCODED_CLIENT_ID = "Kl9G8jQT22DxqatQk09IjWRujGlut5Vd"; // Updated on 04/03/21 |
| 44 | + private static final String HARDCODED_CLIENT_ID = "NcIaRZItQCNQp3Vq9Plvzf7tvjmVJnF6"; // Updated on 26/04/21 |
44 | 45 | private static String clientId; |
45 | 46 |
|
46 | 47 | private SoundcloudParsingHelper() { |
@@ -140,27 +141,37 @@ public static String resolveUrlWithEmbedPlayer(String apiUrl) throws IOException |
140 | 141 | } |
141 | 142 |
|
142 | 143 | /** |
143 | | - * Fetch the embed player with the url and return the id (like the id from the json api). |
| 144 | + * Fetch the widget API with the url and return the id (like the id from the json api). |
144 | 145 | * |
145 | 146 | * @return the resolved id |
146 | 147 | */ |
147 | | - public static String resolveIdWithEmbedPlayer(String urlString) throws IOException, ReCaptchaException, ParsingException { |
| 148 | + public static String resolveIdWithWidgetApi(String urlString) throws IOException, ReCaptchaException, ParsingException { |
148 | 149 | // Remove the tailing slash from URLs due to issues with the SoundCloud API |
149 | 150 | if (urlString.charAt(urlString.length() - 1) == '/') urlString = urlString.substring(0, urlString.length() - 1); |
| 151 | + // Make URL lower case and remove www. if it exists. |
| 152 | + // Without doing this, the widget API does not recognize the URL. |
| 153 | + urlString = Utils.removeWWWFromUrl(urlString.toLowerCase()); |
150 | 154 |
|
151 | | - URL url; |
| 155 | + final URL url; |
152 | 156 | try { |
153 | 157 | url = Utils.stringToURL(urlString); |
154 | 158 | } catch (MalformedURLException e) { |
155 | 159 | throw new IllegalArgumentException("The given URL is not valid"); |
156 | 160 | } |
157 | 161 |
|
158 | | - String response = NewPipe.getDownloader().get("https://w.soundcloud.com/player/?url=" |
159 | | - + URLEncoder.encode(url.toString(), UTF_8), SoundCloud.getLocalization()).responseBody(); |
160 | | - // handle playlists / sets different and get playlist id via uir field in JSON |
161 | | - if (url.getPath().contains("/sets/") && !url.getPath().endsWith("/sets")) |
162 | | - return Parser.matchGroup1("\"uri\":\\s*\"https:\\/\\/api\\.soundcloud\\.com\\/playlists\\/((\\d)*?)\"", response); |
163 | | - return Parser.matchGroup1(",\"id\":(([^}\\n])*?),", response); |
| 162 | + try { |
| 163 | + final String widgetUrl = "https://api-widget.soundcloud.com/resolve?url=" |
| 164 | + + URLEncoder.encode(url.toString(), UTF_8) |
| 165 | + + "&format=json&client_id=" + SoundcloudParsingHelper.clientId(); |
| 166 | + final String response = NewPipe.getDownloader().get(widgetUrl, |
| 167 | + SoundCloud.getLocalization()).responseBody(); |
| 168 | + final JsonObject o = JsonParser.object().from(response); |
| 169 | + return String.valueOf(JsonUtils.getValue(o, "id")); |
| 170 | + } catch (JsonParserException e) { |
| 171 | + throw new ParsingException("Could not parse JSON response", e); |
| 172 | + } catch (ExtractionException e) { |
| 173 | + throw new ParsingException("Could not resolve id with embedded player. ClientId not extracted", e); |
| 174 | + } |
164 | 175 | } |
165 | 176 |
|
166 | 177 | /** |
|
0 commit comments