mirror of
https://github.com/TeamNewPipe/NewPipeExtractor
synced 2025-08-29 13:27:38 +00:00
Implement pagination in YoutubePlaylistExtractor
This commit is contained in:
parent
4039409820
commit
c0a8e01889
@ -231,6 +231,10 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
|||||||
|
|
||||||
|
|
||||||
private String getNextPageUrlFrom(JsonArray continuations) {
|
private String getNextPageUrlFrom(JsonArray continuations) {
|
||||||
|
if (continuations == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData");
|
JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData");
|
||||||
String continuation = nextContinuationData.getString("continuation");
|
String continuation = nextContinuationData.getString("continuation");
|
||||||
String clickTrackingParams = nextContinuationData.getString("clickTrackingParams");
|
String clickTrackingParams = nextContinuationData.getString("clickTrackingParams");
|
||||||
|
@ -5,9 +5,7 @@ import com.grack.nanojson.JsonObject;
|
|||||||
import com.grack.nanojson.JsonParser;
|
import com.grack.nanojson.JsonParser;
|
||||||
import com.grack.nanojson.JsonParserException;
|
import com.grack.nanojson.JsonParserException;
|
||||||
|
|
||||||
import org.jsoup.Jsoup;
|
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.jsoup.nodes.Element;
|
|
||||||
import org.schabi.newpipe.extractor.StreamingService;
|
import org.schabi.newpipe.extractor.StreamingService;
|
||||||
import org.schabi.newpipe.extractor.downloader.Downloader;
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
||||||
import org.schabi.newpipe.extractor.downloader.Response;
|
import org.schabi.newpipe.extractor.downloader.Response;
|
||||||
@ -22,9 +20,12 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
|
|||||||
import org.schabi.newpipe.extractor.utils.Utils;
|
import org.schabi.newpipe.extractor.utils.Utils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
@SuppressWarnings("WeakerAccess")
|
||||||
public class YoutubePlaylistExtractor extends PlaylistExtractor {
|
public class YoutubePlaylistExtractor extends PlaylistExtractor {
|
||||||
@ -95,7 +96,11 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNextPageUrl() throws ExtractionException {
|
public String getNextPageUrl() throws ExtractionException {
|
||||||
return getNextPageUrlFrom(doc);
|
return getNextPageUrlFrom(initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
|
||||||
|
.getArray("tabs").getObject(0).getObject("tabRenderer").getObject("content")
|
||||||
|
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
||||||
|
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
|
||||||
|
.getObject("playlistVideoListRenderer").getArray("continuations"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -174,8 +179,14 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
|
|||||||
@Override
|
@Override
|
||||||
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
|
||||||
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
||||||
Element tbody = doc.select("tbody[id=\"pl-load-more-destination\"]").first();
|
|
||||||
collectStreamsFrom(collector, tbody);
|
JsonArray videos = initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
|
||||||
|
.getArray("tabs").getObject(0).getObject("tabRenderer").getObject("content")
|
||||||
|
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
||||||
|
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
|
||||||
|
.getObject("playlistVideoListRenderer").getArray("contents");
|
||||||
|
|
||||||
|
collectStreamsFrom(collector, videos);
|
||||||
return new InfoItemsPage<>(collector, getNextPageUrl());
|
return new InfoItemsPage<>(collector, getNextPageUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,58 +197,42 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
||||||
JsonObject pageJson;
|
JsonArray ajaxJson;
|
||||||
try {
|
try {
|
||||||
final String responseBody = getDownloader().get(pageUrl, getExtractorLocalization()).responseBody();
|
Map<String, List<String>> headers = new HashMap<>();
|
||||||
pageJson = JsonParser.object().from(responseBody);
|
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
|
||||||
|
headers.put("X-YouTube-Client-Version", Collections.singletonList("2.20200221.03.00")); // TODO: Automatically get YouTube client version somehow
|
||||||
|
final String response = getDownloader().get(pageUrl, headers, getExtractorLocalization()).responseBody();
|
||||||
|
ajaxJson = JsonParser.array().from(response);
|
||||||
} catch (JsonParserException pe) {
|
} catch (JsonParserException pe) {
|
||||||
throw new ParsingException("Could not parse ajax json", pe);
|
throw new ParsingException("Could not parse json data for next streams", pe);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Document pageHtml = Jsoup.parse("<table><tbody id=\"pl-load-more-destination\">"
|
JsonObject sectionListContinuation = ajaxJson.getObject(1).getObject("response")
|
||||||
+ pageJson.getString("content_html")
|
.getObject("continuationContents").getObject("playlistVideoListContinuation");
|
||||||
+ "</tbody></table>", pageUrl);
|
|
||||||
|
|
||||||
collectStreamsFrom(collector, pageHtml.select("tbody[id=\"pl-load-more-destination\"]").first());
|
collectStreamsFrom(collector, sectionListContinuation.getArray("contents"));
|
||||||
|
|
||||||
return new InfoItemsPage<>(collector, getNextPageUrlFromAjax(pageJson, pageUrl));
|
return new InfoItemsPage<>(collector, getNextPageUrlFrom(sectionListContinuation.getArray("continuations")));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getNextPageUrlFromAjax(final JsonObject pageJson, final String pageUrl)
|
private String getNextPageUrlFrom(JsonArray continuations) {
|
||||||
throws ParsingException {
|
if (continuations == null) {
|
||||||
String nextPageHtml = pageJson.getString("load_more_widget_html");
|
|
||||||
if (!nextPageHtml.isEmpty()) {
|
|
||||||
return getNextPageUrlFrom(Jsoup.parse(nextPageHtml, pageUrl));
|
|
||||||
} else {
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData");
|
||||||
|
String continuation = nextContinuationData.getString("continuation");
|
||||||
|
String clickTrackingParams = nextContinuationData.getString("clickTrackingParams");
|
||||||
|
return "https://www.youtube.com/browse_ajax?ctoken=" + continuation + "&continuation=" + continuation
|
||||||
|
+ "&itct=" + clickTrackingParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getNextPageUrlFrom(Document d) throws ParsingException {
|
private void collectStreamsFrom(StreamInfoItemsCollector collector, JsonArray videos) {
|
||||||
try {
|
|
||||||
Element button = d.select("button[class*=\"yt-uix-load-more\"]").first();
|
|
||||||
if (button != null) {
|
|
||||||
return button.attr("abs:data-uix-load-more-href");
|
|
||||||
} else {
|
|
||||||
// Sometimes playlists are simply so small, they don't have a more streams/videos
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ParsingException("could not get next streams' url", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void collectStreamsFrom(@Nonnull StreamInfoItemsCollector collector, @Nullable Element element) {
|
|
||||||
collector.reset();
|
collector.reset();
|
||||||
|
|
||||||
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
||||||
|
|
||||||
JsonArray videos = initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
|
|
||||||
.getArray("tabs").getObject(0).getObject("tabRenderer").getObject("content")
|
|
||||||
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
|
||||||
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
|
|
||||||
.getObject("playlistVideoListRenderer").getArray("contents");
|
|
||||||
|
|
||||||
for (Object video : videos) {
|
for (Object video : videos) {
|
||||||
if (((JsonObject) video).getObject("playlistVideoRenderer") != null) {
|
if (((JsonObject) video).getObject("playlistVideoRenderer") != null) {
|
||||||
collector.commit(new YoutubeStreamInfoItemExtractor(((JsonObject) video).getObject("playlistVideoRenderer"), timeAgoParser) {
|
collector.commit(new YoutubeStreamInfoItemExtractor(((JsonObject) video).getObject("playlistVideoRenderer"), timeAgoParser) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user