[YouTube] Simplify usage of channel header json
This commit is contained in:
parent
a104cf3227
commit
ef67c7cd74
|
@ -57,6 +57,7 @@ import javax.annotation.Nullable;
|
||||||
public class YoutubeChannelExtractor extends ChannelExtractor {
|
public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
|
|
||||||
private JsonObject jsonResponse;
|
private JsonObject jsonResponse;
|
||||||
|
|
||||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||||
private Optional<YoutubeChannelHelper.ChannelHeader> channelHeader;
|
private Optional<YoutubeChannelHelper.ChannelHeader> channelHeader;
|
||||||
|
|
||||||
|
@ -89,6 +90,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
"EgZ2aWRlb3PyBgQKAjoA", getExtractorLocalization(), getExtractorContentCountry());
|
"EgZ2aWRlb3PyBgQKAjoA", getExtractorLocalization(), getExtractorContentCountry());
|
||||||
|
|
||||||
jsonResponse = data.jsonResponse;
|
jsonResponse = data.jsonResponse;
|
||||||
|
channelHeader = YoutubeChannelHelper.getChannelHeader(jsonResponse);
|
||||||
channelId = data.channelId;
|
channelId = data.channelId;
|
||||||
channelAgeGateRenderer = getChannelAgeGateRenderer();
|
channelAgeGateRenderer = getChannelAgeGateRenderer();
|
||||||
}
|
}
|
||||||
|
@ -115,12 +117,8 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private Optional<YoutubeChannelHelper.ChannelHeader> getChannelHeader() {
|
private Optional<JsonObject> getChannelHeaderJson() {
|
||||||
//noinspection OptionalAssignedToNull
|
return channelHeader.map(it -> it.json);
|
||||||
if (channelHeader == null) {
|
|
||||||
channelHeader = YoutubeChannelHelper.getChannelHeader(jsonResponse);
|
|
||||||
}
|
|
||||||
return channelHeader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
@ -136,9 +134,9 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public String getId() throws ParsingException {
|
public String getId() throws ParsingException {
|
||||||
return getChannelHeader()
|
return getChannelHeaderJson()
|
||||||
.flatMap(header -> Optional.ofNullable(header.json.getString("channelId"))
|
.flatMap(header -> Optional.ofNullable(header.getString("channelId"))
|
||||||
.or(() -> Optional.ofNullable(header.json.getObject("navigationEndpoint")
|
.or(() -> Optional.ofNullable(header.getObject("navigationEndpoint")
|
||||||
.getObject("browseEndpoint")
|
.getObject("browseEndpoint")
|
||||||
.getString("browseId"))
|
.getString("browseId"))
|
||||||
))
|
))
|
||||||
|
@ -160,8 +158,8 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
return metadataRendererTitle;
|
return metadataRendererTitle;
|
||||||
}
|
}
|
||||||
|
|
||||||
return getChannelHeader().flatMap(header -> {
|
return getChannelHeaderJson().flatMap(header -> {
|
||||||
final Object title = header.json.get("title");
|
final Object title = header.get("title");
|
||||||
if (title instanceof String) {
|
if (title instanceof String) {
|
||||||
return Optional.of((String) title);
|
return Optional.of((String) title);
|
||||||
} else if (title instanceof JsonObject) {
|
} else if (title instanceof JsonObject) {
|
||||||
|
@ -180,7 +178,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
if (channelAgeGateRenderer != null) {
|
if (channelAgeGateRenderer != null) {
|
||||||
avatarJsonObjectContainer = channelAgeGateRenderer;
|
avatarJsonObjectContainer = channelAgeGateRenderer;
|
||||||
} else {
|
} else {
|
||||||
avatarJsonObjectContainer = getChannelHeader().map(header -> header.json)
|
avatarJsonObjectContainer = getChannelHeaderJson()
|
||||||
.orElseThrow(() -> new ParsingException("Could not get avatar URL"));
|
.orElseThrow(() -> new ParsingException("Could not get avatar URL"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,8 +194,8 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
return getChannelHeader().flatMap(header -> Optional.ofNullable(
|
return getChannelHeaderJson().flatMap(header -> Optional.ofNullable(
|
||||||
header.json.getObject("banner")
|
header.getObject("banner")
|
||||||
.getArray("thumbnails")
|
.getArray("thumbnails")
|
||||||
.getObject(0)
|
.getObject(0)
|
||||||
.getString("url")))
|
.getString("url")))
|
||||||
|
@ -226,9 +224,9 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
return UNKNOWN_SUBSCRIBER_COUNT;
|
return UNKNOWN_SUBSCRIBER_COUNT;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Optional<YoutubeChannelHelper.ChannelHeader> headerOpt = getChannelHeader();
|
final Optional<JsonObject> headerOpt = getChannelHeaderJson();
|
||||||
if (headerOpt.isPresent()) {
|
if (headerOpt.isPresent()) {
|
||||||
final JsonObject header = headerOpt.get().json;
|
final JsonObject header = headerOpt.get();
|
||||||
JsonObject textObject = null;
|
JsonObject textObject = null;
|
||||||
|
|
||||||
if (header.has("subscriberCountText")) {
|
if (header.has("subscriberCountText")) {
|
||||||
|
@ -285,9 +283,8 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Optional<YoutubeChannelHelper.ChannelHeader> headerOpt = getChannelHeader();
|
if (channelHeader.isPresent()) {
|
||||||
if (headerOpt.isPresent()) {
|
final YoutubeChannelHelper.ChannelHeader header = channelHeader.get();
|
||||||
final YoutubeChannelHelper.ChannelHeader header = headerOpt.get();
|
|
||||||
|
|
||||||
// The CarouselHeaderRenderer does not contain any verification badges.
|
// The CarouselHeaderRenderer does not contain any verification badges.
|
||||||
// Since it is only shown on YT-internal channels or on channels of large organizations
|
// Since it is only shown on YT-internal channels or on channels of large organizations
|
||||||
|
|
Loading…
Reference in New Issue