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

133 lines
4.3 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;
import java.io.IOException;
import java.util.Collections;
2017-08-06 22:20:15 +02:00
import java.util.List;
2020-06-13 20:25:38 +02:00
import javax.annotation.Nonnull;
2020-04-15 18:49:58 +02:00
2020-04-15 14:09:46 +02:00
/**
2017-08-07 18:12:51 +02:00
* Base class to extractors that have a list (e.g. playlists, users).
* @param <R> the info item type this list extractor provides
*/
public abstract class ListExtractor<R extends InfoItem> extends Extractor {
2020-03-21 19:56:17 +01:00
/**
* Constant that should be returned whenever
* a list has an unknown number of items.
*/
public static final long ITEM_COUNT_UNKNOWN = -1;
/**
* Constant that should be returned whenever a list has an
* infinite number of items. For example a YouTube mix.
*/
public static final long ITEM_COUNT_INFINITE = -2;
/**
* Constant that should be returned whenever a list
* has an unknown number of items bigger than 100.
*/
public static final long ITEM_COUNT_MORE_THAN_100 = -3;
public ListExtractor(final StreamingService service, final ListLinkHandler linkHandler) {
super(service, linkHandler);
}
/**
2020-04-15 14:09:46 +02:00
* A {@link InfoItemsPage InfoItemsPage} corresponding to the initial page
* where the items are from the initial request and the nextPage 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;
/**
* Get a list of items corresponding to the specific requested page.
*
2020-04-15 14:09:46 +02:00
* @param page any page got from the exclusive implementation of the list extractor
* @return a {@link InfoItemsPage} corresponding to the requested page
2020-04-15 14:09:46 +02:00
* @see InfoItemsPage#getNextPage()
*/
public abstract InfoItemsPage<R> getPage(Page page) throws IOException, ExtractionException;
@Nonnull
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
2020-04-15 14:09:46 +02:00
* also contains a field that points to the next available page ({@link #nextPage}).
* @param <T> the info item type that this page is supposed to store and provide
*/
public static class InfoItemsPage<T extends InfoItem> {
private static final InfoItemsPage<InfoItem> EMPTY =
new InfoItemsPage<>(Collections.emptyList(), null, Collections.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 nextPage set to
* {@code null}.
*/
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
*
2020-04-15 14:09:46 +02:00
* @see ListExtractor#getPage(Page)
* @see Page
2017-08-06 22:20:15 +02:00
*/
2020-04-15 14:09:46 +02:00
private final Page nextPage;
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(final InfoItemsCollector<T, ?> collector, final Page nextPage) {
2020-04-15 14:09:46 +02:00
this(collector.getItems(), nextPage, collector.getErrors());
}
public InfoItemsPage(final List<T> itemsList,
final Page nextPage,
final List<Throwable> errors) {
2018-03-01 01:02:43 +01:00
this.itemsList = itemsList;
2020-04-15 14:09:46 +02:00
this.nextPage = nextPage;
this.errors = errors;
2017-08-06 22:20:15 +02:00
}
2018-02-24 22:20:50 +01:00
public boolean hasNextPage() {
2020-06-13 20:25:38 +02:00
return Page.isValid(nextPage);
2017-08-06 22:20:15 +02:00
}
public List<T> getItems() {
2018-03-01 01:02:43 +01:00
return itemsList;
}
2020-04-15 14:09:46 +02:00
public Page getNextPage() {
return nextPage;
}
public List<Throwable> getErrors() {
return errors;
}
2017-08-06 22:20:15 +02:00
}
}