Fixed SoundCloud's search(for tests)

This commit is contained in:
litetex 2021-12-27 21:42:43 +01:00
parent 4995709871
commit 4291a90251

View file

@ -20,7 +20,6 @@ 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 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;
@ -28,7 +27,7 @@ import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class SoundcloudSearchExtractor extends SearchExtractor { public class SoundcloudSearchExtractor extends SearchExtractor {
private JsonArray searchCollection; private JsonArray initialSearchCollection;
public SoundcloudSearchExtractor(final StreamingService service, public SoundcloudSearchExtractor(final StreamingService service,
final SearchQueryHandler linkHandler) { final SearchQueryHandler linkHandler) {
@ -56,8 +55,8 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
@Override @Override
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
return new InfoItemsPage<>( return new InfoItemsPage<>(
collectItems(searchCollection), collectItems(initialSearchCollection),
getNextPageFromCurrentUrl(getUrl(), currentOffset -> 0)); getNextPageFromCurrentUrl(getUrl(), currentOffset -> ITEMS_PER_PAGE));
} }
@Override @Override
@ -68,6 +67,7 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
} }
final Downloader dl = getDownloader(); final Downloader dl = getDownloader();
final JsonArray searchCollection;
try { try {
final String response = dl.get(page.getUrl(), getExtractorLocalization()) final String response = dl.get(page.getUrl(), getExtractorLocalization())
.responseBody(); .responseBody();
@ -88,12 +88,12 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
final String url = getUrl(); final String url = getUrl();
try { try {
final String response = dl.get(url, getExtractorLocalization()).responseBody(); final String response = dl.get(url, getExtractorLocalization()).responseBody();
searchCollection = JsonParser.object().from(response).getArray("collection"); initialSearchCollection = JsonParser.object().from(response).getArray("collection");
} catch (final JsonParserException e) { } catch (final JsonParserException e) {
throw new ParsingException("Could not parse json response", e); throw new ParsingException("Could not parse json response", e);
} }
if (searchCollection.isEmpty()) { if (initialSearchCollection.isEmpty()) {
throw new SearchExtractor.NothingFoundException("Nothing found"); throw new SearchExtractor.NothingFoundException("Nothing found");
} }
} }
@ -122,19 +122,14 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
return collector; return collector;
} }
private Page getNextPageFromCurrentUrl(final String currentUrl, final IntUnaryOperator pageOffsetHandler) private Page getNextPageFromCurrentUrl(final String currentUrl, final IntUnaryOperator newPageOffsetCalculator)
throws MalformedURLException, UnsupportedEncodingException { throws MalformedURLException, UnsupportedEncodingException {
int currentPageOffset; final int currentPageOffset = Integer.parseInt(
try { Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset"));
currentPageOffset = Integer.parseInt(
Parser.compatParseMap(new URL(currentUrl).getQuery()).getOrDefault("offset", "0"));
} catch (final NumberFormatException ex) {
currentPageOffset = 0;
}
return new Page( return new Page(
currentUrl.replace( currentUrl.replace(
"&offset=" + currentPageOffset, "&offset=" + currentPageOffset,
"&offset=" + pageOffsetHandler.applyAsInt(currentPageOffset))); "&offset=" + newPageOffsetCalculator.applyAsInt(currentPageOffset)));
} }
} }