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 ;
@@ -103,8 +103,8 @@ public static synchronized String clientId() throws ExtractionException, IOExcep
103103
104104 final Downloader dl = NewPipe .getDownloader ();
105105
106- final Response download = dl .get ("https://soundcloud.com" );
107- final String responseBody = download .responseBody ();
106+ final Response downloadResponse = dl .get ("https://soundcloud.com" ). validateResponseCode ( );
107+ final String responseBody = downloadResponse .responseBody ();
108108 final String clientIdPattern = ",client_id:\" (.*?)\" " ;
109109
110110 final Document doc = Jsoup .parse (responseBody );
@@ -115,11 +115,12 @@ public static synchronized String clientId() throws ExtractionException, IOExcep
115115
116116 final var headers = Map .of ("Range" , List .of ("bytes=0-50000" ));
117117
118- for (final Element element : possibleScripts ) {
118+ for (final var element : possibleScripts ) {
119119 final String srcUrl = element .attr ("src" );
120120 if (!isNullOrEmpty (srcUrl )) {
121121 try {
122122 clientId = Parser .matchGroup1 (clientIdPattern , dl .get (srcUrl , headers )
123+ .validateResponseCode ()
123124 .responseBody ());
124125 return clientId ;
125126 } catch (final RegexException ignored ) {
@@ -147,11 +148,13 @@ public static OffsetDateTime parseDateFrom(final String textualUploadDate)
147148 }
148149 }
149150
151+ // CHECKSTYLE:OFF
150152 /**
151- * Call the endpoint "/resolve" of the API.<p>
153+ * Call the endpoint "/resolve" of the API.
152154 * <p>
153- * See https://developers.soundcloud.com/docs/api/reference#resolve
155+ * See https://web.archive.org/web/20170804051146/https:// developers.soundcloud.com/docs/api/reference#resolve
154156 */
157+ // CHECKSTYLE:ON
155158 public static JsonObject resolveFor (@ Nonnull final Downloader downloader , final String url )
156159 throws IOException , ExtractionException {
157160 final String apiUrl = SOUNDCLOUD_API_V2_URL + "resolve"
@@ -176,10 +179,11 @@ public static JsonObject resolveFor(@Nonnull final Downloader downloader, final
176179 public static String resolveUrlWithEmbedPlayer (final String apiUrl ) throws IOException ,
177180 ReCaptchaException {
178181
179- final String response = NewPipe .getDownloader ().get ("https://w.soundcloud.com/player/?url="
180- + Utils .encodeUrlUtf8 (apiUrl ), SoundCloud .getLocalization ()).responseBody ();
181-
182- return Jsoup .parse (response ).select ("link[rel=\" canonical\" ]" ).first ()
182+ final var response = NewPipe .getDownloader ().get ("https://w.soundcloud.com/player/?url="
183+ + Utils .encodeUrlUtf8 (apiUrl ), SoundCloud .getLocalization ());
184+ validateResponseCode (response );
185+ final var responseBody = response .responseBody ();
186+ return Jsoup .parse (responseBody ).select ("link[rel=\" canonical\" ]" ).first ()
183187 .attr ("abs:href" );
184188 }
185189
@@ -188,6 +192,7 @@ public static String resolveUrlWithEmbedPlayer(final String apiUrl) throws IOExc
188192 *
189193 * @return the resolved id
190194 */
195+ // TODO: what makes this method different from the others? Don' they all return the same?
191196 public static String resolveIdWithWidgetApi (final String urlString ) throws IOException ,
192197 ParsingException {
193198 String fixedUrl = urlString ;
@@ -223,9 +228,12 @@ public static String resolveIdWithWidgetApi(final String urlString) throws IOExc
223228 final String widgetUrl = "https://api-widget.soundcloud.com/resolve?url="
224229 + Utils .encodeUrlUtf8 (url .toString ())
225230 + "&format=json&client_id=" + SoundcloudParsingHelper .clientId ();
226- final String response = NewPipe .getDownloader ().get (widgetUrl ,
227- SoundCloud .getLocalization ()).responseBody ();
228- final JsonObject o = JsonParser .object ().from (response );
231+
232+ final var response = NewPipe .getDownloader ().get (widgetUrl ,
233+ SoundCloud .getLocalization ());
234+
235+ final var responseBody = response .validateResponseCode ().responseBody ();
236+ final JsonObject o = JsonParser .object ().from (responseBody );
229237 return String .valueOf (JsonUtils .getValue (o , "id" ));
230238 } catch (final JsonParserException e ) {
231239 throw new ParsingException ("Could not parse JSON response" , e );
0 commit comments