2018-05-08 21:19:03 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2020-02-17 18:58:12 +01:00
|
|
|
import com.grack.nanojson.JsonArray;
|
2017-08-16 04:40:03 +02:00
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2020-02-22 20:19:41 +01:00
|
|
|
|
2017-06-29 20:12:55 +02:00
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
2018-09-04 03:37:31 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
|
|
|
import org.schabi.newpipe.extractor.downloader.Response;
|
2017-06-29 20:12:55 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2018-07-13 18:02:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
|
2017-06-29 20:12:55 +02:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
|
2018-07-13 18:02:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
|
2018-03-01 01:02:43 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
2018-02-24 22:20:50 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
|
2017-07-11 05:08:03 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2020-02-22 20:19:41 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2018-09-04 03:37:31 +02:00
|
|
|
import javax.annotation.Nullable;
|
2017-06-29 20:12:55 +02:00
|
|
|
|
|
|
|
@SuppressWarnings("WeakerAccess")
|
|
|
|
public class YoutubePlaylistExtractor extends PlaylistExtractor {
|
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
private Document doc;
|
2020-02-17 18:58:12 +01:00
|
|
|
private JsonObject initialData;
|
|
|
|
private JsonObject uploaderInfo;
|
|
|
|
private JsonObject playlistInfo;
|
|
|
|
private JsonObject playlistVideos;
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
public YoutubePlaylistExtractor(StreamingService service, ListLinkHandler linkHandler) {
|
|
|
|
super(service, linkHandler);
|
2017-08-06 22:20:15 +02:00
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2017-08-06 22:20:15 +02:00
|
|
|
@Override
|
2017-11-28 13:37:01 +01:00
|
|
|
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
|
2019-10-29 06:00:29 +01:00
|
|
|
final String url = getUrl();
|
2019-04-28 22:03:16 +02:00
|
|
|
final Response response = downloader.get(url, getExtractorLocalization());
|
2019-10-29 06:00:29 +01:00
|
|
|
doc = YoutubeParsingHelper.parseAndCheckPage(url, response);
|
2020-02-22 23:51:02 +01:00
|
|
|
initialData = YoutubeParsingHelper.getInitialData(response.responseBody());
|
2020-02-17 18:58:12 +01:00
|
|
|
uploaderInfo = getUploaderInfo();
|
|
|
|
playlistInfo = getPlaylistInfo();
|
|
|
|
playlistVideos = getPlaylistVideos();
|
|
|
|
}
|
|
|
|
|
|
|
|
private JsonObject getUploaderInfo() throws ParsingException {
|
|
|
|
JsonArray items = initialData.getObject("sidebar").getObject("playlistSidebarRenderer").getArray("items");
|
|
|
|
try {
|
|
|
|
JsonObject uploaderInfo = items.getObject(1).getObject("playlistSidebarSecondaryInfoRenderer")
|
|
|
|
.getObject("videoOwner").getObject("videoOwnerRenderer");
|
|
|
|
if (uploaderInfo != null) {
|
|
|
|
return uploaderInfo;
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
|
|
|
|
// we might want to create a loop here instead of using duplicated code
|
|
|
|
try {
|
|
|
|
JsonObject uploaderInfo = items.getObject(items.size()).getObject("playlistSidebarSecondaryInfoRenderer")
|
|
|
|
.getObject("videoOwner").getObject("videoOwnerRenderer");
|
|
|
|
if (uploaderInfo != null) {
|
|
|
|
return uploaderInfo;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get uploader info", e);
|
|
|
|
}
|
|
|
|
throw new ParsingException("Could not get uploader info");
|
|
|
|
}
|
|
|
|
|
|
|
|
private JsonObject getPlaylistInfo() throws ParsingException {
|
|
|
|
try {
|
|
|
|
return initialData.getObject("sidebar").getObject("playlistSidebarRenderer").getArray("items")
|
|
|
|
.getObject(0).getObject("playlistSidebarPrimaryInfoRenderer");
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get PlaylistInfo", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private JsonObject getPlaylistVideos() throws ParsingException {
|
|
|
|
try {
|
|
|
|
return 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");
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get playlist info", e);
|
|
|
|
}
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNextPageUrl() throws ExtractionException {
|
|
|
|
return getNextPageUrlFrom(doc);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2017-06-29 20:12:55 +02:00
|
|
|
@Override
|
2017-08-11 03:23:09 +02:00
|
|
|
public String getName() throws ParsingException {
|
2017-06-29 20:12:55 +02:00
|
|
|
try {
|
2020-02-17 18:58:12 +01:00
|
|
|
String name = playlistInfo.getObject("title").getArray("runs").getObject(0).getString("text");
|
|
|
|
if (name != null) return name;
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
try {
|
|
|
|
return initialData.getObject("microformat").getObject("microformatDataRenderer").getString("title");
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (Exception e) {
|
2019-08-16 21:17:03 +02:00
|
|
|
throw new ParsingException("Could not get playlist name", e);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-08 23:36:11 +02:00
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
2017-06-29 20:12:55 +02:00
|
|
|
try {
|
2020-02-17 18:58:12 +01:00
|
|
|
return playlistInfo.getObject("thumbnailRenderer").getObject("playlistVideoThumbnailRenderer")
|
|
|
|
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
|
|
|
|
} catch (Exception ignored) {}
|
|
|
|
try {
|
|
|
|
return initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail")
|
|
|
|
.getArray("thumbnails").getObject(0).getString("url");
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (Exception e) {
|
2019-08-16 21:17:03 +02:00
|
|
|
throw new ParsingException("Could not get playlist thumbnail", e);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-12 16:14:33 +01:00
|
|
|
public String getBannerUrl() {
|
|
|
|
return ""; // Banner can't be handled by frontend right now.
|
2020-02-16 23:59:31 +01:00
|
|
|
// Whoever is willing to implement this should also implement it in the frontend.
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() throws ParsingException {
|
|
|
|
try {
|
2019-08-16 21:17:03 +02:00
|
|
|
return YoutubeChannelExtractor.CHANNEL_URL_BASE +
|
2020-02-17 18:58:12 +01:00
|
|
|
uploaderInfo.getObject("navigationEndpoint").getObject("browseEndpoint").getString("browseId");
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (Exception e) {
|
2019-08-16 21:17:03 +02:00
|
|
|
throw new ParsingException("Could not get playlist uploader url", e);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderName() throws ParsingException {
|
|
|
|
try {
|
2020-02-17 18:58:12 +01:00
|
|
|
return uploaderInfo.getObject("title").getArray("runs").getObject(0).getString("text");
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (Exception e) {
|
2019-08-16 21:17:03 +02:00
|
|
|
throw new ParsingException("Could not get playlist uploader name", e);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderAvatarUrl() throws ParsingException {
|
|
|
|
try {
|
2020-02-17 18:58:12 +01:00
|
|
|
return uploaderInfo.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
|
2017-06-29 20:12:55 +02:00
|
|
|
} catch (Exception e) {
|
2019-08-16 21:17:03 +02:00
|
|
|
throw new ParsingException("Could not get playlist uploader avatar", e);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-06 22:20:15 +02:00
|
|
|
public long getStreamCount() throws ParsingException {
|
|
|
|
try {
|
2020-02-17 18:58:12 +01:00
|
|
|
String viewsText = getPlaylistInfo().getArray("stats").getObject(0).getArray("runs").getObject(0).getString("text");
|
|
|
|
return Long.parseLong(Utils.removeNonDigitCharacters(viewsText));
|
|
|
|
} catch (Exception e) {
|
2017-08-06 22:20:15 +02:00
|
|
|
throw new ParsingException("Could not get video count from playlist", e);
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 02:03:30 +01:00
|
|
|
@Nonnull
|
2017-06-29 20:12:55 +02:00
|
|
|
@Override
|
2018-08-28 14:33:05 +02:00
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
|
2018-02-24 22:20:50 +01:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2017-06-29 20:12:55 +02:00
|
|
|
Element tbody = doc.select("tbody[id=\"pl-load-more-destination\"]").first();
|
|
|
|
collectStreamsFrom(collector, tbody);
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrl());
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
|
2018-03-01 01:02:43 +01:00
|
|
|
if (pageUrl == null || pageUrl.isEmpty()) {
|
|
|
|
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
|
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2018-03-01 01:02:43 +01:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
|
|
|
JsonObject pageJson;
|
|
|
|
try {
|
2019-04-28 22:03:16 +02:00
|
|
|
final String responseBody = getDownloader().get(pageUrl, getExtractorLocalization()).responseBody();
|
|
|
|
pageJson = JsonParser.object().from(responseBody);
|
2018-02-26 15:55:27 +01:00
|
|
|
} catch (JsonParserException pe) {
|
|
|
|
throw new ParsingException("Could not parse ajax json", pe);
|
|
|
|
}
|
2018-03-01 01:02:43 +01:00
|
|
|
|
|
|
|
final Document pageHtml = Jsoup.parse("<table><tbody id=\"pl-load-more-destination\">"
|
|
|
|
+ pageJson.getString("content_html")
|
|
|
|
+ "</tbody></table>", pageUrl);
|
|
|
|
|
|
|
|
collectStreamsFrom(collector, pageHtml.select("tbody[id=\"pl-load-more-destination\"]").first());
|
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrlFromAjax(pageJson, pageUrl));
|
2018-02-26 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private String getNextPageUrlFromAjax(final JsonObject pageJson, final String pageUrl)
|
2020-02-08 23:58:46 +01:00
|
|
|
throws ParsingException {
|
2018-02-26 15:55:27 +01:00
|
|
|
String nextPageHtml = pageJson.getString("load_more_widget_html");
|
|
|
|
if (!nextPageHtml.isEmpty()) {
|
|
|
|
return getNextPageUrlFrom(Jsoup.parse(nextPageHtml, pageUrl));
|
|
|
|
} else {
|
|
|
|
return "";
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:20:50 +01:00
|
|
|
private String getNextPageUrlFrom(Document d) throws ParsingException {
|
2017-06-29 20:12:55 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 03:37:31 +02:00
|
|
|
private void collectStreamsFrom(@Nonnull StreamInfoItemsCollector collector, @Nullable Element element) {
|
2017-11-11 12:17:14 +01:00
|
|
|
collector.reset();
|
2017-06-29 20:12:55 +02:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
|
|
|
|
2020-02-23 13:48:54 +01:00
|
|
|
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) {
|
|
|
|
if (((JsonObject) video).getObject("playlistVideoRenderer") != null) {
|
|
|
|
collector.commit(new YoutubeStreamInfoItemExtractor(((JsonObject) video).getObject("playlistVideoRenderer"), timeAgoParser) {
|
|
|
|
@Override
|
|
|
|
public long getViewCount() {
|
|
|
|
return -1;
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
2020-02-23 13:48:54 +01:00
|
|
|
});
|
|
|
|
}
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|