2017-07-10 00:43:04 +02:00
|
|
|
package org.schabi.newpipe.extractor;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
2018-07-13 18:02:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2017-07-10 00:43:04 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-03-11 21:54:41 +01:00
|
|
|
import java.util.Collections;
|
2017-08-06 22:20:15 +02:00
|
|
|
import java.util.List;
|
2017-07-10 00:43:04 +02:00
|
|
|
|
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-07-10 00:43:04 +02:00
|
|
|
/**
|
2017-08-07 18:12:51 +02:00
|
|
|
* Base class to extractors that have a list (e.g. playlists, users).
|
2017-07-10 00:43:04 +02:00
|
|
|
*/
|
2018-03-11 21:54:41 +01:00
|
|
|
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;
|
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
public ListExtractor(StreamingService service, ListLinkHandler linkHandler) {
|
|
|
|
super(service, linkHandler);
|
2017-07-10 00:43:04 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
/**
|
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.
|
2018-03-11 21:54:41 +01:00
|
|
|
*
|
|
|
|
* @return a {@link InfoItemsPage} corresponding to the initial page
|
|
|
|
*/
|
2017-11-25 02:03:30 +01:00
|
|
|
@Nonnull
|
2018-03-11 21:54:41 +01:00
|
|
|
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
|
2018-03-11 21:54:41 +01:00
|
|
|
* @return a {@link InfoItemsPage} corresponding to the requested page
|
2020-04-15 14:09:46 +02:00
|
|
|
* @see InfoItemsPage#getNextPage()
|
2018-03-11 21:54:41 +01:00
|
|
|
*/
|
2020-04-15 14:09:46 +02:00
|
|
|
public abstract InfoItemsPage<R> getPage(final Page page) throws IOException, ExtractionException;
|
2017-07-10 00:43:04 +02:00
|
|
|
|
2018-05-13 21:28:51 +02:00
|
|
|
@Override
|
2018-09-15 20:12:52 +02:00
|
|
|
public ListLinkHandler getLinkHandler() {
|
|
|
|
return (ListLinkHandler) super.getLinkHandler();
|
2018-05-13 21:28:51 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Inner
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
/**
|
|
|
|
* 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}).
|
2018-03-11 21:54:41 +01:00
|
|
|
*/
|
|
|
|
public static class InfoItemsPage<T extends InfoItem> {
|
|
|
|
private static final InfoItemsPage<InfoItem> EMPTY =
|
2020-04-15 14:09:46 +02:00
|
|
|
new InfoItemsPage<>(Collections.<InfoItem>emptyList(), null, Collections.<Throwable>emptyList());
|
2018-03-11 21:54:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A convenient method that returns a representation of an empty page.
|
|
|
|
*
|
2020-04-15 14:09:46 +02:00
|
|
|
* @return a type-safe page with the list of items and errors empty and the nextPage set to {@code null}.
|
2018-03-11 21:54:41 +01:00
|
|
|
*/
|
|
|
|
public static <T extends InfoItem> InfoItemsPage<T> emptyPage() {
|
|
|
|
//noinspection unchecked
|
|
|
|
return (InfoItemsPage<T>) EMPTY;
|
|
|
|
}
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
/**
|
2018-03-11 21:54:41 +01: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
|
|
|
|
|
|
|
/**
|
2018-03-11 21:54:41 +01: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
|
|
|
|
2017-08-12 22:19:56 +02:00
|
|
|
/**
|
|
|
|
* Errors that happened during the extraction
|
|
|
|
*/
|
2018-03-01 01:02:43 +01:00
|
|
|
private final List<Throwable> errors;
|
2017-08-12 22:19:56 +02:00
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
public InfoItemsPage(InfoItemsCollector<T, ?> collector, Page nextPage) {
|
|
|
|
this(collector.getItems(), nextPage, collector.getErrors());
|
2017-08-12 22:19:56 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
public InfoItemsPage(List<T> itemsList, Page nextPage, 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;
|
2017-08-12 22:19:56 +02:00
|
|
|
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
|
|
|
}
|
2017-11-11 12:17:14 +01:00
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
public List<T> getItems() {
|
2018-03-01 01:02:43 +01:00
|
|
|
return itemsList;
|
2017-11-11 12:17:14 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
public Page getNextPage() {
|
|
|
|
return nextPage;
|
2017-11-11 12:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Throwable> getErrors() {
|
|
|
|
return errors;
|
|
|
|
}
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
2017-07-10 00:43:04 +02:00
|
|
|
}
|