Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YouTube]: Fixed channel continuation #539

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import javax.annotation.Nonnull;
import java.io.IOException;

import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

Expand Down Expand Up @@ -230,9 +228,9 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException
.getArray("contents").getObject(0).getObject("itemSectionRenderer")
.getArray("contents").getObject(0).getObject("gridRenderer");

collectStreamsFrom(collector, gridRenderer.getArray("items"));
final JsonObject continuation = collectStreamsFrom(collector, gridRenderer.getArray("items"));

nextPage = getNextPageFrom(gridRenderer.getArray("continuations"));
nextPage = getNextPageFrom(continuation);
}

return new InfoItemsPage<>(collector, nextPage);
Expand All @@ -252,36 +250,47 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
final JsonArray ajaxJson = getJsonResponse(page.getUrl(), getExtractorLocalization());

JsonObject sectionListContinuation = ajaxJson.getObject(1).getObject("response")
.getObject("continuationContents").getObject("gridContinuation");
.getArray("onResponseReceivedActions").getObject(0).getObject("appendContinuationItemsAction");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it safe to assume that the appendContinuationItemsAction object is always in the first array element?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

youtube-dl also assume it's the first element. https://github.com/ytdl-org/youtube-dl/blob/master/youtube_dl/extractor/youtube.py#L2507
but if you wanna be sure the safest way is a simple for loop.


collectStreamsFrom(collector, sectionListContinuation.getArray("items"));
final JsonObject continuation = collectStreamsFrom(collector, sectionListContinuation.getArray("continuationItems"));

return new InfoItemsPage<>(collector, getNextPageFrom(sectionListContinuation.getArray("continuations")));
return new InfoItemsPage<>(collector, getNextPageFrom(continuation));
}

private Page getNextPageFrom(final JsonArray continuations) {
private Page getNextPageFrom(final JsonObject continuations) {
if (isNullOrEmpty(continuations)) {
return null;
}

final JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData");
final String continuation = nextContinuationData.getString("continuation");
final String clickTrackingParams = nextContinuationData.getString("clickTrackingParams");
final JsonObject continuationEndpoint = continuations.getObject("continuationEndpoint");
final String continuation = continuationEndpoint.getObject("continuationCommand").getString("token");
final String clickTrackingParams = continuationEndpoint.getString("clickTrackingParams");
return new Page("https://www.youtube.com/browse_ajax?ctoken=" + continuation
+ "&continuation=" + continuation + "&itct=" + clickTrackingParams);
}

private void collectStreamsFrom(StreamInfoItemsCollector collector, JsonArray videos) throws ParsingException {
/**
* Collect streams from an array of items
*
* @param collector the collector where videos will be commited
* @param videos the array to get videos from
* @return the continuation object
* @throws ParsingException if an error happened while extracting
*/
private JsonObject collectStreamsFrom(StreamInfoItemsCollector collector, JsonArray videos) throws ParsingException {
collector.reset();

final String uploaderName = getName();
final String uploaderUrl = getUrl();
final TimeAgoParser timeAgoParser = getTimeAgoParser();

for (Object video : videos) {
if (((JsonObject) video).has("gridVideoRenderer")) {
JsonObject continuation = null;

for (Object object : videos) {
final JsonObject video = (JsonObject) object;
if (video.has("gridVideoRenderer")) {
collector.commit(new YoutubeStreamInfoItemExtractor(
((JsonObject) video).getObject("gridVideoRenderer"), timeAgoParser) {
video.getObject("gridVideoRenderer"), timeAgoParser) {
@Override
public String getUploaderName() {
return uploaderName;
Expand All @@ -292,8 +301,12 @@ public String getUploaderUrl() {
return uploaderUrl;
}
});
} else if (video.has("continuationItemRenderer")) {
continuation = video.getObject("continuationItemRenderer");
}
}

return continuation;
}

private JsonObject getVideoTab() throws ParsingException {
Expand Down