Skip to content

Commit 00d9b7e

Browse files
authored
Merge pull request #70 from G-flat/fix_like_count_for_yt_videos_with_no_likes
[YouTube] Fix retrieving the like count for videos with no likes
2 parents 592b5ca + cabd55e commit 00d9b7e

1 file changed

Lines changed: 4 additions & 75 deletions

File tree

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

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -414,77 +414,6 @@ public long getLikeCount() throws ParsingException {
414414
.getObject("menuRenderer")
415415
.getArray("topLevelButtons");
416416

417-
try {
418-
return parseLikeCountFromLikeButtonViewModel(topLevelButtons);
419-
} catch (final ParsingException ignored) {
420-
// A segmentedLikeDislikeButtonRenderer could be returned instead of a
421-
// segmentedLikeDislikeButtonViewModel, so ignore extraction errors relative to
422-
// segmentedLikeDislikeButtonViewModel object
423-
}
424-
425-
try {
426-
return parseLikeCountFromLikeButtonRenderer(topLevelButtons);
427-
} catch (final ParsingException e) {
428-
throw new ParsingException("Could not get like count", e);
429-
}
430-
}
431-
432-
private static long parseLikeCountFromLikeButtonRenderer(
433-
@Nonnull final JsonArray topLevelButtons) throws ParsingException {
434-
String likesString = null;
435-
final JsonObject likeToggleButtonRenderer = topLevelButtons.stream()
436-
.filter(JsonObject.class::isInstance)
437-
.map(JsonObject.class::cast)
438-
.map(button -> button.getObject("segmentedLikeDislikeButtonRenderer")
439-
.getObject("likeButton")
440-
.getObject("toggleButtonRenderer"))
441-
.filter(toggleButtonRenderer -> !isNullOrEmpty(toggleButtonRenderer))
442-
.findFirst()
443-
.orElse(null);
444-
445-
if (likeToggleButtonRenderer != null) {
446-
// Use one of the accessibility strings available (this one has the same path as the
447-
// one used for comments' like count extraction)
448-
likesString = likeToggleButtonRenderer.getObject("accessibilityData")
449-
.getObject("accessibilityData")
450-
.getString("label");
451-
452-
// Use the other accessibility string available which contains the exact like count
453-
if (likesString == null) {
454-
likesString = likeToggleButtonRenderer.getObject("accessibility")
455-
.getString("label");
456-
}
457-
458-
// Last method: use the defaultText's accessibility data, which contains the exact like
459-
// count too, except when it is equal to 0, where a localized string is returned instead
460-
if (likesString == null) {
461-
likesString = likeToggleButtonRenderer.getObject("defaultText")
462-
.getObject("accessibility")
463-
.getObject("accessibilityData")
464-
.getString("label");
465-
}
466-
467-
// This check only works with English localizations!
468-
if (likesString != null && likesString.toLowerCase().contains("no likes")) {
469-
return 0;
470-
}
471-
}
472-
473-
// If ratings are allowed and the likes string is null, it means that we couldn't extract
474-
// the full like count from accessibility data
475-
if (likesString == null) {
476-
throw new ParsingException("Could not get like count from accessibility data");
477-
}
478-
479-
try {
480-
return Long.parseLong(Utils.removeNonDigitCharacters(likesString));
481-
} catch (final NumberFormatException e) {
482-
throw new ParsingException("Could not parse \"" + likesString + "\" as a long", e);
483-
}
484-
}
485-
486-
private static long parseLikeCountFromLikeButtonViewModel(
487-
@Nonnull final JsonArray topLevelButtons) throws ParsingException {
488417
// Try first with the current video actions buttons data structure
489418
final JsonObject likeToggleButtonViewModel = topLevelButtons.stream()
490419
.filter(JsonObject.class::isInstance)
@@ -509,14 +438,14 @@ private static long parseLikeCountFromLikeButtonViewModel(
509438
throw new ParsingException("Could not find buttonViewModel's accessibilityText string");
510439
}
511440

512-
// The like count is always returned as a number in this element, even for videos with no
513-
// likes
441+
// The like count is always returned as a number in this element for videos with likes
514442
try {
515443
return Long.parseLong(Utils.removeNonDigitCharacters(accessibilityText));
516444
} catch (final NumberFormatException e) {
517-
throw new ParsingException(
518-
"Could not parse \"" + accessibilityText + "\" as a long", e);
445+
// If an exception was thrown, the video has zero likes
519446
}
447+
448+
return 0;
520449
}
521450

522451
@Nonnull

0 commit comments

Comments
 (0)