Skip to content

Commit c4eca91

Browse files
committed
Fix YouTube likes + dislikes
1 parent 5028396 commit c4eca91

1 file changed

Lines changed: 43 additions & 25 deletions

File tree

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

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -337,19 +337,33 @@ public long getViewCount() throws ParsingException {
337337
@Override
338338
public long getLikeCount() throws ParsingException {
339339
assertPageFetched();
340-
String likesString = "";
340+
String likesString = null;
341341
try {
342-
try {
343-
likesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar")
344-
.getObject("sentimentBarRenderer").getString("tooltip").split("/")[0];
345-
} catch (final NullPointerException e) {
342+
likesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar")
343+
.getObject("sentimentBarRenderer").getString("tooltip");
344+
if (likesString != null && likesString.contains("/")) {
345+
likesString = likesString.split("/")[0];
346+
} else {
347+
likesString = getVideoPrimaryInfoRenderer()
348+
.getObject("videoActions")
349+
.getObject("menuRenderer")
350+
.getArray("topLevelButtons")
351+
.getObject(0)
352+
.getObject("toggleButtonRenderer")
353+
.getObject("defaultText")
354+
.getObject("accessibility")
355+
.getObject("accessibilityData")
356+
.getString("label");
357+
}
358+
359+
if (likesString == null) {
346360
// If this kicks in our button has no content and therefore ratings must be disabled
347361
if (playerResponse.getObject("videoDetails").getBoolean("allowRatings")) {
348-
throw new ParsingException(
349-
"Ratings are enabled even though the like button is missing", e);
362+
throw new ParsingException("Ratings are enabled even though the like button is missing");
350363
}
351364
return -1;
352365
}
366+
353367
return Integer.parseInt(Utils.removeNonDigitCharacters(likesString));
354368
} catch (final NumberFormatException nfe) {
355369
throw new ParsingException("Could not parse \"" + likesString + "\" as an Integer",
@@ -366,29 +380,33 @@ public long getLikeCount() throws ParsingException {
366380
public long getDislikeCount() throws ParsingException {
367381
assertPageFetched();
368382

369-
String dislikesString = "";
383+
String dislikesString = null;
370384
try {
371-
try {
372-
dislikesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar")
373-
.getObject("sentimentBarRenderer").getString("tooltip").split("/")[1];
374-
} catch (final NullPointerException e) {
375-
// If this kicks in our button has no content and therefore ratings must be disabled
376-
if (playerResponse.getObject("videoDetails").getBoolean("allowRatings")) {
377-
throw new ParsingException(
378-
"Ratings are enabled even though the dislike button is missing", e);
385+
dislikesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar")
386+
.getObject("sentimentBarRenderer").getString("tooltip");
387+
if (dislikesString != null && dislikesString.contains("/")) {
388+
dislikesString = dislikesString.split("/")[1];
389+
} else {
390+
// Calculate dislike with average rating and like count
391+
long likes = getLikeCount();
392+
double averageRating = playerResponse.getObject("videoDetails").getDouble("averageRating");
393+
394+
if (likes != -1 && averageRating > 1) {
395+
// If averageRating can't be gathered, it will be 0,
396+
// but we also can't divide by 0 so we need > 1
397+
return Math.round(likes * ((5 - averageRating) / (averageRating - 1)));
379398
}
380-
return -1;
381399
}
382-
return Integer.parseInt(Utils.removeNonDigitCharacters(dislikesString));
383-
} catch (final NumberFormatException nfe) {
384-
throw new ParsingException("Could not parse \"" + dislikesString + "\" as an Integer",
385-
nfe);
386-
} catch (final Exception e) {
387-
if (getAgeLimit() == NO_AGE_LIMIT) {
388-
throw new ParsingException("Could not get dislike count", e);
400+
401+
if (dislikesString != null) {
402+
return Integer.parseInt(Utils.removeNonDigitCharacters(dislikesString));
389403
}
390-
return -1;
404+
405+
} catch (final Exception e) {
391406
}
407+
// Silently fail as YouTube is "gradually rolling out" removing dislike count
408+
// https://blog.youtube/news-and-events/update-to-youtube/
409+
return -1;
392410
}
393411

394412
@Nonnull

0 commit comments

Comments
 (0)