Skip to content

Commit d42e381

Browse files
committed
[Soundcloud] Charts: Fetch correctly so that tests work
1 parent 1f8fc57 commit d42e381

6 files changed

Lines changed: 142 additions & 116 deletions

File tree

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.schabi.newpipe.extractor.services.soundcloud.extractors;
22

3+
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
4+
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
5+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
6+
37
import org.schabi.newpipe.extractor.Page;
48
import org.schabi.newpipe.extractor.StreamingService;
59
import org.schabi.newpipe.extractor.downloader.Downloader;
@@ -11,51 +15,33 @@
1115
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1216
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
1317

14-
import javax.annotation.Nonnull;
1518
import java.io.IOException;
1619

17-
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
18-
import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.SOUNDCLOUD_API_V2_URL;
19-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
20+
import javax.annotation.Nonnull;
2021

2122
public class SoundcloudChartsExtractor extends KioskExtractor<StreamInfoItem> {
23+
24+
private String initialFetchNextPageUrl;
25+
private StreamInfoItemsCollector initialFetchCollector;
26+
2227
public SoundcloudChartsExtractor(final StreamingService service,
2328
final ListLinkHandler linkHandler,
2429
final String kioskId) {
2530
super(service, linkHandler, kioskId);
2631
}
2732

2833
@Override
29-
public void onFetchPage(@Nonnull final Downloader downloader) {
30-
}
31-
32-
@Nonnull
33-
@Override
34-
public String getName() {
35-
return getId();
36-
}
37-
38-
@Override
39-
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException,
40-
ExtractionException {
41-
if (page == null || isNullOrEmpty(page.getUrl())) {
42-
throw new IllegalArgumentException("Page doesn't contain an URL");
34+
public void onFetchPage(@Nonnull final Downloader downloader)
35+
throws ExtractionException, IOException {
36+
// Check if already run
37+
if (initialFetchNextPageUrl != null) {
38+
return;
4339
}
4440

45-
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
46-
final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector,
47-
page.getUrl(), true);
48-
49-
return new InfoItemsPage<>(collector, new Page(nextPageUrl));
50-
}
51-
52-
@Nonnull
53-
@Override
54-
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
55-
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
41+
initialFetchCollector = new StreamInfoItemsCollector(getServiceId());
5642

5743
String apiUrl = SOUNDCLOUD_API_V2_URL + "charts" + "?genre=soundcloud:genres:all-music"
58-
+ "&client_id=" + SoundcloudParsingHelper.clientId();
44+
+ "&client_id=" + SoundcloudParsingHelper.clientId();
5945

6046
if (getId().equals("Top 50")) {
6147
apiUrl += "&kind=top";
@@ -67,20 +53,45 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
6753
String apiUrlWithRegion = null;
6854
if (getService().getSupportedCountries().contains(contentCountry)) {
6955
apiUrlWithRegion = apiUrl + "&region=soundcloud:regions:"
70-
+ contentCountry.getCountryCode();
56+
+ contentCountry.getCountryCode();
7157
}
7258

73-
String nextPageUrl;
7459
try {
75-
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector,
76-
apiUrlWithRegion == null ? apiUrl : apiUrlWithRegion, true);
60+
initialFetchNextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(
61+
initialFetchCollector,
62+
apiUrlWithRegion == null ? apiUrl : apiUrlWithRegion, true);
7763
} catch (final IOException e) {
7864
// Request to other region may be geo-restricted.
7965
// See https://github.com/TeamNewPipe/NewPipeExtractor/issues/537.
8066
// We retry without the specified region.
81-
nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl, true);
67+
initialFetchNextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(
68+
initialFetchCollector, apiUrl, true);
8269
}
70+
}
71+
72+
@Nonnull
73+
@Override
74+
public String getName() {
75+
return getId();
76+
}
77+
78+
@Override
79+
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException,
80+
ExtractionException {
81+
if (page == null || isNullOrEmpty(page.getUrl())) {
82+
throw new IllegalArgumentException("Page doesn't contain an URL");
83+
}
84+
85+
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
86+
final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector,
87+
page.getUrl(), true);
8388

8489
return new InfoItemsPage<>(collector, new Page(nextPageUrl));
8590
}
91+
92+
@Nonnull
93+
@Override
94+
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
95+
return new InfoItemsPage<>(initialFetchCollector, new Page(initialFetchNextPageUrl));
96+
}
8697
}

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/soundcloud/soundcloudchartsextractor/newandhot/generated_mock_0.json

Lines changed: 26 additions & 26 deletions
Large diffs are not rendered by default.

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/soundcloud/soundcloudchartsextractor/newandhot/generated_mock_1.json

Lines changed: 46 additions & 31 deletions
Large diffs are not rendered by default.

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/soundcloud/soundcloudchartsextractor/newandhot/generated_mock_2.json

Lines changed: 9 additions & 9 deletions
Large diffs are not rendered by default.

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/soundcloud/soundcloudchartsextractor/newandhot/generated_mock_3.json

Lines changed: 9 additions & 9 deletions
Large diffs are not rendered by default.

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/soundcloud/soundcloudchartsextractor/newandhot/generated_mock_4.json

Lines changed: 6 additions & 6 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)