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 ;
@@ -104,8 +104,8 @@ public static synchronized String clientId() throws ExtractionException, IOExcep
104104
105105 final Downloader dl = NewPipe .getDownloader ();
106106
107- final Response download = dl .get ("https://soundcloud.com" );
108- final String responseBody = download .responseBody ();
107+ final Response downloadResponse = dl .get ("https://soundcloud.com" ). validateResponseCode ( );
108+ final String responseBody = downloadResponse .responseBody ();
109109 final String clientIdPattern = ",client_id:\" (.*?)\" " ;
110110
111111 final Document doc = Jsoup .parse (responseBody );
@@ -116,11 +116,12 @@ public static synchronized String clientId() throws ExtractionException, IOExcep
116116
117117 final var headers = Map .of ("Range" , List .of ("bytes=0-50000" ));
118118
119- for (final Element element : possibleScripts ) {
119+ for (final var element : possibleScripts ) {
120120 final String srcUrl = element .attr ("src" );
121121 if (!isNullOrEmpty (srcUrl )) {
122122 try {
123123 clientId = Parser .matchGroup1 (clientIdPattern , dl .get (srcUrl , headers )
124+ .validateResponseCode ()
124125 .responseBody ());
125126 return clientId ;
126127 } catch (final RegexException ignored ) {
@@ -148,11 +149,13 @@ public static OffsetDateTime parseDateFrom(final String textualUploadDate)
148149 }
149150 }
150151
152+ // CHECKSTYLE:OFF
151153 /**
152- * Call the endpoint "/resolve" of the API.<p>
154+ * Call the endpoint "/resolve" of the API.
153155 * <p>
154- * See https://developers.soundcloud.com/docs/api/reference#resolve
156+ * See https://web.archive.org/web/20170804051146/https:// developers.soundcloud.com/docs/api/reference#resolve
155157 */
158+ // CHECKSTYLE:ON
156159 public static JsonObject resolveFor (@ Nonnull final Downloader downloader , final String url )
157160 throws IOException , ExtractionException {
158161 final String apiUrl = SOUNDCLOUD_API_V2_URL + "resolve"
@@ -177,10 +180,11 @@ public static JsonObject resolveFor(@Nonnull final Downloader downloader, final
177180 public static String resolveUrlWithEmbedPlayer (final String apiUrl ) throws IOException ,
178181 ReCaptchaException {
179182
180- final String response = NewPipe .getDownloader ().get ("https://w.soundcloud.com/player/?url="
181- + Utils .encodeUrlUtf8 (apiUrl ), SoundCloud .getLocalization ()).responseBody ();
182-
183- return Jsoup .parse (response ).select ("link[rel=\" canonical\" ]" ).first ()
183+ final var response = NewPipe .getDownloader ().get ("https://w.soundcloud.com/player/?url="
184+ + Utils .encodeUrlUtf8 (apiUrl ), SoundCloud .getLocalization ());
185+ validateResponseCode (response );
186+ final var responseBody = response .responseBody ();
187+ return Jsoup .parse (responseBody ).select ("link[rel=\" canonical\" ]" ).first ()
184188 .attr ("abs:href" );
185189 }
186190
@@ -189,6 +193,7 @@ public static String resolveUrlWithEmbedPlayer(final String apiUrl) throws IOExc
189193 *
190194 * @return the resolved id
191195 */
196+ // TODO: what makes this method different from the others? Don' they all return the same?
192197 public static String resolveIdWithWidgetApi (final String urlString ) throws IOException ,
193198 ParsingException {
194199 String fixedUrl = urlString ;
@@ -224,9 +229,12 @@ public static String resolveIdWithWidgetApi(final String urlString) throws IOExc
224229 final String widgetUrl = "https://api-widget.soundcloud.com/resolve?url="
225230 + Utils .encodeUrlUtf8 (url .toString ())
226231 + "&format=json&client_id=" + SoundcloudParsingHelper .clientId ();
227- final String response = NewPipe .getDownloader ().get (widgetUrl ,
228- SoundCloud .getLocalization ()).responseBody ();
229- final JsonObject o = JsonParser .object ().from (response );
232+
233+ final var response = NewPipe .getDownloader ().get (widgetUrl ,
234+ SoundCloud .getLocalization ());
235+
236+ final var responseBody = response .validateResponseCode ().responseBody ();
237+ final JsonObject o = JsonParser .object ().from (responseBody );
230238 return String .valueOf (JsonUtils .getValue (o , "id" ));
231239 } catch (final JsonParserException e ) {
232240 throw new ParsingException ("Could not parse JSON response" , e );
0 commit comments