Skip to content

Commit 5879190

Browse files
committed
[YouTube] Move channel header's verified status code to YoutubeChannelHelper
Also throw an exception when we cannot get the verified status of a channel in YoutubeChannelExtractor due to a missing channelHeader, if the channel has no channelAgeGateRenderer.
1 parent 9fa8d4c commit 5879190

2 files changed

Lines changed: 34 additions & 22 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelHelper.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,35 @@ public static Optional<ChannelHeader> getChannelHeader(
320320
return Optional.empty();
321321
}
322322
}
323+
324+
/**
325+
* Check if a channel is verified by using its header.
326+
*
327+
* <p>
328+
* The header is mandatory, so the verified status of age-restricted channels with a
329+
* {@code channelAgeGateRenderer} cannot be checked.
330+
* </p>
331+
*
332+
* @param channelHeader the {@link ChannelHeader} of a non age-restricted channel
333+
* @return whether the channel is verified
334+
*/
335+
public static boolean isChannelVerified(@Nonnull final ChannelHeader channelHeader) {
336+
// carouselHeaderRenderer and pageHeaderRenderer does not contain any verification
337+
// badges
338+
// Since they are only shown on YouTube internal channels or on channels of large
339+
// organizations broadcasting live events, we can assume the channel to be verified
340+
if (channelHeader.headerType == ChannelHeader.HeaderType.CAROUSEL
341+
|| channelHeader.headerType == ChannelHeader.HeaderType.PAGE) {
342+
return true;
343+
}
344+
345+
if (channelHeader.headerType == ChannelHeader.HeaderType.INTERACTIVE_TABBED) {
346+
// If the header has an autoGenerated property, it should mean that the channel has
347+
// been auto generated by YouTube: we can assume the channel to be verified in this
348+
// case
349+
return channelHeader.json.has("autoGenerated");
350+
}
351+
352+
return YoutubeParsingHelper.isVerified(channelHeader.json.getArray("badges"));
353+
}
323354
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -350,31 +350,12 @@ public List<Image> getParentChannelAvatars() {
350350
public boolean isVerified() throws ParsingException {
351351
assertPageFetched();
352352
if (channelAgeGateRenderer != null) {
353+
// Verified status is unknown with channelAgeGateRenderers, return false in this case
353354
return false;
354355
}
355356

356-
if (channelHeader.isPresent()) {
357-
final ChannelHeader header = channelHeader.get();
358-
359-
// carouselHeaderRenderer and pageHeaderRenderer does not contain any verification
360-
// badges
361-
// Since they are only shown on YouTube internal channels or on channels of large
362-
// organizations broadcasting live events, we can assume the channel to be verified
363-
if (header.headerType == HeaderType.CAROUSEL || header.headerType == HeaderType.PAGE) {
364-
return true;
365-
}
366-
367-
if (header.headerType == HeaderType.INTERACTIVE_TABBED) {
368-
// If the header has an autoGenerated property, it should mean that the channel has
369-
// been auto generated by YouTube: we can assume the channel to be verified in this
370-
// case
371-
return header.json.has("autoGenerated");
372-
}
373-
374-
return YoutubeParsingHelper.isVerified(header.json.getArray("badges"));
375-
}
376-
377-
return false;
357+
return YoutubeChannelHelper.isChannelVerified(channelHeader.orElseThrow(() ->
358+
new ParsingException("Could not get verified status")));
378359
}
379360

380361
@Nonnull

0 commit comments

Comments
 (0)