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

126 lines
4.2 KiB
Java
Raw Normal View History

package org.schabi.newpipe.extractor;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
2017-11-25 02:03:30 +01:00
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Collections;
2017-08-06 22:20:15 +02:00
import java.util.List;
/**
2017-08-07 18:12:51 +02:00
* Base class to extractors that have a list (e.g. playlists, users).
*/
public abstract class ListExtractor<R extends InfoItem> extends Extractor {
public ListExtractor(StreamingService service, ListLinkHandler linkHandler) {
super(service, linkHandler);
}
/**
* A {@link InfoItemsPage InfoItemsPage} corresponding to the initial page where the items are from the initial request and
* the nextPageUrl relative to it.
*
* @return a {@link InfoItemsPage} corresponding to the initial page
*/
2017-11-25 02:03:30 +01:00
@Nonnull
public abstract InfoItemsPage<R> getInitialPage() throws IOException, ExtractionException;
/**
* Returns an url that can be used to get the next page relative to the initial one.
* <p>Usually, these links will only work in the implementation itself.</p>
*
* @return an url pointing to the next page relative to the initial page
* @see #getPage(String)
*/
2018-02-26 15:55:27 +01:00
public abstract String getNextPageUrl() throws IOException, ExtractionException;
/**
* Get a list of items corresponding to the specific requested page.
*
* @param pageUrl any page url got from the exclusive implementation of the list extractor
* @return a {@link InfoItemsPage} corresponding to the requested page
* @see #getNextPageUrl()
* @see InfoItemsPage#getNextPageUrl()
*/
public abstract InfoItemsPage<R> getPage(final String pageUrl) throws IOException, ExtractionException;
2018-02-26 15:55:27 +01:00
public boolean hasNextPage() throws IOException, ExtractionException {
2018-03-01 01:02:43 +01:00
final String nextPageUrl = getNextPageUrl();
return nextPageUrl != null && !nextPageUrl.isEmpty();
}
2018-05-13 21:28:51 +02:00
@Override
public ListLinkHandler getLinkHandler() {
return (ListLinkHandler) super.getLinkHandler();
2018-05-13 21:28:51 +02:00
}
2017-08-06 22:20:15 +02:00
/*//////////////////////////////////////////////////////////////////////////
// Inner
//////////////////////////////////////////////////////////////////////////*/
/**
* A class that is used to wrap a list of gathered items and eventual errors, it
* also contains a field that points to the next available page ({@link #nextPageUrl}).
*/
public static class InfoItemsPage<T extends InfoItem> {
private static final InfoItemsPage<InfoItem> EMPTY =
new InfoItemsPage<>(Collections.<InfoItem>emptyList(), "", Collections.<Throwable>emptyList());
/**
* A convenient method that returns a representation of an empty page.
*
* @return a type-safe page with the list of items and errors empty and the nextPageUrl set to an empty string.
*/
public static <T extends InfoItem> InfoItemsPage<T> emptyPage() {
//noinspection unchecked
return (InfoItemsPage<T>) EMPTY;
}
2017-08-06 22:20:15 +02:00
/**
* The current list of items of this page
2017-08-06 22:20:15 +02:00
*/
2018-03-01 01:02:43 +01:00
private final List<T> itemsList;
2017-08-06 22:20:15 +02:00
/**
* Url pointing to the next page relative to this one
*
* @see ListExtractor#getPage(String)
2017-08-06 22:20:15 +02:00
*/
2018-03-01 01:02:43 +01:00
private final String nextPageUrl;
2017-08-06 22:20:15 +02:00
/**
* Errors that happened during the extraction
*/
2018-03-01 01:02:43 +01:00
private final List<Throwable> errors;
public InfoItemsPage(InfoItemsCollector<T, ?> collector, String nextPageUrl) {
this(collector.getItems(), nextPageUrl, collector.getErrors());
}
public InfoItemsPage(List<T> itemsList, String nextPageUrl, List<Throwable> errors) {
2018-03-01 01:02:43 +01:00
this.itemsList = itemsList;
2018-02-24 22:20:50 +01:00
this.nextPageUrl = nextPageUrl;
this.errors = errors;
2017-08-06 22:20:15 +02:00
}
2018-02-24 22:20:50 +01:00
public boolean hasNextPage() {
return nextPageUrl != null && !nextPageUrl.isEmpty();
2017-08-06 22:20:15 +02:00
}
public List<T> getItems() {
2018-03-01 01:02:43 +01:00
return itemsList;
}
2018-02-24 22:20:50 +01:00
public String getNextPageUrl() {
return nextPageUrl;
}
public List<Throwable> getErrors() {
return errors;
}
2017-08-06 22:20:15 +02:00
}
}