[media.ccc.de] Recent kiosk: order streams by upload date

This commit is contained in:
TobiGr 2020-12-30 18:38:04 +01:00
parent 3c8c8e7307
commit 5dc9a76e3c
3 changed files with 31 additions and 2 deletions

View File

@ -3,8 +3,10 @@ package org.schabi.newpipe.extractor;
import org.schabi.newpipe.extractor.exceptions.FoundAdException; import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
/* /*
@ -32,17 +34,31 @@ public abstract class InfoItemsCollector<I extends InfoItem, E extends InfoItemE
private final List<I> itemList = new ArrayList<>(); private final List<I> itemList = new ArrayList<>();
private final List<Throwable> errors = new ArrayList<>(); private final List<Throwable> errors = new ArrayList<>();
private final int serviceId; private final int serviceId;
@Nullable
private final Comparator<I> comparator;
/**
* Create a new collector with no comparator / sorting function
* @param serviceId the service id
*/
public InfoItemsCollector(final int serviceId) {
this(serviceId, null);
}
/** /**
* Create a new collector * Create a new collector
* @param serviceId the service id * @param serviceId the service id
*/ */
public InfoItemsCollector(int serviceId) { public InfoItemsCollector(final int serviceId, @Nullable final Comparator<I> comparator) {
this.serviceId = serviceId; this.serviceId = serviceId;
this.comparator = comparator;
} }
@Override @Override
public List<I> getItems() { public List<I> getItems() {
if (comparator != null) {
itemList.sort(comparator);
}
return Collections.unmodifiableList(itemList); return Collections.unmodifiableList(itemList);
} }

View File

@ -16,6 +16,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
import java.util.Comparator;
public class MediaCCCRecentKiosk extends KioskExtractor<StreamInfoItem> { public class MediaCCCRecentKiosk extends KioskExtractor<StreamInfoItem> {
@ -40,7 +41,14 @@ public class MediaCCCRecentKiosk extends KioskExtractor<StreamInfoItem> {
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
final JsonArray events = doc.getArray("events"); final JsonArray events = doc.getArray("events");
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
// Streams in the recent kiosk are not ordered by the release date.
// Sort them to have the latest stream at the beginning of the list.
Comparator<StreamInfoItem> comparator = Comparator.comparing(
streamInfoItem -> streamInfoItem.getUploadDate().offsetDateTime());
comparator = comparator.reversed();
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId(), comparator);
for (int i = 0; i < events.size(); i++) { for (int i = 0; i < events.size(); i++) {
collector.commit(new MediaCCCRecentKioskExtractor(events.getObject(i))); collector.commit(new MediaCCCRecentKioskExtractor(events.getObject(i)));
} }

View File

@ -5,6 +5,7 @@ import org.schabi.newpipe.extractor.InfoItemsCollector;
import org.schabi.newpipe.extractor.exceptions.FoundAdException; import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Vector; import java.util.Vector;
@ -34,6 +35,10 @@ public class StreamInfoItemsCollector extends InfoItemsCollector<StreamInfoItem,
super(serviceId); super(serviceId);
} }
public StreamInfoItemsCollector(int serviceId, Comparator<StreamInfoItem> comparator) {
super(serviceId, comparator);
}
@Override @Override
public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws ParsingException { public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws ParsingException {
if (extractor.isAd()) { if (extractor.isAd()) {