[SoundCloud] Move try-catch inside getOffsetFromUrl

This commit is contained in:
Stypox 2023-08-06 11:35:37 +02:00
parent aa6c17dc77
commit 485bfbca9d
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -92,19 +92,13 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
} 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 { if (getOffsetFromUrl(page.getUrl()) + ITEMS_PER_PAGE <= totalResults) {
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); return new InfoItemsPage<>(collectItems(searchCollection), null);
} }
@Override @Override
@ -153,7 +147,7 @@ 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 ParsingException {
final int currentPageOffset = getOffsetFromUrl(currentUrl); final int currentPageOffset = getOffsetFromUrl(currentUrl);
return new Page( return new Page(
@ -162,8 +156,11 @@ public class SoundcloudSearchExtractor extends SearchExtractor {
"&offset=" + newPageOffsetCalculator.applyAsInt(currentPageOffset))); "&offset=" + newPageOffsetCalculator.applyAsInt(currentPageOffset)));
} }
private int getOffsetFromUrl(final String url) private int getOffsetFromUrl(final String url) throws ParsingException {
throws MalformedURLException, UnsupportedEncodingException { try {
return Integer.parseInt(Parser.compatParseMap(new URL(url).getQuery()).get("offset")); return Integer.parseInt(Parser.compatParseMap(new URL(url).getQuery()).get("offset"));
} catch (MalformedURLException | UnsupportedEncodingException e) {
throw new ParsingException("Could not get offset from page URL", e);
}
} }
} }