[YouTube] Move channel age-gate renderer extraction method

The method has been moved from YoutubeChannelExtractor to YoutubeChannelHelper
and has been made public in order to be used for channel and channel tabs
extraction.
This commit is contained in:
AudricV 2023-08-27 15:01:32 +02:00
parent 1f08d28ae5
commit 155960f1d8
No known key found for this signature in database
GPG Key ID: DA92EC7905614198
2 changed files with 44 additions and 31 deletions

View File

@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
@ -320,4 +321,46 @@ public final class YoutubeChannelHelper {
return Optional.empty();
}
}
/**
* Get a {@code channelAgeGateRenderer} object from a channel response if it exists.
*
* <p>
* A {@code channelAgeGateRenderer} is returned when a channel is age-restricted (creator seems
* to be able to set this setting), its pages are only accessible to logged-in and age-verified
* users. This renderer contains only the following channel metadata: name and avatar.
* </p>
*
* <p>
* This restriction doesn't seem to apply to all countries.
* </p>
*
* <p>
* At most one {@code channelAgeGateRenderer} should be returned per age-restricted channel
* response.
* </p>
*
* @param jsonResponse a channel JSON response
* @return the first {@code channelAgeGateRenderer} if there is one present or {@code null}
*/
@Nullable
public static JsonObject getChannelAgeGateRenderer(@Nonnull final JsonObject jsonResponse) {
return jsonResponse.getObject("contents")
.getObject("twoColumnBrowseResultsRenderer")
.getArray("tabs")
.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.flatMap(tab -> tab.getObject("tabRenderer")
.getObject("content")
.getObject("sectionListRenderer")
.getArray("contents")
.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast))
.filter(content -> content.has("channelAgeGateRenderer"))
.map(content -> content.getObject("channelAgeGateRenderer"))
.findFirst()
.orElse(null);
}
}

View File

@ -66,15 +66,6 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
private String channelId;
/**
* If a channel is age-restricted, its pages are only accessible to logged-in and
* age-verified users, we get an {@code channelAgeGateRenderer} in this case, containing only
* the following metadata: channel name and channel avatar.
*
* <p>
* This restriction doesn't seem to apply to all countries.
* </p>
*/
@Nullable
private JsonObject channelAgeGateRenderer;
@ -95,28 +86,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
jsonResponse = data.jsonResponse;
channelHeader = YoutubeChannelHelper.getChannelHeader(jsonResponse);
channelId = data.channelId;
channelAgeGateRenderer = getChannelAgeGateRenderer();
}
@Nullable
private JsonObject getChannelAgeGateRenderer() {
return jsonResponse.getObject("contents")
.getObject("twoColumnBrowseResultsRenderer")
.getArray("tabs")
.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.flatMap(tab -> tab.getObject("tabRenderer")
.getObject("content")
.getObject("sectionListRenderer")
.getArray("contents")
.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast))
.filter(content -> content.has("channelAgeGateRenderer"))
.map(content -> content.getObject("channelAgeGateRenderer"))
.findFirst()
.orElse(null);
channelAgeGateRenderer = YoutubeChannelHelper.getChannelAgeGateRenderer(jsonResponse);
}
@Nonnull