|
23 | 23 | import static org.schabi.newpipe.extractor.ServiceList.YouTube; |
24 | 24 |
|
25 | 25 | public class YoutubeCommentsExtractorTest { |
26 | | - private static final String urlYT = "https://www.youtube.com/watch?v=D00Au7k3i6o"; |
27 | | - private static final String urlInvidious = "https://invidio.us/watch?v=D00Au7k3i6o"; |
28 | | - private static YoutubeCommentsExtractor extractorYT; |
29 | | - private static YoutubeCommentsExtractor extractorInvidious; |
30 | | - |
31 | | - @BeforeClass |
32 | | - public static void setUp() throws Exception { |
33 | | - NewPipe.init(DownloaderTestImpl.getInstance()); |
34 | | - extractorYT = (YoutubeCommentsExtractor) YouTube |
35 | | - .getCommentsExtractor(urlYT); |
36 | | - extractorYT.fetchPage(); |
37 | | - extractorInvidious = (YoutubeCommentsExtractor) YouTube |
38 | | - .getCommentsExtractor(urlInvidious); |
39 | | - } |
| 26 | + /** |
| 27 | + * Test a "normal" YouTube and Invidious page |
| 28 | + */ |
| 29 | + public static class Thomas { |
| 30 | + private static final String urlYT = "https://www.youtube.com/watch?v=D00Au7k3i6o"; |
| 31 | + private static final String urlInvidious = "https://invidio.us/watch?v=D00Au7k3i6o"; |
| 32 | + private static YoutubeCommentsExtractor extractorYT; |
| 33 | + private static YoutubeCommentsExtractor extractorInvidious; |
| 34 | + |
| 35 | + private static final String commentContent = "sub 4 sub"; |
| 36 | + |
| 37 | + @BeforeClass |
| 38 | + public static void setUp() throws Exception { |
| 39 | + NewPipe.init(DownloaderTestImpl.getInstance()); |
| 40 | + extractorYT = (YoutubeCommentsExtractor) YouTube |
| 41 | + .getCommentsExtractor(urlYT); |
| 42 | + extractorYT.fetchPage(); |
| 43 | + extractorInvidious = (YoutubeCommentsExtractor) YouTube |
| 44 | + .getCommentsExtractor(urlInvidious); |
| 45 | + extractorInvidious.fetchPage(); |
| 46 | + } |
40 | 47 |
|
41 | | - @Test |
42 | | - public void testGetComments() throws IOException, ExtractionException { |
43 | | - assertTrue(getCommentsHelper(extractorYT)); |
44 | | - assertTrue(getCommentsHelper(extractorInvidious)); |
45 | | - } |
| 48 | + @Test |
| 49 | + public void testGetComments() throws IOException, ExtractionException { |
| 50 | + assertTrue(getCommentsHelper(extractorYT)); |
| 51 | + assertTrue(getCommentsHelper(extractorInvidious)); |
| 52 | + } |
| 53 | + |
| 54 | + private boolean getCommentsHelper(YoutubeCommentsExtractor extractor) throws IOException, ExtractionException { |
| 55 | + InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage(); |
| 56 | + boolean result = findInComments(comments, commentContent); |
46 | 57 |
|
47 | | - private boolean getCommentsHelper(YoutubeCommentsExtractor extractor) throws IOException, ExtractionException { |
48 | | - InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage(); |
49 | | - boolean result = findInComments(comments, "s1ck m3m3"); |
| 58 | + while (comments.hasNextPage() && !result) { |
| 59 | + comments = extractor.getPage(comments.getNextPage()); |
| 60 | + result = findInComments(comments, commentContent); |
| 61 | + } |
50 | 62 |
|
51 | | - while (comments.hasNextPage() && !result) { |
52 | | - comments = extractor.getPage(comments.getNextPage()); |
53 | | - result = findInComments(comments, "s1ck m3m3"); |
| 63 | + return result; |
54 | 64 | } |
55 | 65 |
|
56 | | - return result; |
57 | | - } |
| 66 | + @Test |
| 67 | + public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException { |
| 68 | + assertTrue(getCommentsFromCommentsInfoHelper(urlYT)); |
| 69 | + assertTrue(getCommentsFromCommentsInfoHelper(urlInvidious)); |
| 70 | + } |
58 | 71 |
|
59 | | - @Test |
60 | | - public void testGetCommentsFromCommentsInfo() throws IOException, ExtractionException { |
61 | | - assertTrue(getCommentsFromCommentsInfoHelper(urlYT)); |
62 | | - assertTrue(getCommentsFromCommentsInfoHelper(urlInvidious)); |
63 | | - } |
| 72 | + private boolean getCommentsFromCommentsInfoHelper(String url) throws IOException, ExtractionException { |
| 73 | + final CommentsInfo commentsInfo = CommentsInfo.getInfo(url); |
64 | 74 |
|
65 | | - private boolean getCommentsFromCommentsInfoHelper(String url) throws IOException, ExtractionException { |
66 | | - CommentsInfo commentsInfo = CommentsInfo.getInfo(url); |
| 75 | + assertEquals("Comments", commentsInfo.getName()); |
| 76 | + boolean result = findInComments(commentsInfo.getRelatedItems(), commentContent); |
67 | 77 |
|
68 | | - assertEquals("Comments", commentsInfo.getName()); |
69 | | - boolean result = findInComments(commentsInfo.getRelatedItems(), "s1ck m3m3"); |
| 78 | + Page nextPage = commentsInfo.getNextPage(); |
| 79 | + InfoItemsPage<CommentsInfoItem> moreItems = new InfoItemsPage<>(null, nextPage, null); |
| 80 | + while (moreItems.hasNextPage() && !result) { |
| 81 | + moreItems = CommentsInfo.getMoreItems(YouTube, commentsInfo, nextPage); |
| 82 | + result = findInComments(moreItems.getItems(), commentContent); |
| 83 | + nextPage = moreItems.getNextPage(); |
| 84 | + } |
| 85 | + return result; |
| 86 | + } |
70 | 87 |
|
71 | | - Page nextPage = commentsInfo.getNextPage(); |
72 | | - InfoItemsPage<CommentsInfoItem> moreItems = new InfoItemsPage<>(null, nextPage, null); |
73 | | - while (moreItems.hasNextPage() && !result) { |
74 | | - moreItems = CommentsInfo.getMoreItems(YouTube, commentsInfo, nextPage); |
75 | | - result = findInComments(moreItems.getItems(), "s1ck m3m3"); |
76 | | - nextPage = moreItems.getNextPage(); |
| 88 | + @Test |
| 89 | + public void testGetCommentsAllData() throws IOException, ExtractionException { |
| 90 | + InfoItemsPage<CommentsInfoItem> comments = extractorYT.getInitialPage(); |
| 91 | + |
| 92 | + DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors()); |
| 93 | + for (CommentsInfoItem c : comments.getItems()) { |
| 94 | + assertFalse(Utils.isBlank(c.getUploaderUrl())); |
| 95 | + assertFalse(Utils.isBlank(c.getUploaderName())); |
| 96 | + assertFalse(Utils.isBlank(c.getUploaderAvatarUrl())); |
| 97 | + assertFalse(Utils.isBlank(c.getCommentId())); |
| 98 | + assertFalse(Utils.isBlank(c.getCommentText())); |
| 99 | + assertFalse(Utils.isBlank(c.getName())); |
| 100 | + assertFalse(Utils.isBlank(c.getTextualUploadDate())); |
| 101 | + assertNotNull(c.getUploadDate()); |
| 102 | + assertFalse(Utils.isBlank(c.getThumbnailUrl())); |
| 103 | + assertFalse(Utils.isBlank(c.getUrl())); |
| 104 | + assertFalse(c.getLikeCount() < 0); |
| 105 | + } |
77 | 106 | } |
78 | | - return result; |
79 | | - } |
80 | 107 |
|
81 | | - @Test |
82 | | - public void testGetCommentsAllData() throws IOException, ExtractionException { |
83 | | - InfoItemsPage<CommentsInfoItem> comments = extractorYT.getInitialPage(); |
84 | | - |
85 | | - DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors()); |
86 | | - for (CommentsInfoItem c : comments.getItems()) { |
87 | | - assertFalse(Utils.isBlank(c.getUploaderUrl())); |
88 | | - assertFalse(Utils.isBlank(c.getUploaderName())); |
89 | | - assertFalse(Utils.isBlank(c.getUploaderAvatarUrl())); |
90 | | - assertFalse(Utils.isBlank(c.getCommentId())); |
91 | | - assertFalse(Utils.isBlank(c.getCommentText())); |
92 | | - assertFalse(Utils.isBlank(c.getName())); |
93 | | - assertFalse(Utils.isBlank(c.getTextualUploadDate())); |
94 | | - assertNotNull(c.getUploadDate()); |
95 | | - assertFalse(Utils.isBlank(c.getThumbnailUrl())); |
96 | | - assertFalse(Utils.isBlank(c.getUrl())); |
97 | | - assertFalse(c.getLikeCount() < 0); |
| 108 | + private boolean findInComments(InfoItemsPage<CommentsInfoItem> comments, String comment) { |
| 109 | + return findInComments(comments.getItems(), comment); |
98 | 110 | } |
99 | | - } |
100 | 111 |
|
101 | | - private boolean findInComments(InfoItemsPage<CommentsInfoItem> comments, String comment) { |
102 | | - return findInComments(comments.getItems(), comment); |
| 112 | + private boolean findInComments(List<CommentsInfoItem> comments, String comment) { |
| 113 | + for (CommentsInfoItem c : comments) { |
| 114 | + if (c.getCommentText().contains(comment)) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
103 | 120 | } |
104 | 121 |
|
105 | | - private boolean findInComments(List<CommentsInfoItem> comments, String comment) { |
106 | | - for (CommentsInfoItem c : comments) { |
107 | | - if (c.getCommentText().contains(comment)) { |
108 | | - return true; |
| 122 | + /** |
| 123 | + * Test a video with an empty comment |
| 124 | + */ |
| 125 | + public static class EmptyComment { |
| 126 | + private static YoutubeCommentsExtractor extractor; |
| 127 | + private final static String url = "https://www.youtube.com/watch?v=VM_6n762j6M"; |
| 128 | + |
| 129 | + @BeforeClass |
| 130 | + public static void setUp() throws Exception { |
| 131 | + NewPipe.init(DownloaderTestImpl.getInstance()); |
| 132 | + extractor = (YoutubeCommentsExtractor) YouTube |
| 133 | + .getCommentsExtractor(url); |
| 134 | + extractor.fetchPage(); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testGetCommentsAllData() throws IOException, ExtractionException { |
| 139 | + final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage(); |
| 140 | + |
| 141 | + DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors()); |
| 142 | + for (CommentsInfoItem c : comments.getItems()) { |
| 143 | + assertFalse(Utils.isBlank(c.getUploaderUrl())); |
| 144 | + assertFalse(Utils.isBlank(c.getUploaderName())); |
| 145 | + assertFalse(Utils.isBlank(c.getUploaderAvatarUrl())); |
| 146 | + assertFalse(Utils.isBlank(c.getCommentId())); |
| 147 | + assertFalse(Utils.isBlank(c.getName())); |
| 148 | + assertFalse(Utils.isBlank(c.getTextualUploadDate())); |
| 149 | + assertNotNull(c.getUploadDate()); |
| 150 | + assertFalse(Utils.isBlank(c.getThumbnailUrl())); |
| 151 | + assertFalse(Utils.isBlank(c.getUrl())); |
| 152 | + assertFalse(c.getLikeCount() < 0); |
| 153 | + if (c.getCommentId().equals("Ugga_h1-EXdHB3gCoAEC")) { // comment without text |
| 154 | + assertTrue(Utils.isBlank(c.getCommentText())); |
| 155 | + } else { |
| 156 | + assertFalse(Utils.isBlank(c.getCommentText())); |
| 157 | + } |
109 | 158 | } |
110 | 159 | } |
111 | | - return false; |
| 160 | + |
112 | 161 | } |
113 | 162 | } |
0 commit comments