Fix YouTube likes + dislikes

This commit is contained in:
bopol 2021-11-19 21:36:03 +01:00
parent 5028396405
commit c4eca91be9
1 changed files with 43 additions and 25 deletions

View File

@ -337,19 +337,33 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override @Override
public long getLikeCount() throws ParsingException { public long getLikeCount() throws ParsingException {
assertPageFetched(); assertPageFetched();
String likesString = ""; String likesString = null;
try { try {
try { likesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar")
likesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar") .getObject("sentimentBarRenderer").getString("tooltip");
.getObject("sentimentBarRenderer").getString("tooltip").split("/")[0]; if (likesString != null && likesString.contains("/")) {
} catch (final NullPointerException e) { likesString = likesString.split("/")[0];
} else {
likesString = getVideoPrimaryInfoRenderer()
.getObject("videoActions")
.getObject("menuRenderer")
.getArray("topLevelButtons")
.getObject(0)
.getObject("toggleButtonRenderer")
.getObject("defaultText")
.getObject("accessibility")
.getObject("accessibilityData")
.getString("label");
}
if (likesString == null) {
// If this kicks in our button has no content and therefore ratings must be disabled // If this kicks in our button has no content and therefore ratings must be disabled
if (playerResponse.getObject("videoDetails").getBoolean("allowRatings")) { if (playerResponse.getObject("videoDetails").getBoolean("allowRatings")) {
throw new ParsingException( throw new ParsingException("Ratings are enabled even though the like button is missing");
"Ratings are enabled even though the like button is missing", e);
} }
return -1; return -1;
} }
return Integer.parseInt(Utils.removeNonDigitCharacters(likesString)); return Integer.parseInt(Utils.removeNonDigitCharacters(likesString));
} catch (final NumberFormatException nfe) { } catch (final NumberFormatException nfe) {
throw new ParsingException("Could not parse \"" + likesString + "\" as an Integer", throw new ParsingException("Could not parse \"" + likesString + "\" as an Integer",
@ -366,29 +380,33 @@ public class YoutubeStreamExtractor extends StreamExtractor {
public long getDislikeCount() throws ParsingException { public long getDislikeCount() throws ParsingException {
assertPageFetched(); assertPageFetched();
String dislikesString = ""; String dislikesString = null;
try { try {
try { dislikesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar")
dislikesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar") .getObject("sentimentBarRenderer").getString("tooltip");
.getObject("sentimentBarRenderer").getString("tooltip").split("/")[1]; if (dislikesString != null && dislikesString.contains("/")) {
} catch (final NullPointerException e) { dislikesString = dislikesString.split("/")[1];
// If this kicks in our button has no content and therefore ratings must be disabled } else {
if (playerResponse.getObject("videoDetails").getBoolean("allowRatings")) { // Calculate dislike with average rating and like count
throw new ParsingException( long likes = getLikeCount();
"Ratings are enabled even though the dislike button is missing", e); double averageRating = playerResponse.getObject("videoDetails").getDouble("averageRating");
if (likes != -1 && averageRating > 1) {
// If averageRating can't be gathered, it will be 0,
// but we also can't divide by 0 so we need > 1
return Math.round(likes * ((5 - averageRating) / (averageRating - 1)));
} }
return -1;
} }
return Integer.parseInt(Utils.removeNonDigitCharacters(dislikesString));
} catch (final NumberFormatException nfe) { if (dislikesString != null) {
throw new ParsingException("Could not parse \"" + dislikesString + "\" as an Integer", return Integer.parseInt(Utils.removeNonDigitCharacters(dislikesString));
nfe); }
} catch (final Exception e) { } catch (final Exception e) {
if (getAgeLimit() == NO_AGE_LIMIT) {
throw new ParsingException("Could not get dislike count", e);
}
return -1;
} }
// Silently fail as YouTube is "gradually rolling out" removing dislike count
// https://blog.youtube/news-and-events/update-to-youtube/
return -1;
} }
@Nonnull @Nonnull