[Youtube] Extract initial playlist info

This commit is contained in:
Xiang Rong Lin 2020-03-06 20:40:40 +01:00 committed by XiangRongLin
parent a376792a5d
commit 0ff054acb4
1 changed files with 83 additions and 73 deletions

View File

@ -1,5 +1,9 @@
package org.schabi.newpipe.extractor.services.youtube.extractors;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getJsonResponse;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import java.io.IOException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -24,6 +28,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
*/
public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
private JsonObject initialData;
private Document doc;
public YoutubeMixPlaylistExtractor(StreamingService service, ListLinkHandler linkHandler) {
@ -32,9 +37,14 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
@Override
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
final String url = getUrl();
final Response response = downloader.get(url, getExtractorLocalization());
doc = YoutubeParsingHelper.parseAndCheckPage(url, response);
final String url = getUrl() + "&pbj=1";
final JsonArray ajaxJson = getJsonResponse(url, getExtractorLocalization());
initialData = ajaxJson.getObject(3).getObject("response");
JsonObject a = initialData.getObject("contents");
JsonObject b = initialData.getObject("contents").getObject("twoColumnWatchNextResults");
JsonObject c = initialData.getObject("contents");
JsonObject playlist = initialData.getObject("contents").getObject("twoColumnWatchNextResults").getObject("playlist").getObject("playlist");
System.out.println();
}
@Nonnull
@ -127,76 +137,76 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
final LinkHandlerFactory streamLinkHandlerFactory = getService().getStreamLHFactory();
final TimeAgoParser timeAgoParser = getTimeAgoParser();
for (final Element li : element.children()) {
collector.commit(new YoutubeStreamInfoItemExtractor(li, timeAgoParser) {
@Override
public boolean isAd() {
return false;
}
@Override
public String getUrl() throws ParsingException {
try {
return streamLinkHandlerFactory.fromId(li.attr("data-video-id")).getUrl();
} catch (Exception e) {
throw new ParsingException("Could not get web page url for the video", e);
}
}
@Override
public String getName() throws ParsingException {
try {
return li.attr("data-video-title");
} catch (Exception e) {
throw new ParsingException("Could not get name", e);
}
}
@Override
public long getDuration() {
//Not present in doc
return 0;
}
@Override
public String getUploaderName() throws ParsingException {
String uploaderName = li.attr("data-video-username");
if (uploaderName == null || uploaderName.isEmpty()) {
throw new ParsingException("Could not get uploader name");
} else {
return uploaderName;
}
}
@Override
public String getUploaderUrl() {
//Not present in doc
return "";
}
@Override
public String getTextualUploadDate() {
//Not present in doc
return "";
}
@Override
public long getViewCount() {
return -1;
}
@Override
public String getThumbnailUrl() throws ParsingException {
try {
return getThumbnailUrlFromId(streamLinkHandlerFactory.fromUrl(getUrl()).getId());
} catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e);
}
}
});
}
// for (final Element li : element.children()) {
//
// collector.commit(new YoutubeStreamInfoItemExtractor(li, timeAgoParser) {
//
// @Override
// public boolean isAd() {
// return false;
// }
//
// @Override
// public String getUrl() throws ParsingException {
// try {
// return streamLinkHandlerFactory.fromId(li.attr("data-video-id")).getUrl();
// } catch (Exception e) {
// throw new ParsingException("Could not get web page url for the video", e);
// }
// }
//
// @Override
// public String getName() throws ParsingException {
// try {
// return li.attr("data-video-title");
// } catch (Exception e) {
// throw new ParsingException("Could not get name", e);
// }
// }
//
// @Override
// public long getDuration() {
// //Not present in doc
// return 0;
// }
//
// @Override
// public String getUploaderName() throws ParsingException {
// String uploaderName = li.attr("data-video-username");
// if (uploaderName == null || uploaderName.isEmpty()) {
// throw new ParsingException("Could not get uploader name");
// } else {
// return uploaderName;
// }
// }
//
// @Override
// public String getUploaderUrl() {
// //Not present in doc
// return "";
// }
//
// @Override
// public String getTextualUploadDate() {
// //Not present in doc
// return "";
// }
//
// @Override
// public long getViewCount() {
// return -1;
// }
//
// @Override
// public String getThumbnailUrl() throws ParsingException {
// try {
// return getThumbnailUrlFromId(streamLinkHandlerFactory.fromUrl(getUrl()).getId());
// } catch (Exception e) {
// throw new ParsingException("Could not get thumbnail url", e);
// }
// }
// });
// }
}
private String getThumbnailUrlFromId(String videoId) {