Skip to content

Commit

Permalink
[YouTube] Fix search subscriber count extraction with channel handles
Browse files Browse the repository at this point in the history
Author: Theta-Dev
  • Loading branch information
ShareASmile authored Sep 2, 2023
1 parent 9bf0069 commit 0e15037
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,26 @@

public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor {
private final JsonObject channelInfoItem;
/**
* New layout:
* "subscriberCountText": Channel handle
* "videoCountText": Subscriber count
*/
private final boolean withHandle;

public YoutubeChannelInfoItemExtractor(final JsonObject channelInfoItem) {
this.channelInfoItem = channelInfoItem;

boolean wHandle = false;
try {
final String subscriberCountText = getTextFromObject(
channelInfoItem.getObject("subscriberCountText"));
if (subscriberCountText != null) {
wHandle = subscriberCountText.startsWith("@");
}
} catch (final ParsingException ignored) {
}
this.withHandle = wHandle;
}

@Override
Expand Down Expand Up @@ -78,6 +95,11 @@ public long getSubscriberCount() throws ParsingException {
return -1;
}

if (withHandle) {
return Utils.mixedNumberWordToLong(getTextFromObject(
channelInfoItem.getObject("videoCountText")));
}

return Utils.mixedNumberWordToLong(getTextFromObject(
channelInfoItem.getObject("subscriberCountText")));
} catch (final Exception e) {
Expand All @@ -88,8 +110,9 @@ public long getSubscriberCount() throws ParsingException {
@Override
public long getStreamCount() throws ParsingException {
try {
if (!channelInfoItem.has("videoCountText")) {
// Video count is not available, channel probably has no public uploads.
if (withHandle || !channelInfoItem.has("videoCountText")) {
// Video count is not available, either the channel has no public uploads
// or YouTube displays the channel handle instead.
return ListExtractor.ITEM_COUNT_UNKNOWN;
}

Expand Down

0 comments on commit 0e15037

Please sign in to comment.