Skip to content

Commit 37f2e5c

Browse files
committed
Make some methods return the specific InfoItem type
- Some methods were returning a broader range of InfoItem types than it should be. For example, a ChannelInfo should return a List containing only StreamInfoItem, instead of the more general InfoItem. - Renamed and changed return type of ListExtractor.getInfoItems to getInitialPage returning a InfoItemsPage, to be consistent with getPage(url)
1 parent 5dd2daa commit 37f2e5c

19 files changed

Lines changed: 159 additions & 151 deletions

src/main/java/org/schabi/newpipe/extractor/ListExtractor.java

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,45 @@
44

55
import javax.annotation.Nonnull;
66
import java.io.IOException;
7+
import java.util.Collections;
78
import java.util.List;
89

910
/**
1011
* Base class to extractors that have a list (e.g. playlists, users).
1112
*/
12-
public abstract class ListExtractor extends Extractor {
13+
public abstract class ListExtractor<R extends InfoItem> extends Extractor {
1314

1415
public ListExtractor(StreamingService service, String url) {
1516
super(service, url);
1617
}
1718

19+
/**
20+
* A {@link InfoItemsPage InfoItemsPage} corresponding to the initial page where the items are from the initial request and
21+
* the nextPageUrl relative to it.
22+
*
23+
* @return a {@link InfoItemsPage} corresponding to the initial page
24+
*/
1825
@Nonnull
19-
public abstract InfoItemsCollector<? extends InfoItem, ?> getInfoItems() throws IOException, ExtractionException;
26+
public abstract InfoItemsPage<R> getInitialPage() throws IOException, ExtractionException;
27+
28+
/**
29+
* Returns an url that can be used to get the next page relative to the initial one.<br/>
30+
* <p>Usually, these links will only work in the implementation itself.</p>
31+
*
32+
* @return an url pointing to the next page relative to the initial page
33+
* @see #getPage(String)
34+
*/
2035
public abstract String getNextPageUrl() throws IOException, ExtractionException;
21-
public abstract InfoItemPage<? extends InfoItem> getPage(final String nextPageUrl) throws IOException, ExtractionException;
36+
37+
/**
38+
* Get a list of items corresponding to the specific requested page.
39+
*
40+
* @param nextPageUrl any next page url got from the exclusive implementation of the list extractor
41+
* @return a {@link InfoItemsPage} corresponding to the requested page
42+
* @see #getNextPageUrl()
43+
* @see InfoItemsPage#getNextPageUrl()
44+
*/
45+
public abstract InfoItemsPage<R> getPage(final String nextPageUrl) throws IOException, ExtractionException;
2246

2347
public boolean hasNextPage() throws IOException, ExtractionException {
2448
final String nextPageUrl = getNextPageUrl();
@@ -29,14 +53,34 @@ public boolean hasNextPage() throws IOException, ExtractionException {
2953
// Inner
3054
//////////////////////////////////////////////////////////////////////////*/
3155

32-
public static class InfoItemPage<T extends InfoItem> {
56+
/**
57+
* A class that is used to wrap a list of gathered items and eventual errors, it
58+
* also contains a field that points to the next available page ({@link #nextPageUrl}).
59+
*/
60+
public static class InfoItemsPage<T extends InfoItem> {
61+
private static final InfoItemsPage<InfoItem> EMPTY =
62+
new InfoItemsPage<>(Collections.<InfoItem>emptyList(), "", Collections.<Throwable>emptyList());
63+
64+
/**
65+
* A convenient method that returns a representation of an empty page.
66+
*
67+
* @return a type-safe page with the list of items and errors empty and the nextPageUrl set to an empty string.
68+
*/
69+
public static <T extends InfoItem> InfoItemsPage<T> emptyPage() {
70+
//noinspection unchecked
71+
return (InfoItemsPage<T>) EMPTY;
72+
}
73+
74+
3375
/**
34-
* The current list of items to this result
76+
* The current list of items of this page
3577
*/
3678
private final List<T> itemsList;
3779

3880
/**
39-
* Next url to fetch more items
81+
* Url pointing to the next page relative to this one
82+
*
83+
* @see ListExtractor#getPage(String)
4084
*/
4185
private final String nextPageUrl;
4286

@@ -45,11 +89,11 @@ public static class InfoItemPage<T extends InfoItem> {
4589
*/
4690
private final List<Throwable> errors;
4791

48-
public InfoItemPage(InfoItemsCollector<T, ?> collector, String nextPageUrl) {
92+
public InfoItemsPage(InfoItemsCollector<T, ?> collector, String nextPageUrl) {
4993
this(collector.getItems(), nextPageUrl, collector.getErrors());
5094
}
5195

52-
public InfoItemPage(List<T> itemsList, String nextPageUrl, List<Throwable> errors) {
96+
public InfoItemsPage(List<T> itemsList, String nextPageUrl, List<Throwable> errors) {
5397
this.itemsList = itemsList;
5498
this.nextPageUrl = nextPageUrl;
5599
this.errors = errors;
@@ -59,7 +103,7 @@ public boolean hasNextPage() {
59103
return nextPageUrl != null && !nextPageUrl.isEmpty();
60104
}
61105

62-
public List<T> getItemsList() {
106+
public List<T> getItems() {
63107
return itemsList;
64108
}
65109

src/main/java/org/schabi/newpipe/extractor/channel/ChannelExtractor.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package org.schabi.newpipe.extractor.channel;
22

3-
import edu.umd.cs.findbugs.annotations.NonNull;
43
import org.schabi.newpipe.extractor.ListExtractor;
54
import org.schabi.newpipe.extractor.StreamingService;
65
import org.schabi.newpipe.extractor.UrlIdHandler;
7-
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
86
import org.schabi.newpipe.extractor.exceptions.ParsingException;
97
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
10-
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
118

129
import javax.annotation.Nonnull;
13-
import java.io.IOException;
1410

1511
/*
1612
* Created by Christian Schabesberger on 25.07.16.
@@ -32,7 +28,7 @@
3228
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
3329
*/
3430

35-
public abstract class ChannelExtractor extends ListExtractor {
31+
public abstract class ChannelExtractor extends ListExtractor<StreamInfoItem> {
3632

3733
public ChannelExtractor(StreamingService service, String url) {
3834
super(service, url);
@@ -44,12 +40,6 @@ protected UrlIdHandler getUrlIdHandler() {
4440
return getService().getChannelUrlIdHandler();
4541
}
4642

47-
@NonNull
48-
@Override
49-
public abstract StreamInfoItemsCollector getInfoItems() throws IOException, ExtractionException;
50-
@Override
51-
public abstract InfoItemPage<StreamInfoItem> getPage(String nextPageUrl) throws IOException, ExtractionException;
52-
5343
public abstract String getAvatarUrl() throws ParsingException;
5444
public abstract String getBannerUrl() throws ParsingException;
5545
public abstract String getFeedUrl() throws ParsingException;

src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.schabi.newpipe.extractor.channel;
22

3-
import org.schabi.newpipe.extractor.ListExtractor.InfoItemPage;
3+
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
44
import org.schabi.newpipe.extractor.ListInfo;
55
import org.schabi.newpipe.extractor.NewPipe;
66
import org.schabi.newpipe.extractor.StreamingService;
@@ -30,18 +30,12 @@
3030
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
3131
*/
3232

33-
public class ChannelInfo extends ListInfo {
33+
public class ChannelInfo extends ListInfo<StreamInfoItem> {
3434

3535
public ChannelInfo(int serviceId, String url, String id, String name) {
3636
super(serviceId, id, url, name);
3737
}
3838

39-
40-
public static InfoItemPage<StreamInfoItem> getMoreItems(StreamingService service, String url, String pageUrl)
41-
throws IOException, ExtractionException {
42-
return service.getChannelExtractor(url).getPage(pageUrl);
43-
}
44-
4539
public static ChannelInfo getInfo(String url) throws IOException, ExtractionException {
4640
return getInfo(NewPipe.getServiceByUrl(url), url);
4741
}
@@ -52,6 +46,10 @@ public static ChannelInfo getInfo(StreamingService service, String url) throws I
5246
return getInfo(extractor);
5347
}
5448

49+
public static InfoItemsPage<StreamInfoItem> getMoreItems(StreamingService service, String url, String pageUrl) throws IOException, ExtractionException {
50+
return service.getChannelExtractor(url).getPage(pageUrl);
51+
}
52+
5553
public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException, ExtractionException {
5654

5755
// important data
@@ -79,7 +77,9 @@ public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException
7977
info.addError(e);
8078
}
8179

82-
info.setRelatedItems(ExtractorHelper.getInfoItemsOrLogError(info, extractor));
80+
final InfoItemsPage<StreamInfoItem> itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor);
81+
info.setRelatedItems(itemsPage.getItems());
82+
info.setNextPageUrl(itemsPage.getNextPageUrl());
8383

8484
try {
8585
info.setSubscriberCount(extractor.getSubscriberCount());
@@ -92,7 +92,6 @@ public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException
9292
info.addError(e);
9393
}
9494

95-
info.setNextPageUrl(extractor.getNextPageUrl());
9695
return info;
9796
}
9897

src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@
2020
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2121
*/
2222

23-
import edu.umd.cs.findbugs.annotations.NonNull;
2423
import org.schabi.newpipe.extractor.ListExtractor;
2524
import org.schabi.newpipe.extractor.StreamingService;
2625
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2726
import org.schabi.newpipe.extractor.exceptions.ParsingException;
2827
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
29-
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
3028

3129
import javax.annotation.Nonnull;
32-
import java.io.IOException;
3330

34-
public abstract class KioskExtractor extends ListExtractor {
31+
public abstract class KioskExtractor extends ListExtractor<StreamInfoItem> {
3532
private String contentCountry = null;
3633
private final String id;
3734

@@ -43,12 +40,6 @@ public KioskExtractor(StreamingService streamingService,
4340
this.id = kioskId;
4441
}
4542

46-
@NonNull
47-
@Override
48-
public abstract StreamInfoItemsCollector getInfoItems() throws IOException, ExtractionException;
49-
@Override
50-
public abstract InfoItemPage<StreamInfoItem> getPage(String nextPageUrl) throws IOException, ExtractionException;
51-
5243
/**
5344
* For certain Websites the content of a kiosk will be different depending
5445
* on the country you want to poen the website in. Therefore you should

src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030

3131
import java.io.IOException;
3232

33-
public class KioskInfo extends ListInfo {
33+
public class KioskInfo extends ListInfo<StreamInfoItem> {
3434

3535
private KioskInfo(int serviceId, String id, String url, String name) {
3636
super(serviceId, id, url, name);
3737
}
3838

39-
public static ListExtractor.InfoItemPage<StreamInfoItem> getMoreItems(StreamingService service,
40-
String url,
41-
String pageUrl,
42-
String contentCountry) throws IOException, ExtractionException {
39+
public static ListExtractor.InfoItemsPage<StreamInfoItem> getMoreItems(StreamingService service,
40+
String url,
41+
String pageUrl,
42+
String contentCountry) throws IOException, ExtractionException {
4343
KioskList kl = service.getKioskList();
4444
KioskExtractor extractor = kl.getExtractorByUrl(url, pageUrl);
4545
extractor.setContentCountry(contentCountry);
@@ -75,7 +75,9 @@ public static KioskInfo getInfo(KioskExtractor extractor) throws ExtractionExcep
7575

7676
KioskInfo info = new KioskInfo(serviceId, id, name, url);
7777

78-
info.setRelatedItems(ExtractorHelper.getInfoItemsOrLogError(info, extractor));
78+
final ListExtractor.InfoItemsPage<StreamInfoItem> itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor);
79+
info.setRelatedItems(itemsPage.getItems());
80+
info.setNextPageUrl(itemsPage.getNextPageUrl());
7981

8082
return info;
8183
}

src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistExtractor.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package org.schabi.newpipe.extractor.playlist;
22

3-
import edu.umd.cs.findbugs.annotations.NonNull;
43
import org.schabi.newpipe.extractor.ListExtractor;
54
import org.schabi.newpipe.extractor.StreamingService;
65
import org.schabi.newpipe.extractor.UrlIdHandler;
7-
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
86
import org.schabi.newpipe.extractor.exceptions.ParsingException;
97
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
10-
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
118

129
import javax.annotation.Nonnull;
13-
import java.io.IOException;
1410

15-
public abstract class PlaylistExtractor extends ListExtractor {
11+
public abstract class PlaylistExtractor extends ListExtractor<StreamInfoItem> {
1612

1713
public PlaylistExtractor(StreamingService service, String url) {
1814
super(service, url);
@@ -24,12 +20,6 @@ protected UrlIdHandler getUrlIdHandler() {
2420
return getService().getPlaylistUrlIdHandler();
2521
}
2622

27-
@NonNull
28-
@Override
29-
public abstract StreamInfoItemsCollector getInfoItems() throws IOException, ExtractionException;
30-
@Override
31-
public abstract InfoItemPage<StreamInfoItem> getPage(String nextPageUrl) throws IOException, ExtractionException;
32-
3323
public abstract String getThumbnailUrl() throws ParsingException;
3424
public abstract String getBannerUrl() throws ParsingException;
3525

src/main/java/org/schabi/newpipe/extractor/playlist/PlaylistInfo.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
package org.schabi.newpipe.extractor.playlist;
22

3-
import org.schabi.newpipe.extractor.ListExtractor.InfoItemPage;
3+
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
44
import org.schabi.newpipe.extractor.ListInfo;
55
import org.schabi.newpipe.extractor.NewPipe;
66
import org.schabi.newpipe.extractor.StreamingService;
77
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
88
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
9+
import org.schabi.newpipe.extractor.utils.ExtractorHelper;
910

1011
import java.io.IOException;
1112

12-
import static org.schabi.newpipe.extractor.utils.ExtractorHelper.getInfoItemsOrLogError;
13-
14-
public class PlaylistInfo extends ListInfo {
13+
public class PlaylistInfo extends ListInfo<StreamInfoItem> {
1514

1615
public PlaylistInfo(int serviceId, String id, String url, String name) {
1716
super(serviceId, id, url, name);
1817
}
1918

20-
public static InfoItemPage<StreamInfoItem> getMoreItems(StreamingService service, String url, String pageUrl) throws IOException, ExtractionException {
21-
return service.getPlaylistExtractor(url).getPage(pageUrl);
22-
}
23-
2419
public static PlaylistInfo getInfo(String url) throws IOException, ExtractionException {
2520
return getInfo(NewPipe.getServiceByUrl(url), url);
2621
}
@@ -31,6 +26,10 @@ public static PlaylistInfo getInfo(StreamingService service, String url) throws
3126
return getInfo(extractor);
3227
}
3328

29+
public static InfoItemsPage<StreamInfoItem> getMoreItems(StreamingService service, String url, String pageUrl) throws IOException, ExtractionException {
30+
return service.getPlaylistExtractor(url).getPage(pageUrl);
31+
}
32+
3433
/**
3534
* Get PlaylistInfo from PlaylistExtractor
3635
*
@@ -75,8 +74,10 @@ public static PlaylistInfo getInfo(PlaylistExtractor extractor) throws IOExcepti
7574
info.addError(e);
7675
}
7776

78-
info.setRelatedItems(getInfoItemsOrLogError(info, extractor));
79-
info.setNextPageUrl(extractor.getNextPageUrl());
77+
final InfoItemsPage<StreamInfoItem> itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor);
78+
info.setRelatedItems(itemsPage.getItems());
79+
info.setNextPageUrl(itemsPage.getNextPageUrl());
80+
8081
return info;
8182
}
8283

src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChannelExtractor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ public String getDescription() {
9292

9393
@Nonnull
9494
@Override
95-
public StreamInfoItemsCollector getInfoItems() throws ExtractionException {
95+
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
9696
if(streamInfoItemsCollector == null) {
9797
computeNextPageAndGetStreams();
9898
}
99-
return streamInfoItemsCollector;
99+
return new InfoItemsPage<>(streamInfoItemsCollector, getNextPageUrl());
100100
}
101101

102102
@Override
@@ -123,14 +123,14 @@ private void computeNextPageAndGetStreams() throws ExtractionException {
123123
}
124124

125125
@Override
126-
public InfoItemPage<StreamInfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
126+
public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
127127
if (pageUrl == null || pageUrl.isEmpty()) {
128128
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
129129
}
130130

131131
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
132132
String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApiMinItems(15, collector, pageUrl);
133133

134-
return new InfoItemPage<>(collector, nextPageUrl);
134+
return new InfoItemsPage<>(collector, nextPageUrl);
135135
}
136136
}

0 commit comments

Comments
 (0)