Fixed SoundCloud's search(for tests)

Getting the initial page was not returning initial page
This commit is contained in:
litetex 2021-12-27 21:18:39 +01:00
parent a2cbdf0991
commit 4995709871

View file

@ -20,6 +20,8 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Function;
import java.util.function.IntUnaryOperator;
import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.ITEMS_PER_PAGE; import static org.schabi.newpipe.extractor.services.soundcloud.linkHandler.SoundcloudSearchQueryHandlerFactory.ITEMS_PER_PAGE;
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING; import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
@ -53,8 +55,9 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
@Nonnull @Nonnull
@Override @Override
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl( return new InfoItemsPage<>(
getUrl())); collectItems(searchCollection),
getNextPageFromCurrentUrl(getUrl(), currentOffset -> 0));
} }
@Override @Override
@ -73,8 +76,9 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
throw new ParsingException("Could not parse json response", e); throw new ParsingException("Could not parse json response", e);
} }
return new InfoItemsPage<>(collectItems(searchCollection), getNextPageFromCurrentUrl(page return new InfoItemsPage<>(
.getUrl())); collectItems(searchCollection),
getNextPageFromCurrentUrl(page.getUrl(), currentOffset -> currentOffset + ITEMS_PER_PAGE));
} }
@Override @Override
@ -118,12 +122,19 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
return collector; return collector;
} }
private Page getNextPageFromCurrentUrl(final String currentUrl) private Page getNextPageFromCurrentUrl(final String currentUrl, final IntUnaryOperator pageOffsetHandler)
throws MalformedURLException, UnsupportedEncodingException { throws MalformedURLException, UnsupportedEncodingException {
final int pageOffset = Integer.parseInt( int currentPageOffset;
Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset")); try {
currentPageOffset = Integer.parseInt(
Parser.compatParseMap(new URL(currentUrl).getQuery()).getOrDefault("offset", "0"));
} catch (final NumberFormatException ex) {
currentPageOffset = 0;
}
return new Page(currentUrl.replace("&offset=" + pageOffset, "&offset=" return new Page(
+ (pageOffset + ITEMS_PER_PAGE))); currentUrl.replace(
"&offset=" + currentPageOffset,
"&offset=" + pageOffsetHandler.applyAsInt(currentPageOffset)));
} }
} }