comments: add heartedByCreator and extract it for youtube

This commit is contained in:
bopol 2021-01-11 18:38:54 +01:00
parent da968cf9c5
commit f3c22da618
7 changed files with 79 additions and 7 deletions

View File

@ -16,6 +16,7 @@ public class CommentsInfoItem extends InfoItem {
@Nullable
private DateWrapper uploadDate;
private int likeCount;
private boolean heartedByUploader;
public CommentsInfoItem(int serviceId, String url, String name) {
super(InfoType.COMMENT, serviceId, url, name);
@ -85,4 +86,12 @@ public class CommentsInfoItem extends InfoItem {
public void setLikeCount(int likeCount) {
this.likeCount = likeCount;
}
public void setHeartedByUploader(boolean isHeartedByUploader) {
this.heartedByUploader = isHeartedByUploader;
}
public boolean getHeartedByUploader() {
return this.heartedByUploader;
}
}

View File

@ -40,4 +40,9 @@ public interface CommentsInfoItemExtractor extends InfoItemExtractor {
String getUploaderName() throws ParsingException;
String getUploaderAvatarUrl() throws ParsingException;
/**
* Whether the comment has been hearted by the uploader
*/
boolean getHeartedByUploader() throws ParsingException;
}

View File

@ -70,6 +70,12 @@ public class CommentsInfoItemsCollector extends InfoItemsCollector<CommentsInfoI
addError(e);
}
try {
resultItem.setHeartedByUploader(extractor.getHeartedByUploader());
} catch (Exception e) {
addError(e);
}
return resultItem;
}

View File

@ -88,6 +88,11 @@ public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtrac
return baseUrl + value;
}
@Override
public boolean getHeartedByUploader() throws ParsingException {
return false;
}
@Override
public String getUploaderName() throws ParsingException {
return JsonUtils.getString(item, "account.name") + "@" + JsonUtils.getString(item, "account.host");

View File

@ -39,6 +39,11 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr
return json.getObject("user").getString("avatar_url");
}
@Override
public boolean getHeartedByUploader() throws ParsingException {
return false;
}
@Override
public String getUploaderUrl() {
return json.getObject("user").getString("permalink_url");

View File

@ -115,6 +115,11 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract
}
}
@Override
public boolean getHeartedByUploader() throws ParsingException {
return json.has("creatorHeart");
}
@Override
public String getUploaderName() throws ParsingException {
try {

View File

@ -16,10 +16,7 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
public class YoutubeCommentsExtractorTest {
@ -28,9 +25,8 @@ public class YoutubeCommentsExtractorTest {
*/
public static class Thomas {
private static final String url = "https://www.youtube.com/watch?v=D00Au7k3i6o";
private static YoutubeCommentsExtractor extractor;
private static final String commentContent = "Category: Education";
private static YoutubeCommentsExtractor extractor;
@BeforeClass
public static void setUp() throws Exception {
@ -116,8 +112,8 @@ public class YoutubeCommentsExtractorTest {
* Test a video with an empty comment
*/
public static class EmptyComment {
private static YoutubeCommentsExtractor extractor;
private final static String url = "https://www.youtube.com/watch?v=VM_6n762j6M";
private static YoutubeCommentsExtractor extractor;
@BeforeClass
public static void setUp() throws Exception {
@ -152,4 +148,45 @@ public class YoutubeCommentsExtractorTest {
}
}
public static class HeartedByCreator {
private final static String url = "https://www.youtube.com/watch?v=tR11b7uh17Y";
private static YoutubeCommentsExtractor extractor;
@BeforeClass
public static void setUp() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance());
extractor = (YoutubeCommentsExtractor) YouTube
.getCommentsExtractor(url);
extractor.fetchPage();
}
@Test
public void testGetCommentsAllData() throws IOException, ExtractionException {
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());
boolean heartedByUploader = false;
for (CommentsInfoItem c : comments.getItems()) {
assertFalse(Utils.isBlank(c.getUploaderUrl()));
assertFalse(Utils.isBlank(c.getUploaderName()));
assertFalse(Utils.isBlank(c.getUploaderAvatarUrl()));
assertFalse(Utils.isBlank(c.getCommentId()));
assertFalse(Utils.isBlank(c.getName()));
assertFalse(Utils.isBlank(c.getTextualUploadDate()));
assertNotNull(c.getUploadDate());
assertFalse(Utils.isBlank(c.getThumbnailUrl()));
assertFalse(Utils.isBlank(c.getUrl()));
assertFalse(c.getLikeCount() < 0);
assertFalse(Utils.isBlank(c.getCommentText()));
if (c.getHeartedByUploader()) {
heartedByUploader = true;
}
}
assertTrue("No comments was hearted by uploader", heartedByUploader);
}
}
}