[SoundCloud] Detect whether there are any more search results
Add test for this edge case.
This commit is contained in:
parent
5492343b8e
commit
2fb9922a15
|
@ -33,7 +33,9 @@ import java.util.function.IntUnaryOperator;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class SoundcloudSearchExtractor extends SearchExtractor {
|
public class SoundcloudSearchExtractor extends SearchExtractor {
|
||||||
private JsonArray initialSearchCollection;
|
private JsonObject initialSearchObject;
|
||||||
|
private static final String COLLECTION = "collection";
|
||||||
|
private static final String TOTAL_RESULTS = "total_results";
|
||||||
|
|
||||||
public SoundcloudSearchExtractor(final StreamingService service,
|
public SoundcloudSearchExtractor(final StreamingService service,
|
||||||
final SearchQueryHandler linkHandler) {
|
final SearchQueryHandler linkHandler) {
|
||||||
|
@ -60,9 +62,15 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
||||||
|
if (initialSearchObject.getInt(TOTAL_RESULTS) > ITEMS_PER_PAGE) {
|
||||||
return new InfoItemsPage<>(
|
return new InfoItemsPage<>(
|
||||||
collectItems(initialSearchCollection),
|
collectItems(initialSearchObject.getArray(COLLECTION)),
|
||||||
getNextPageFromCurrentUrl(getUrl(), currentOffset -> ITEMS_PER_PAGE));
|
getNextPageFromCurrentUrl(getUrl(), currentOffset -> ITEMS_PER_PAGE));
|
||||||
|
} else {
|
||||||
|
return new InfoItemsPage<>(
|
||||||
|
collectItems(initialSearchObject.getArray(COLLECTION)), null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,18 +82,30 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
|
||||||
|
|
||||||
final Downloader dl = getDownloader();
|
final Downloader dl = getDownloader();
|
||||||
final JsonArray searchCollection;
|
final JsonArray searchCollection;
|
||||||
|
final int totalResults;
|
||||||
try {
|
try {
|
||||||
final String response = dl.get(page.getUrl(), getExtractorLocalization())
|
final String response = dl.get(page.getUrl(), getExtractorLocalization())
|
||||||
.responseBody();
|
.responseBody();
|
||||||
searchCollection = JsonParser.object().from(response).getArray("collection");
|
final JsonObject result = JsonParser.object().from(response);
|
||||||
|
searchCollection = result.getArray(COLLECTION);
|
||||||
|
totalResults = result.getInt(TOTAL_RESULTS);
|
||||||
} 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);
|
||||||
}
|
}
|
||||||
|
final boolean hasNextPage;
|
||||||
|
try {
|
||||||
|
hasNextPage = getOffsetFromUrl(page.getUrl()) + ITEMS_PER_PAGE <= totalResults;
|
||||||
|
} catch (MalformedURLException | UnsupportedEncodingException e) {
|
||||||
|
throw new ParsingException("Could not get offset from page URL", e);
|
||||||
|
}
|
||||||
|
if (hasNextPage) {
|
||||||
return new InfoItemsPage<>(collectItems(searchCollection),
|
return new InfoItemsPage<>(collectItems(searchCollection),
|
||||||
getNextPageFromCurrentUrl(page.getUrl(),
|
getNextPageFromCurrentUrl(page.getUrl(),
|
||||||
currentOffset -> currentOffset + ITEMS_PER_PAGE));
|
currentOffset -> currentOffset + ITEMS_PER_PAGE));
|
||||||
}
|
}
|
||||||
|
return new InfoItemsPage<>(collectItems(searchCollection), null);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException,
|
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException,
|
||||||
|
@ -94,12 +114,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();
|
||||||
initialSearchCollection = JsonParser.object().from(response).getArray("collection");
|
initialSearchObject = JsonParser.object().from(response);
|
||||||
} 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 (initialSearchCollection.isEmpty()) {
|
if (initialSearchObject.getArray(COLLECTION).isEmpty()) {
|
||||||
throw new SearchExtractor.NothingFoundException("Nothing found");
|
throw new SearchExtractor.NothingFoundException("Nothing found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,12 +154,16 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
|
||||||
private Page getNextPageFromCurrentUrl(final String currentUrl,
|
private Page getNextPageFromCurrentUrl(final String currentUrl,
|
||||||
final IntUnaryOperator newPageOffsetCalculator)
|
final IntUnaryOperator newPageOffsetCalculator)
|
||||||
throws MalformedURLException, UnsupportedEncodingException {
|
throws MalformedURLException, UnsupportedEncodingException {
|
||||||
final int currentPageOffset = Integer.parseInt(
|
final int currentPageOffset = getOffsetFromUrl(currentUrl);
|
||||||
Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset"));
|
|
||||||
|
|
||||||
return new Page(
|
return new Page(
|
||||||
currentUrl.replace(
|
currentUrl.replace(
|
||||||
"&offset=" + currentPageOffset,
|
"&offset=" + currentPageOffset,
|
||||||
"&offset=" + newPageOffsetCalculator.applyAsInt(currentPageOffset)));
|
"&offset=" + newPageOffsetCalculator.applyAsInt(currentPageOffset)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getOffsetFromUrl(final String url)
|
||||||
|
throws MalformedURLException, UnsupportedEncodingException {
|
||||||
|
return Integer.parseInt(Parser.compatParseMap(new URL(url).getQuery()).get("offset"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.schabi.newpipe.extractor.services.soundcloud.search;
|
package org.schabi.newpipe.extractor.services.soundcloud.search;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
||||||
import static org.schabi.newpipe.extractor.services.DefaultTests.assertNoDuplicatedItems;
|
import static org.schabi.newpipe.extractor.services.DefaultTests.assertNoDuplicatedItems;
|
||||||
|
@ -181,4 +182,27 @@ public class SoundcloudSearchExtractorTest {
|
||||||
assertTrue(verified);
|
assertTrue(verified);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class NoNextPage extends DefaultSearchExtractorTest {
|
||||||
|
|
||||||
|
private static SearchExtractor extractor;
|
||||||
|
private static final String QUERY = "Dan at hor#berlgbd";
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void setUp() throws Exception {
|
||||||
|
NewPipe.init(DownloaderTestImpl.getInstance());
|
||||||
|
extractor = SoundCloud.getSearchExtractor(QUERY);
|
||||||
|
extractor.fetchPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public boolean expectedHasMoreItems() { return false; }
|
||||||
|
@Override public SearchExtractor extractor() throws Exception { return extractor; }
|
||||||
|
@Override public StreamingService expectedService() throws Exception { return SoundCloud; }
|
||||||
|
@Override public String expectedName() throws Exception { return QUERY; }
|
||||||
|
@Override public String expectedId() throws Exception { return QUERY; }
|
||||||
|
@Override public String expectedUrlContains() { return "soundcloud.com/search?q=" + urlEncode(QUERY); }
|
||||||
|
@Override public String expectedOriginalUrlContains() { return "soundcloud.com/search?q=" + urlEncode(QUERY); }
|
||||||
|
@Override public String expectedSearchString() { return QUERY; }
|
||||||
|
@Nullable @Override public String expectedSearchSuggestion() { return null; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue