Skip to content

Commit 8d89c82

Browse files
committed
Soundcloud: fix charts extraction when georestricted
1 parent 8d7b629 commit 8d89c82

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ public static String resolveUrlWithEmbedPlayer(String apiUrl) throws IOException
148148
*/
149149
public static String resolveIdWithEmbedPlayer(String urlString) throws IOException, ReCaptchaException, ParsingException {
150150
// Remove the tailing slash from URLs due to issues with the SoundCloud API
151-
if (urlString.charAt(urlString.length() -1) == '/') urlString = urlString.substring(0, urlString.length()-1);
151+
if (urlString.charAt(urlString.length() - 1) == '/') urlString = urlString.substring(0, urlString.length() - 1);
152152

153153
URL url;
154154
try {
155155
url = Utils.stringToURL(urlString);
156-
} catch (MalformedURLException e){
156+
} catch (MalformedURLException e) {
157157
throw new IllegalArgumentException("The given URL is not valid");
158158
}
159159

@@ -240,10 +240,14 @@ public static String getStreamsFromApiMinItems(int minItems, StreamInfoItemsColl
240240
* @return the next streams url, empty if don't have
241241
*/
242242
public static String getStreamsFromApi(StreamInfoItemsCollector collector, String apiUrl, boolean charts) throws IOException, ReCaptchaException, ParsingException {
243-
String response = NewPipe.getDownloader().get(apiUrl, SoundCloud.getLocalization()).responseBody();
243+
final Response response = NewPipe.getDownloader().get(apiUrl, SoundCloud.getLocalization());
244+
if (response.responseCode() >= 400) {
245+
throw new IOException("Could not get streams from API, HTTP " + response.responseCode());
246+
}
247+
244248
JsonObject responseObject;
245249
try {
246-
responseObject = JsonParser.object().from(response);
250+
responseObject = JsonParser.object().from(response.responseBody());
247251
} catch (JsonParserException e) {
248252
throw new ParsingException("Could not parse json response", e);
249253
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
77
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
88
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
9+
import org.schabi.newpipe.extractor.localization.ContentCountry;
910
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
1011
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1112
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
1213

13-
import java.io.IOException;
14-
1514
import javax.annotation.Nonnull;
15+
import java.io.IOException;
1616

1717
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
1818
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
@@ -61,10 +61,20 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
6161
apiUrl += "&kind=trending";
6262
}
6363

64-
final String contentCountry = SoundCloud.getContentCountry().getCountryCode();
65-
apiUrl += "&region=soundcloud:regions:" + contentCountry;
64+
final ContentCountry contentCountry = SoundCloud.getContentCountry();
65+
String apiUrlWithRegion = null;
66+
if (getService().getSupportedCountries().contains(contentCountry)) {
67+
apiUrlWithRegion = apiUrl + "&region=soundcloud:regions:" + contentCountry.getCountryCode();
68+
}
6669

67-
final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl, true);
70+
String nextPageUrl;
71+
try {
72+
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrlWithRegion == null ? apiUrl : apiUrlWithRegion, true);
73+
} catch (IOException e) {
74+
// Request to other region may be geo-restricted. See https://github.com/TeamNewPipe/NewPipeExtractor/issues/537
75+
// we retry without the specified region.
76+
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl, true);
77+
}
6878

6979
return new InfoItemsPage<>(collector, new Page(nextPageUrl));
7080
}

0 commit comments

Comments
 (0)