Skip to content

Commit 4cc3120

Browse files
committed
Introduce Page class
1 parent e3bfdba commit 4cc3120

43 files changed

Lines changed: 463 additions & 415 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
55
import org.schabi.newpipe.extractor.utils.Utils;
66

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

11-
import javax.annotation.Nonnull;
12-
1312
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
1413

14+
1515
/**
1616
* Base class to extractors that have a list (e.g. playlists, users).
1717
*/
@@ -37,8 +37,8 @@ public ListExtractor(StreamingService service, ListLinkHandler linkHandler) {
3737
}
3838

3939
/**
40-
* A {@link InfoItemsPage InfoItemsPage} corresponding to the initial page where the items are from the initial request and
41-
* the nextPageUrl relative to it.
40+
* A {@link InfoItemsPage InfoItemsPage} corresponding to the initial page
41+
* where the items are from the initial request and the nextPage relative to it.
4242
*
4343
* @return a {@link InfoItemsPage} corresponding to the initial page
4444
*/
@@ -48,11 +48,11 @@ public ListExtractor(StreamingService service, ListLinkHandler linkHandler) {
4848
/**
4949
* Get a list of items corresponding to the specific requested page.
5050
*
51-
* @param pageUrl any page url got from the exclusive implementation of the list extractor
51+
* @param page any page got from the exclusive implementation of the list extractor
5252
* @return a {@link InfoItemsPage} corresponding to the requested page
53-
* @see InfoItemsPage#getNextPageUrl()
53+
* @see InfoItemsPage#getNextPage()
5454
*/
55-
public abstract InfoItemsPage<R> getPage(final String pageUrl) throws IOException, ExtractionException;
55+
public abstract InfoItemsPage<R> getPage(final Page page) throws IOException, ExtractionException;
5656

5757
@Override
5858
public ListLinkHandler getLinkHandler() {
@@ -65,23 +65,22 @@ public ListLinkHandler getLinkHandler() {
6565

6666
/**
6767
* A class that is used to wrap a list of gathered items and eventual errors, it
68-
* also contains a field that points to the next available page ({@link #nextPageUrl}).
68+
* also contains a field that points to the next available page ({@link #nextPage}).
6969
*/
7070
public static class InfoItemsPage<T extends InfoItem> {
7171
private static final InfoItemsPage<InfoItem> EMPTY =
72-
new InfoItemsPage<>(Collections.<InfoItem>emptyList(), "", Collections.<Throwable>emptyList());
72+
new InfoItemsPage<>(Collections.<InfoItem>emptyList(), null, Collections.<Throwable>emptyList());
7373

7474
/**
7575
* A convenient method that returns a representation of an empty page.
7676
*
77-
* @return a type-safe page with the list of items and errors empty and the nextPageUrl set to an empty string.
77+
* @return a type-safe page with the list of items and errors empty and the nextPage set to {@code null}.
7878
*/
7979
public static <T extends InfoItem> InfoItemsPage<T> emptyPage() {
8080
//noinspection unchecked
8181
return (InfoItemsPage<T>) EMPTY;
8282
}
8383

84-
8584
/**
8685
* The current list of items of this page
8786
*/
@@ -90,35 +89,37 @@ public static <T extends InfoItem> InfoItemsPage<T> emptyPage() {
9089
/**
9190
* Url pointing to the next page relative to this one
9291
*
93-
* @see ListExtractor#getPage(String)
92+
* @see ListExtractor#getPage(Page)
93+
* @see Page
9494
*/
95-
private final String nextPageUrl;
95+
private final Page nextPage;
9696

9797
/**
9898
* Errors that happened during the extraction
9999
*/
100100
private final List<Throwable> errors;
101101

102-
public InfoItemsPage(InfoItemsCollector<T, ?> collector, String nextPageUrl) {
103-
this(collector.getItems(), nextPageUrl, collector.getErrors());
102+
public InfoItemsPage(InfoItemsCollector<T, ?> collector, Page nextPage) {
103+
this(collector.getItems(), nextPage, collector.getErrors());
104104
}
105105

106-
public InfoItemsPage(List<T> itemsList, String nextPageUrl, List<Throwable> errors) {
106+
public InfoItemsPage(List<T> itemsList, Page nextPage, List<Throwable> errors) {
107107
this.itemsList = itemsList;
108-
this.nextPageUrl = nextPageUrl;
108+
this.nextPage = nextPage;
109109
this.errors = errors;
110110
}
111111

112112
public boolean hasNextPage() {
113-
return !isNullOrEmpty(nextPageUrl);
113+
return nextPage != null && (!isNullOrEmpty(nextPage.getUrl())
114+
|| !isNullOrEmpty(nextPage.getIds()));
114115
}
115116

116117
public List<T> getItems() {
117118
return itemsList;
118119
}
119120

120-
public String getNextPageUrl() {
121-
return nextPageUrl;
121+
public Page getNextPage() {
122+
return nextPage;
122123
}
123124

124125
public List<Throwable> getErrors() {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public abstract class ListInfo<T extends InfoItem> extends Info {
1010
private List<T> relatedItems;
11-
private String nextPageUrl = null;
11+
private Page nextPage = null;
1212
private final List<String> contentFilters;
1313
private final String sortFilter;
1414

@@ -39,15 +39,16 @@ public void setRelatedItems(List<T> relatedItems) {
3939
}
4040

4141
public boolean hasNextPage() {
42-
return !isNullOrEmpty(nextPageUrl);
42+
return nextPage != null && (!isNullOrEmpty(nextPage.getUrl())
43+
|| !isNullOrEmpty(nextPage.getIds()));
4344
}
4445

45-
public String getNextPageUrl() {
46-
return nextPageUrl;
46+
public Page getNextPage() {
47+
return nextPage;
4748
}
4849

49-
public void setNextPageUrl(String pageUrl) {
50-
this.nextPageUrl = pageUrl;
50+
public void setNextPage(Page page) {
51+
this.nextPage = page;
5152
}
5253

5354
public List<String> getContentFilters() {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.schabi.newpipe.extractor;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
public class Page implements Serializable {
8+
private final String url;
9+
private final List<String> ids;
10+
private final Map<String, String> cookies;
11+
12+
public Page(final String url, final List<String> ids, final Map<String, String> cookies) {
13+
this.url = url;
14+
this.ids = ids;
15+
this.cookies = cookies;
16+
}
17+
18+
public Page(final String url) {
19+
this(url, null, null);
20+
}
21+
22+
public Page(final String url, final Map<String, String> cookies) {
23+
this(url, null, cookies);
24+
}
25+
26+
public Page(final List<String> ids) {
27+
this(null, ids, null);
28+
}
29+
30+
public Page(final List<String> ids, final Map<String, String> cookies) {
31+
this(null, ids, cookies);
32+
}
33+
34+
public String getUrl() {
35+
return url;
36+
}
37+
38+
public List<String> getIds() {
39+
return ids;
40+
}
41+
42+
public Map<String, String> getCookies() {
43+
return cookies;
44+
}
45+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
44
import org.schabi.newpipe.extractor.ListInfo;
55
import org.schabi.newpipe.extractor.NewPipe;
6+
import org.schabi.newpipe.extractor.Page;
67
import org.schabi.newpipe.extractor.StreamingService;
78
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
89
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
@@ -49,8 +50,8 @@ public static ChannelInfo getInfo(StreamingService service, String url) throws I
4950

5051
public static InfoItemsPage<StreamInfoItem> getMoreItems(StreamingService service,
5152
String url,
52-
String pageUrl) throws IOException, ExtractionException {
53-
return service.getChannelExtractor(url).getPage(pageUrl);
53+
Page page) throws IOException, ExtractionException {
54+
return service.getChannelExtractor(url).getPage(page);
5455
}
5556

5657
public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException, ExtractionException {
@@ -81,7 +82,7 @@ public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException
8182

8283
final InfoItemsPage<StreamInfoItem> itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor);
8384
info.setRelatedItems(itemsPage.getItems());
84-
info.setNextPageUrl(itemsPage.getNextPageUrl());
85+
info.setNextPage(itemsPage.getNextPage());
8586

8687
try {
8788
info.setSubscriberCount(extractor.getSubscriberCount());

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.schabi.newpipe.extractor.ListExtractor.InfoItemsPage;
44
import org.schabi.newpipe.extractor.ListInfo;
55
import org.schabi.newpipe.extractor.NewPipe;
6+
import org.schabi.newpipe.extractor.Page;
67
import org.schabi.newpipe.extractor.StreamingService;
78
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
89
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
@@ -39,23 +40,23 @@ private static CommentsInfo getInfo(CommentsExtractor commentsExtractor) throws
3940
InfoItemsPage<CommentsInfoItem> initialCommentsPage = ExtractorHelper.getItemsPageOrLogError(commentsInfo,
4041
commentsExtractor);
4142
commentsInfo.setRelatedItems(initialCommentsPage.getItems());
42-
commentsInfo.setNextPageUrl(initialCommentsPage.getNextPageUrl());
43+
commentsInfo.setNextPage(initialCommentsPage.getNextPage());
4344

4445
return commentsInfo;
4546
}
4647

47-
public static InfoItemsPage<CommentsInfoItem> getMoreItems(CommentsInfo commentsInfo, String pageUrl)
48+
public static InfoItemsPage<CommentsInfoItem> getMoreItems(CommentsInfo commentsInfo, Page page)
4849
throws ExtractionException, IOException {
49-
return getMoreItems(NewPipe.getService(commentsInfo.getServiceId()), commentsInfo, pageUrl);
50+
return getMoreItems(NewPipe.getService(commentsInfo.getServiceId()), commentsInfo, page);
5051
}
5152

5253
public static InfoItemsPage<CommentsInfoItem> getMoreItems(StreamingService service, CommentsInfo commentsInfo,
53-
String pageUrl) throws IOException, ExtractionException {
54+
Page page) throws IOException, ExtractionException {
5455
if (null == commentsInfo.getCommentsExtractor()) {
5556
commentsInfo.setCommentsExtractor(service.getCommentsExtractor(commentsInfo.getUrl()));
5657
commentsInfo.getCommentsExtractor().fetchPage();
5758
}
58-
return commentsInfo.getCommentsExtractor().getPage(pageUrl);
59+
return commentsInfo.getCommentsExtractor().getPage(page);
5960
}
6061

6162
private transient CommentsExtractor commentsExtractor;

extractor/src/main/java/org/schabi/newpipe/extractor/feed/FeedInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static FeedInfo getInfo(FeedExtractor extractor) throws IOException, Extr
4545

4646
final InfoItemsPage<StreamInfoItem> itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor);
4747
info.setRelatedItems(itemsPage.getItems());
48-
info.setNextPageUrl(itemsPage.getNextPageUrl());
48+
info.setNextPage(itemsPage.getNextPage());
4949

5050
return info;
5151
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.schabi.newpipe.extractor.ListExtractor;
2424
import org.schabi.newpipe.extractor.ListInfo;
2525
import org.schabi.newpipe.extractor.NewPipe;
26+
import org.schabi.newpipe.extractor.Page;
2627
import org.schabi.newpipe.extractor.StreamingService;
2728
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2829
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -33,18 +34,17 @@
3334
import java.io.IOException;
3435

3536
public class KioskInfo extends ListInfo<StreamInfoItem> {
36-
3737
private KioskInfo(int serviceId, ListLinkHandler linkHandler, String name) throws ParsingException {
3838
super(serviceId, linkHandler, name);
3939
}
4040

4141
public static ListExtractor.InfoItemsPage<StreamInfoItem> getMoreItems(StreamingService service,
4242
String url,
43-
String pageUrl)
43+
Page page)
4444
throws IOException, ExtractionException {
4545
KioskList kl = service.getKioskList();
46-
KioskExtractor extractor = kl.getExtractorByUrl(url, pageUrl);
47-
return extractor.getPage(pageUrl);
46+
KioskExtractor extractor = kl.getExtractorByUrl(url, page);
47+
return extractor.getPage(page);
4848
}
4949

5050
public static KioskInfo getInfo(String url) throws IOException, ExtractionException {
@@ -71,7 +71,7 @@ public static KioskInfo getInfo(KioskExtractor extractor) throws ExtractionExcep
7171

7272
final ListExtractor.InfoItemsPage<StreamInfoItem> itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor);
7373
info.setRelatedItems(itemsPage.getItems());
74-
info.setNextPageUrl(itemsPage.getNextPageUrl());
74+
info.setNextPage(itemsPage.getNextPage());
7575

7676
return info;
7777
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.extractor.kiosk;
22

33
import org.schabi.newpipe.extractor.NewPipe;
4+
import org.schabi.newpipe.extractor.Page;
45
import org.schabi.newpipe.extractor.StreamingService;
56
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
67
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
@@ -59,23 +60,23 @@ public void setDefaultKiosk(String kioskType) {
5960

6061
public KioskExtractor getDefaultKioskExtractor()
6162
throws ExtractionException, IOException {
62-
return getDefaultKioskExtractor("");
63+
return getDefaultKioskExtractor(null);
6364
}
6465

65-
public KioskExtractor getDefaultKioskExtractor(String nextPageUrl)
66+
public KioskExtractor getDefaultKioskExtractor(Page nextPage)
6667
throws ExtractionException, IOException {
67-
return getDefaultKioskExtractor(nextPageUrl, NewPipe.getPreferredLocalization());
68+
return getDefaultKioskExtractor(nextPage, NewPipe.getPreferredLocalization());
6869
}
6970

70-
public KioskExtractor getDefaultKioskExtractor(String nextPageUrl, Localization localization)
71+
public KioskExtractor getDefaultKioskExtractor(Page nextPage, Localization localization)
7172
throws ExtractionException, IOException {
7273
if (defaultKiosk != null && !defaultKiosk.equals("")) {
73-
return getExtractorById(defaultKiosk, nextPageUrl, localization);
74+
return getExtractorById(defaultKiosk, nextPage, localization);
7475
} else {
7576
if (!kioskList.isEmpty()) {
7677
// if not set get any entry
7778
Object[] keySet = kioskList.keySet().toArray();
78-
return getExtractorById(keySet[0].toString(), nextPageUrl, localization);
79+
return getExtractorById(keySet[0].toString(), nextPage, localization);
7980
} else {
8081
return null;
8182
}
@@ -86,12 +87,12 @@ public String getDefaultKioskId() {
8687
return defaultKiosk;
8788
}
8889

89-
public KioskExtractor getExtractorById(String kioskId, String nextPageUrl)
90+
public KioskExtractor getExtractorById(String kioskId, Page nextPage)
9091
throws ExtractionException, IOException {
91-
return getExtractorById(kioskId, nextPageUrl, NewPipe.getPreferredLocalization());
92+
return getExtractorById(kioskId, nextPage, NewPipe.getPreferredLocalization());
9293
}
9394

94-
public KioskExtractor getExtractorById(String kioskId, String nextPageUrl, Localization localization)
95+
public KioskExtractor getExtractorById(String kioskId, Page nextPage, Localization localization)
9596
throws ExtractionException, IOException {
9697
KioskEntry ke = kioskList.get(kioskId);
9798
if (ke == null) {
@@ -111,17 +112,17 @@ public Set<String> getAvailableKiosks() {
111112
return kioskList.keySet();
112113
}
113114

114-
public KioskExtractor getExtractorByUrl(String url, String nextPageUrl)
115+
public KioskExtractor getExtractorByUrl(String url, Page nextPage)
115116
throws ExtractionException, IOException {
116-
return getExtractorByUrl(url, nextPageUrl, NewPipe.getPreferredLocalization());
117+
return getExtractorByUrl(url, nextPage, NewPipe.getPreferredLocalization());
117118
}
118119

119-
public KioskExtractor getExtractorByUrl(String url, String nextPageUrl, Localization localization)
120+
public KioskExtractor getExtractorByUrl(String url, Page nextPage, Localization localization)
120121
throws ExtractionException, IOException {
121122
for (Map.Entry<String, KioskEntry> e : kioskList.entrySet()) {
122123
KioskEntry ke = e.getValue();
123124
if (ke.handlerFactory.acceptUrl(url)) {
124-
return getExtractorById(ke.handlerFactory.getId(url), nextPageUrl, localization);
125+
return getExtractorById(ke.handlerFactory.getId(url), nextPage, localization);
125126
}
126127
}
127128
throw new ExtractionException("Could not find a kiosk that fits to the url: " + url);

0 commit comments

Comments
 (0)