2020-02-02 14:19:48 +01:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
|
|
|
|
2020-03-06 20:40:40 +01:00
|
|
|
import com.grack.nanojson.JsonArray;
|
|
|
|
import com.grack.nanojson.JsonObject;
|
2020-03-17 14:04:46 +01:00
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.ListExtractor;
|
2020-02-02 14:19:48 +01:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
|
|
|
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
|
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
|
|
|
|
|
2020-05-21 14:52:13 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse;
|
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
|
|
|
|
|
2020-02-02 18:15:47 +01:00
|
|
|
/**
|
2020-03-07 16:03:12 +01:00
|
|
|
* A YoutubePlaylistExtractor for a mix (auto-generated playlist). It handles urls in the format of
|
|
|
|
* "youtube.com/watch?v=videoId&list=playlistId"
|
2020-02-02 18:15:47 +01:00
|
|
|
*/
|
2020-02-02 14:19:48 +01:00
|
|
|
public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
|
|
|
|
|
2020-03-21 18:48:12 +01:00
|
|
|
private JsonObject initialData;
|
2020-03-07 16:03:12 +01:00
|
|
|
private JsonObject playlistData;
|
|
|
|
|
|
|
|
public YoutubeMixPlaylistExtractor(StreamingService service, ListLinkHandler linkHandler) {
|
|
|
|
super(service, linkHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFetchPage(@Nonnull Downloader downloader)
|
|
|
|
throws IOException, ExtractionException {
|
|
|
|
final String url = getUrl() + "&pbj=1";
|
|
|
|
final JsonArray ajaxJson = getJsonResponse(url, getExtractorLocalization());
|
2020-03-21 18:48:12 +01:00
|
|
|
initialData = ajaxJson.getObject(3).getObject("response");
|
|
|
|
playlistData = initialData.getObject("contents").getObject("twoColumnWatchNextResults")
|
|
|
|
.getObject("playlist").getObject("playlist");
|
2020-03-07 16:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getName() throws ParsingException {
|
2020-03-17 14:04:46 +01:00
|
|
|
final String name = playlistData.getString("title");
|
|
|
|
if (name == null) {
|
|
|
|
throw new ParsingException("Could not get playlist name");
|
2020-03-07 16:03:12 +01:00
|
|
|
}
|
2020-03-17 14:04:46 +01:00
|
|
|
return name;
|
2020-03-07 16:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
|
|
|
try {
|
|
|
|
final String playlistId = playlistData.getString("playlistId");
|
2020-03-21 18:48:12 +01:00
|
|
|
try {
|
|
|
|
return getThumbnailUrlFromPlaylistId(playlistId);
|
|
|
|
} catch (ParsingException e) {
|
|
|
|
//fallback to thumbnail of current video. Always the case for channel mix
|
|
|
|
return getThumbnailUrlFromVideoId(
|
|
|
|
initialData.getObject("currentVideoEndpoint").getObject("watchEndpoint")
|
|
|
|
.getString("videoId"));
|
|
|
|
}
|
2020-03-07 16:03:12 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get playlist thumbnail", e);
|
|
|
|
}
|
2020-02-02 14:19:48 +01:00
|
|
|
}
|
2020-03-07 16:03:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getBannerUrl() {
|
|
|
|
return "";
|
2020-02-02 14:19:48 +01:00
|
|
|
}
|
2020-03-07 16:03:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() {
|
|
|
|
//Youtube mix are auto-generated
|
|
|
|
return "";
|
2020-03-07 08:42:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 16:03:12 +01:00
|
|
|
@Override
|
|
|
|
public String getUploaderName() {
|
2020-03-17 14:04:46 +01:00
|
|
|
//Youtube mix are auto-generated by YouTube
|
|
|
|
return "YouTube";
|
2020-02-02 14:19:48 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 16:03:12 +01:00
|
|
|
@Override
|
|
|
|
public String getUploaderAvatarUrl() {
|
2020-03-17 14:04:46 +01:00
|
|
|
//Youtube mix are auto-generated by YouTube
|
2020-03-07 16:03:12 +01:00
|
|
|
return "";
|
|
|
|
}
|
2020-02-02 14:19:48 +01:00
|
|
|
|
2020-03-07 16:03:12 +01:00
|
|
|
@Override
|
|
|
|
public long getStreamCount() {
|
|
|
|
// Auto-generated playlist always start with 25 videos and are endless
|
2020-03-17 14:04:46 +01:00
|
|
|
return ListExtractor.ITEM_COUNT_INFINITE;
|
2020-03-07 16:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
|
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2020-03-21 18:48:12 +01:00
|
|
|
collectStreamsFrom(collector, playlistData.getArray("contents"));
|
2020-03-07 16:03:12 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNextPageUrl() throws ExtractionException {
|
2020-03-21 18:48:12 +01:00
|
|
|
return getNextPageUrlFrom(playlistData);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getNextPageUrlFrom(JsonObject playlistData) throws ExtractionException {
|
|
|
|
final JsonObject lastStream = ((JsonObject) playlistData.getArray("contents")
|
|
|
|
.get(playlistData.getArray("contents").size() - 1));
|
|
|
|
if (lastStream == null || lastStream.getObject("playlistPanelVideoRenderer") == null) {
|
2020-03-07 16:03:12 +01:00
|
|
|
throw new ExtractionException("Could not extract next page url");
|
|
|
|
}
|
2020-03-21 18:48:12 +01:00
|
|
|
//Index of video in mix is missing, but adding it doesn't appear to have any effect.
|
|
|
|
//And since the index needs to be tracked by us, it is left out
|
|
|
|
return getUrlFromNavigationEndpoint(
|
|
|
|
lastStream.getObject("playlistPanelVideoRenderer").getObject("navigationEndpoint"))
|
|
|
|
+ "&pbj=1";
|
2020-03-07 16:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl)
|
2020-03-17 14:04:46 +01:00
|
|
|
throws ExtractionException, IOException {
|
2020-03-07 16:03:12 +01:00
|
|
|
if (pageUrl == null || pageUrl.isEmpty()) {
|
|
|
|
throw new ExtractionException(
|
|
|
|
new IllegalArgumentException("Page url is empty or null"));
|
2020-03-07 08:42:26 +01:00
|
|
|
}
|
2020-03-07 16:03:12 +01:00
|
|
|
|
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
|
|
|
final JsonArray ajaxJson = getJsonResponse(pageUrl, getExtractorLocalization());
|
2020-03-21 18:48:12 +01:00
|
|
|
JsonObject playlistData =
|
|
|
|
ajaxJson.getObject(3).getObject("response").getObject("contents")
|
|
|
|
.getObject("twoColumnWatchNextResults").getObject("playlist")
|
|
|
|
.getObject("playlist");
|
|
|
|
final JsonArray streams = playlistData.getArray("contents");
|
2020-03-07 16:03:12 +01:00
|
|
|
//Because continuation requests are created with the last video of previous request as start
|
|
|
|
streams.remove(0);
|
|
|
|
collectStreamsFrom(collector, streams);
|
2020-03-21 18:48:12 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrlFrom(playlistData));
|
2020-03-07 08:42:26 +01:00
|
|
|
}
|
2020-02-02 18:04:15 +01:00
|
|
|
|
2020-03-07 16:03:12 +01:00
|
|
|
private void collectStreamsFrom(
|
2020-03-17 14:04:46 +01:00
|
|
|
@Nonnull StreamInfoItemsCollector collector,
|
|
|
|
@Nullable JsonArray streams) {
|
2020-03-07 16:03:12 +01:00
|
|
|
|
|
|
|
if (streams == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
|
|
|
|
|
|
|
for (Object stream : streams) {
|
|
|
|
if (stream instanceof JsonObject) {
|
|
|
|
JsonObject streamInfo = ((JsonObject) stream)
|
2020-03-21 18:48:12 +01:00
|
|
|
.getObject("playlistPanelVideoRenderer");
|
2020-03-07 16:03:12 +01:00
|
|
|
if (streamInfo != null) {
|
|
|
|
collector.commit(new YoutubeStreamInfoItemExtractor(streamInfo, timeAgoParser));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 18:48:12 +01:00
|
|
|
private String getThumbnailUrlFromPlaylistId(String playlistId) throws ParsingException {
|
2020-03-17 14:04:46 +01:00
|
|
|
final String videoId;
|
|
|
|
if (playlistId.startsWith("RDMM")) {
|
|
|
|
videoId = playlistId.substring(4);
|
2020-03-21 18:48:12 +01:00
|
|
|
} else if (playlistId.startsWith("RDCMUC")) {
|
|
|
|
throw new ParsingException("is channel mix");
|
2020-03-17 14:04:46 +01:00
|
|
|
} else {
|
|
|
|
videoId = playlistId.substring(2);
|
|
|
|
}
|
|
|
|
if (videoId.isEmpty()) {
|
|
|
|
throw new ParsingException("videoId is empty");
|
|
|
|
}
|
2020-03-21 18:48:12 +01:00
|
|
|
return getThumbnailUrlFromVideoId(videoId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getThumbnailUrlFromVideoId(String videoId) {
|
2020-03-07 16:03:12 +01:00
|
|
|
return "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg";
|
|
|
|
}
|
2020-05-21 14:52:13 +02:00
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getSubChannelName() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getSubChannelUrl() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getSubChannelAvatarUrl() {
|
|
|
|
return "";
|
|
|
|
}
|
2020-02-02 14:19:48 +01:00
|
|
|
}
|