Skip to content

Commit 2174685

Browse files
committed
Reimplemented likeCount
1 parent 01cfb55 commit 2174685

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class CommentsInfoItem extends InfoItem {
1616
private String textualUploadDate;
1717
@Nullable
1818
private DateWrapper uploadDate;
19+
private int likeCount;
1920
private String textualVoteCount;
2021
private boolean heartedByUploader;
2122
private boolean pinned;
@@ -81,6 +82,14 @@ public void setUploadDate(@Nullable DateWrapper uploadDate) {
8182
this.uploadDate = uploadDate;
8283
}
8384

85+
public int getLikeCount() {
86+
return likeCount;
87+
}
88+
89+
public void setLikeCount(int likeCount) {
90+
this.likeCount = likeCount;
91+
}
92+
8493
public String getTextualVoteCount() {
8594
return textualVoteCount;
8695
}

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.schabi.newpipe.extractor.InfoItemExtractor;
44
import org.schabi.newpipe.extractor.exceptions.ParsingException;
55
import org.schabi.newpipe.extractor.localization.DateWrapper;
6+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeCommentsInfoItemExtractor;
67
import org.schabi.newpipe.extractor.stream.StreamExtractor;
78
import org.schabi.newpipe.extractor.utils.Utils;
89

@@ -11,8 +12,18 @@
1112
public interface CommentsInfoItemExtractor extends InfoItemExtractor {
1213

1314
/**
14-
* The formatted text (e.g. 420, 4K, 4.2M) of the votes
15+
* Return the like count of the comment, or -1 if it's unavailable<br/>
16+
* NOTE: Currently only implemented for YT {@link YoutubeCommentsInfoItemExtractor#getLikeCount()}
17+
* with limitations
1518
*
19+
* @see StreamExtractor#getLikeCount()
20+
*/
21+
default int getLikeCount() throws ParsingException {
22+
return -1;
23+
}
24+
25+
/**
26+
* The formatted text (e.g. 420, 4K, 4.2M) of the votes<br/>
1627
* May be language dependent
1728
*/
1829
default String getTextualVoteCount() throws ParsingException {

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemsCollector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public CommentsInfoItem extract(CommentsInfoItemExtractor extractor) throws Pars
5959
} catch (Exception e) {
6060
addError(e);
6161
}
62+
try {
63+
resultItem.setLikeCount(extractor.getLikeCount());
64+
} catch (Exception e) {
65+
addError(e);
66+
}
6267
try {
6368
resultItem.setTextualVoteCount(extractor.getTextualVoteCount());
6469
} catch (Exception e) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ public DateWrapper getUploadDate() throws ParsingException {
7070
}
7171
}
7272

73+
/**
74+
* @implNote The method is parsing internally a localized string.<br/>
75+
* This will fail for other languages than English.
76+
* However as long as the Extractor only uses "en-GB"
77+
* (as seen in {@link org.schabi.newpipe.extractor.services.youtube.YoutubeService#SUPPORTED_LANGUAGES})
78+
* everything will work fine.<br/>
79+
* Consider using {@link #getTextualVoteCount()}
80+
*/
81+
@Override
82+
public int getLikeCount() throws ParsingException {
83+
// This may return a language dependent version, e.g. in German: 3,3 Mio
84+
String voteCount = getTextualVoteCount();
85+
try {
86+
if (Utils.isBlank(voteCount)) {
87+
return 0;
88+
}
89+
90+
return (int) Utils.mixedNumberWordToLong(voteCount);
91+
} catch (Exception e) {
92+
throw new ParsingException("Unexpected error while converting vote count", e);
93+
}
94+
}
95+
7396
@Override
7497
public String getTextualVoteCount() throws ParsingException {
7598
/*

0 commit comments

Comments
 (0)