Skip to content

Commit cabd55e

Browse files
committed
Fix retrieving the like count for YouTube videos with no likes
1 parent a88b50e commit cabd55e

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
@@ -402,77 +402,6 @@ public long getLikeCount() throws ParsingException {
402402
.getObject("menuRenderer")
403403
.getArray("topLevelButtons");
404404

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

500-
// The like count is always returned as a number in this element, even for videos with no
501-
// likes
429+
// The like count is always returned as a number in this element for videos with likes
502430
try {
503431
return Long.parseLong(Utils.removeNonDigitCharacters(accessibilityText));
504432
} catch (final NumberFormatException e) {
505-
throw new ParsingException(
506-
"Could not parse \"" + accessibilityText + "\" as a long", e);
433+
// If an exception was thrown, the video has zero likes
507434
}
435+
436+
return 0;
508437
}
509438

510439
@Nonnull

0 commit comments

Comments
 (0)