[YouTube] Fix extraction of like count with the new data model

In the new data model currently A/B tested or deployed, the like count button
data is the same, but its path has been changed.

The extraction of the like count has been also improved, by using the multiple
accessibility data available instead of the only one which was used before.
This accessibility data has been also deprioritized, because it is language
dependent when there is no view.

Like count is always known, even for age-restricted videos, so returning -1 if
the like count extraction fails on this type of videos is not needed and hides
an extraction error: that's the reason why this return has been removed and the
exception will always be thrown, even if a video is age-restricted.
This commit is contained in:
AudricV 2022-09-10 16:38:25 +02:00
parent 6a858368c8
commit 119b9129e2
No known key found for this signature in database
GPG Key ID: DA92EC7905614198
1 changed files with 73 additions and 16 deletions

View File

@ -417,28 +417,88 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override
public long getLikeCount() throws ParsingException {
assertPageFetched();
// If ratings are not allowed, there is no like count available
if (!playerResponse.getObject("videoDetails").getBoolean("allowRatings")) {
return -1;
}
String likesString = "";
try {
likesString = getVideoPrimaryInfoRenderer()
final JsonArray topLevelButtons = getVideoPrimaryInfoRenderer()
.getObject("videoActions")
.getObject("menuRenderer")
.getArray("topLevelButtons")
.getObject(0)
.getObject("toggleButtonRenderer")
.getObject("defaultText")
.getObject("accessibility")
.getArray("topLevelButtons");
// Try first with the new video actions buttons data structure
JsonObject likeToggleButtonRenderer = topLevelButtons.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.map(button -> button.getObject("segmentedLikeDislikeButtonRenderer")
.getObject("likeButton")
.getObject("toggleButtonRenderer"))
.filter(toggleButtonRenderer -> !isNullOrEmpty(toggleButtonRenderer))
.findFirst()
.orElse(null);
// Use the old video actions buttons data structure if the new one isn't returned
if (likeToggleButtonRenderer == null) {
/*
In the old video actions buttons data structure, there are 3 ways to detect whether
a button is the like button, using its toggleButtonRenderer:
- checking whether toggleButtonRenderer.targetId is equal to watch-like;
- checking whether toggleButtonRenderer.defaultIcon.iconType is equal to LIKE;
- checking whether
toggleButtonRenderer.toggleButtonSupportedData.toggleButtonIdData.id
is equal to TOGGLE_BUTTON_ID_TYPE_LIKE.
*/
likeToggleButtonRenderer = topLevelButtons.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.map(topLevelButton -> topLevelButton.getObject("toggleButtonRenderer"))
.filter(toggleButtonRenderer -> toggleButtonRenderer.getString("targetId")
.equalsIgnoreCase("watch-like")
|| toggleButtonRenderer.getObject("defaultIcon")
.getString("iconType")
.equalsIgnoreCase("LIKE")
|| toggleButtonRenderer.getObject("toggleButtonSupportedData")
.getObject("toggleButtonIdData")
.getString("id")
.equalsIgnoreCase("TOGGLE_BUTTON_ID_TYPE_LIKE"))
.findFirst()
.orElseThrow(() -> new ParsingException(
"The like button is missing even though ratings are enabled"));
}
// Use one of the accessibility strings available (this one has the same path as the
// one used for comments' like count extraction)
likesString = likeToggleButtonRenderer.getObject("accessibilityData")
.getObject("accessibilityData")
.getString("label");
// Use the other accessibility string available which contains the exact like count
if (likesString == null) {
// If this kicks in our button has no content and therefore ratings must be disabled
if (playerResponse.getObject("videoDetails").getBoolean("allowRatings")) {
throw new ParsingException(
"Ratings are enabled even though the like button is missing");
}
return -1;
likesString = likeToggleButtonRenderer.getObject("accessibility")
.getString("label");
}
// Last method: use the defaultText's accessibility data, which contains the exact like
// count too, except when it is equal to 0, where a localized string is returned instead
if (likesString == null) {
likesString = likeToggleButtonRenderer.getObject("defaultText")
.getObject("accessibility")
.getObject("accessibilityData")
.getString("label");
}
// If ratings are allowed and the likes string is null, it means that we couldn't
// extract the (real) like count from accessibility data
if (likesString == null) {
throw new ParsingException("Could not get like count from accessibility data");
}
// This check only works with English localizations!
if (likesString.toLowerCase().contains("no likes")) {
return 0;
}
@ -448,10 +508,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
throw new ParsingException("Could not parse \"" + likesString + "\" as an Integer",
nfe);
} catch (final Exception e) {
if (getAgeLimit() == NO_AGE_LIMIT) {
throw new ParsingException("Could not get like count", e);
}
return -1;
throw new ParsingException("Could not get like count", e);
}
}