Skip to content

Commit f6f2724

Browse files
authored
Merge pull request #753 from B0pol/dis-likes
Fix YouTube likes + dislikes
2 parents 5028396 + 56c8af7 commit f6f2724

1 file changed

Lines changed: 38 additions & 25 deletions

File tree

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

Lines changed: 38 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,28 @@ public long getLikeCount() throws ParsingException {
366380
public long getDislikeCount() throws ParsingException {
367381
assertPageFetched();
368382

369-
String dislikesString = "";
370383
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);
384+
String dislikesString = getVideoPrimaryInfoRenderer().getObject("sentimentBar")
385+
.getObject("sentimentBarRenderer").getString("tooltip");
386+
if (dislikesString != null && dislikesString.contains("/")) {
387+
dislikesString = dislikesString.split("/")[1];
388+
return Integer.parseInt(Utils.removeNonDigitCharacters(dislikesString));
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);
386400
} catch (final Exception e) {
387-
if (getAgeLimit() == NO_AGE_LIMIT) {
388-
throw new ParsingException("Could not get dislike count", e);
389-
}
390-
return -1;
391401
}
402+
// Silently fail as YouTube is "gradually rolling out" removing dislike count
403+
// https://blog.youtube/news-and-events/update-to-youtube/
404+
return -1;
392405
}
393406

394407
@Nonnull

0 commit comments

Comments
 (0)