Merge pull request #510 from B0pol/heartedbycreator

comments: add heartedByUploader and extract it for YouTube
This commit is contained in:
Tobias Groza 2021-01-12 19:00:04 +01:00 committed by GitHub
commit 6773ec8699
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 7 deletions

View File

@ -16,6 +16,7 @@ public class CommentsInfoItem extends InfoItem {
@Nullable @Nullable
private DateWrapper uploadDate; private DateWrapper uploadDate;
private int likeCount; private int likeCount;
private boolean heartedByUploader;
public CommentsInfoItem(int serviceId, String url, String name) { public CommentsInfoItem(int serviceId, String url, String name) {
super(InfoType.COMMENT, serviceId, url, name); super(InfoType.COMMENT, serviceId, url, name);
@ -85,4 +86,12 @@ public class CommentsInfoItem extends InfoItem {
public void setLikeCount(int likeCount) { public void setLikeCount(int likeCount) {
this.likeCount = 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 getUploaderName() throws ParsingException;
String getUploaderAvatarUrl() 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); addError(e);
} }
try {
resultItem.setHeartedByUploader(extractor.getHeartedByUploader());
} catch (Exception e) {
addError(e);
}
return resultItem; return resultItem;
} }

View File

@ -88,6 +88,11 @@ public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtrac
return baseUrl + value; return baseUrl + value;
} }
@Override
public boolean getHeartedByUploader() throws ParsingException {
return false;
}
@Override @Override
public String getUploaderName() throws ParsingException { public String getUploaderName() throws ParsingException {
return JsonUtils.getString(item, "account.name") + "@" + JsonUtils.getString(item, "account.host"); 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"); return json.getObject("user").getString("avatar_url");
} }
@Override
public boolean getHeartedByUploader() throws ParsingException {
return false;
}
@Override @Override
public String getUploaderUrl() { public String getUploaderUrl() {
return json.getObject("user").getString("permalink_url"); 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 @Override
public String getUploaderName() throws ParsingException { public String getUploaderName() throws ParsingException {
try { try {

View File

@ -16,10 +16,7 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.YouTube; import static org.schabi.newpipe.extractor.ServiceList.YouTube;
public class YoutubeCommentsExtractorTest { public class YoutubeCommentsExtractorTest {
@ -28,9 +25,8 @@ public class YoutubeCommentsExtractorTest {
*/ */
public static class Thomas { public static class Thomas {
private static final String url = "https://www.youtube.com/watch?v=D00Au7k3i6o"; 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 final String commentContent = "Category: Education";
private static YoutubeCommentsExtractor extractor;
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
@ -116,8 +112,8 @@ public class YoutubeCommentsExtractorTest {
* Test a video with an empty comment * Test a video with an empty comment
*/ */
public static class EmptyComment { public static class EmptyComment {
private static YoutubeCommentsExtractor extractor;
private final static String url = "https://www.youtube.com/watch?v=VM_6n762j6M"; private final static String url = "https://www.youtube.com/watch?v=VM_6n762j6M";
private static YoutubeCommentsExtractor extractor;
@BeforeClass @BeforeClass
public static void setUp() throws Exception { 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);
}
}
} }