Skip to content

Commit

Permalink
[SoundCloud] Detect whether there are any more search results
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiGr committed Jul 14, 2023
1 parent 986a3f7 commit 25718ae
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,29 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException,

final Downloader dl = getDownloader();
final JsonArray searchCollection;
final int totalResults;
try {
final String response = dl.get(page.getUrl(), getExtractorLocalization())
.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) {
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),
getNextPageFromCurrentUrl(page.getUrl(),
currentOffset -> currentOffset + ITEMS_PER_PAGE));
}
return new InfoItemsPage<>(collectItems(searchCollection), null);

return new InfoItemsPage<>(collectItems(searchCollection),
getNextPageFromCurrentUrl(page.getUrl(),
currentOffset -> currentOffset + ITEMS_PER_PAGE));
}

@Override
Expand Down Expand Up @@ -134,12 +146,16 @@ private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(
private Page getNextPageFromCurrentUrl(final String currentUrl,
final IntUnaryOperator newPageOffsetCalculator)
throws MalformedURLException, UnsupportedEncodingException {
final int currentPageOffset = Integer.parseInt(
Parser.compatParseMap(new URL(currentUrl).getQuery()).get("offset"));
final int currentPageOffset = getOffsetFromUrl(currentUrl);

return new Page(
currentUrl.replace(
"&offset=" + 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"));
}
}

0 comments on commit 25718ae

Please sign in to comment.