55import static org .schabi .newpipe .extractor .ServiceList .SoundCloud ;
66import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
77import static org .schabi .newpipe .extractor .utils .Utils .replaceHttpWithHttps ;
8+ import static org .schabi .newpipe .extractor .utils .HttpUtils .validateResponseCode ;
89
910import com .grack .nanojson .JsonArray ;
1011import com .grack .nanojson .JsonObject ;
1112import com .grack .nanojson .JsonParser ;
1213import com .grack .nanojson .JsonParserException ;
1314import org .jsoup .Jsoup ;
1415import org .jsoup .nodes .Document ;
15- import org .jsoup .nodes .Element ;
1616import org .jsoup .select .Elements ;
1717import org .schabi .newpipe .extractor .MultiInfoItemsCollector ;
1818import org .schabi .newpipe .extractor .Image ;
@@ -105,8 +105,8 @@ public static synchronized String clientId() throws ExtractionException, IOExcep
105105
106106 final Downloader dl = NewPipe .getDownloader ();
107107
108- final Response download = dl .get ("https://soundcloud.com" );
109- final String responseBody = download .responseBody ();
108+ final Response downloadResponse = dl .get ("https://soundcloud.com" ). validateResponseCode ( );
109+ final String responseBody = downloadResponse .responseBody ();
110110 final String clientIdPattern = ",client_id:\" (.*?)\" " ;
111111
112112 final Document doc = Jsoup .parse (responseBody );
@@ -117,11 +117,12 @@ public static synchronized String clientId() throws ExtractionException, IOExcep
117117
118118 final var headers = Map .of ("Range" , List .of ("bytes=0-50000" ));
119119
120- for (final Element element : possibleScripts ) {
120+ for (final var element : possibleScripts ) {
121121 final String srcUrl = element .attr ("src" );
122122 if (!isNullOrEmpty (srcUrl )) {
123123 try {
124124 clientId = Parser .matchGroup1 (clientIdPattern , dl .get (srcUrl , headers )
125+ .validateResponseCode ()
125126 .responseBody ());
126127 return clientId ;
127128 } catch (final RegexException ignored ) {
@@ -149,11 +150,13 @@ public static DateWrapper parseDate(final String uploadDate) throws ParsingExcep
149150 }
150151 }
151152
153+ // CHECKSTYLE:OFF
152154 /**
153- * Call the endpoint "/resolve" of the API.<p>
155+ * Call the endpoint "/resolve" of the API.
154156 * <p>
155- * See https://developers.soundcloud.com/docs/api/reference#resolve
157+ * See https://web.archive.org/web/20170804051146/https:// developers.soundcloud.com/docs/api/reference#resolve
156158 */
159+ // CHECKSTYLE:ON
157160 public static JsonObject resolveFor (@ Nonnull final Downloader downloader , final String url )
158161 throws IOException , ExtractionException {
159162 final String apiUrl = SOUNDCLOUD_API_V2_URL + "resolve"
@@ -178,10 +181,11 @@ public static JsonObject resolveFor(@Nonnull final Downloader downloader, final
178181 public static String resolveUrlWithEmbedPlayer (final String apiUrl ) throws IOException ,
179182 ReCaptchaException {
180183
181- final String response = NewPipe .getDownloader ().get ("https://w.soundcloud.com/player/?url="
182- + Utils .encodeUrlUtf8 (apiUrl ), SoundCloud .getLocalization ()).responseBody ();
183-
184- return Jsoup .parse (response ).select ("link[rel=\" canonical\" ]" ).first ()
184+ final var response = NewPipe .getDownloader ().get ("https://w.soundcloud.com/player/?url="
185+ + Utils .encodeUrlUtf8 (apiUrl ), SoundCloud .getLocalization ());
186+ validateResponseCode (response );
187+ final var responseBody = response .responseBody ();
188+ return Jsoup .parse (responseBody ).select ("link[rel=\" canonical\" ]" ).first ()
185189 .attr ("abs:href" );
186190 }
187191
@@ -190,6 +194,7 @@ public static String resolveUrlWithEmbedPlayer(final String apiUrl) throws IOExc
190194 *
191195 * @return the resolved id
192196 */
197+ // TODO: what makes this method different from the others? Don' they all return the same?
193198 public static String resolveIdWithWidgetApi (final String urlString ) throws IOException ,
194199 ParsingException {
195200 String fixedUrl = urlString ;
@@ -225,9 +230,12 @@ public static String resolveIdWithWidgetApi(final String urlString) throws IOExc
225230 final String widgetUrl = "https://api-widget.soundcloud.com/resolve?url="
226231 + Utils .encodeUrlUtf8 (url .toString ())
227232 + "&format=json&client_id=" + SoundcloudParsingHelper .clientId ();
228- final String response = NewPipe .getDownloader ().get (widgetUrl ,
229- SoundCloud .getLocalization ()).responseBody ();
230- final JsonObject o = JsonParser .object ().from (response );
233+
234+ final var response = NewPipe .getDownloader ().get (widgetUrl ,
235+ SoundCloud .getLocalization ());
236+
237+ final var responseBody = response .validateResponseCode ().responseBody ();
238+ final JsonObject o = JsonParser .object ().from (responseBody );
231239 return String .valueOf (JsonUtils .getValue (o , "id" ));
232240 } catch (final JsonParserException e ) {
233241 throw new ParsingException ("Could not parse JSON response" , e );
0 commit comments