2020import java .net .URL ;
2121import java .util .Collections ;
2222import java .util .List ;
23- import java .util .function .Function ;
2423import java .util .function .IntUnaryOperator ;
2524
2625import static org .schabi .newpipe .extractor .services .soundcloud .linkHandler .SoundcloudSearchQueryHandlerFactory .ITEMS_PER_PAGE ;
2726import static org .schabi .newpipe .extractor .utils .Utils .EMPTY_STRING ;
2827import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
2928
3029public class SoundcloudSearchExtractor extends SearchExtractor {
31- private JsonArray searchCollection ;
30+ private JsonArray initialSearchCollection ;
3231
3332 public SoundcloudSearchExtractor (final StreamingService service ,
3433 final SearchQueryHandler linkHandler ) {
@@ -56,8 +55,8 @@ public List<MetaInfo> getMetaInfo() {
5655 @ Override
5756 public InfoItemsPage <InfoItem > getInitialPage () throws IOException , ExtractionException {
5857 return new InfoItemsPage <>(
59- collectItems (searchCollection ),
60- getNextPageFromCurrentUrl (getUrl (), currentOffset -> 0 ));
58+ collectItems (initialSearchCollection ),
59+ getNextPageFromCurrentUrl (getUrl (), currentOffset -> ITEMS_PER_PAGE ));
6160 }
6261
6362 @ Override
@@ -68,6 +67,7 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException,
6867 }
6968
7069 final Downloader dl = getDownloader ();
70+ final JsonArray searchCollection ;
7171 try {
7272 final String response = dl .get (page .getUrl (), getExtractorLocalization ())
7373 .responseBody ();
@@ -88,12 +88,12 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
8888 final String url = getUrl ();
8989 try {
9090 final String response = dl .get (url , getExtractorLocalization ()).responseBody ();
91- searchCollection = JsonParser .object ().from (response ).getArray ("collection" );
91+ initialSearchCollection = JsonParser .object ().from (response ).getArray ("collection" );
9292 } catch (final JsonParserException e ) {
9393 throw new ParsingException ("Could not parse json response" , e );
9494 }
9595
96- if (searchCollection .isEmpty ()) {
96+ if (initialSearchCollection .isEmpty ()) {
9797 throw new SearchExtractor .NothingFoundException ("Nothing found" );
9898 }
9999 }
@@ -122,19 +122,14 @@ private InfoItemsCollector<InfoItem, InfoItemExtractor> collectItems(
122122 return collector ;
123123 }
124124
125- private Page getNextPageFromCurrentUrl (final String currentUrl , final IntUnaryOperator pageOffsetHandler )
125+ private Page getNextPageFromCurrentUrl (final String currentUrl , final IntUnaryOperator newPageOffsetCalculator )
126126 throws MalformedURLException , UnsupportedEncodingException {
127- int currentPageOffset ;
128- try {
129- currentPageOffset = Integer .parseInt (
130- Parser .compatParseMap (new URL (currentUrl ).getQuery ()).getOrDefault ("offset" , "0" ));
131- } catch (final NumberFormatException ex ) {
132- currentPageOffset = 0 ;
133- }
127+ final int currentPageOffset = Integer .parseInt (
128+ Parser .compatParseMap (new URL (currentUrl ).getQuery ()).get ("offset" ));
134129
135130 return new Page (
136131 currentUrl .replace (
137132 "&offset=" + currentPageOffset ,
138- "&offset=" + pageOffsetHandler .applyAsInt (currentPageOffset )));
133+ "&offset=" + newPageOffsetCalculator .applyAsInt (currentPageOffset )));
139134 }
140135}
0 commit comments