Skip to content

Commit 4995709

Browse files
committed
Fixed SoundCloud's search(for tests)
Getting the initial page was not returning initial page
1 parent a2cbdf0 commit 4995709

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.net.URL;
2121
import java.util.Collections;
2222
import java.util.List;
23+
import java.util.function.Function;
24+
import java.util.function.IntUnaryOperator;
2325

2426
import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.ITEMS_PER_PAGE;
2527
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
@@ -53,8 +55,9 @@ public List<MetaInfo> getMetaInfo() {
5355
@Nonnull
5456
@Override
5557
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
56-
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(
57-
getUrl()));
58+
return new InfoItemsPage<>(
59+
collectItems(searchCollection),
60+
getNextPageFromCurrentUrl(getUrl(), currentOffset -> 0));
5861
}
5962

6063
@Override
@@ -73,8 +76,9 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException,
7376
throw new ParsingException("Could not parse json response", e);
7477
}
7578

76-
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(page
77-
.getUrl()));
79+
return new InfoItemsPage<>(
80+
collectItems(searchCollection),
81+
getNextPageFromCurrentUrl(page.getUrl(), currentOffset -> currentOffset + ITEMS_PER_PAGE));
7882
}
7983

8084
@Override
@@ -118,12 +122,19 @@ private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(
118122
return collector;
119123
}
120124

121-
private Page getNextPageFromCurrentUrl(final String currentUrl)
125+
private Page getNextPageFromCurrentUrl(final String currentUrl, final IntUnaryOperator pageOffsetHandler)
122126
throws MalformedURLException, UnsupportedEncodingException {
123-
final int pageOffset = Integer.parseInt(
124-
Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset"));
127+
int currentPageOffset;
128+
try {
129+
currentPageOffset = Integer.parseInt(
130+
Parser.compatParseMap(new URL(currentUrl).getQuery()).getOrDefault("offset", "0"));
131+
} catch (final NumberFormatException ex) {
132+
currentPageOffset = 0;
133+
}
125134

126-
return new Page(currentUrl.replace("&offset=" + pageOffset, "&offset="
127-
+ (pageOffset + ITEMS_PER_PAGE)));
135+
return new Page(
136+
currentUrl.replace(
137+
"&offset=" + currentPageOffset,
138+
"&offset=" + pageOffsetHandler.applyAsInt(currentPageOffset)));
128139
}
129140
}

0 commit comments

Comments
 (0)