Skip to content

Commit eac850c

Browse files
authored
Merge pull request TeamNewPipe#1114 from FineFindus/feat/comment-author-is-owner
[YouTube] Add channel owner to comments
2 parents 5ab1f78 + 34b05a0 commit eac850c

File tree

9 files changed

+1004
-0
lines changed

9 files changed

+1004
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class CommentsInfoItem extends InfoItem {
3030
private int replyCount;
3131
@Nullable
3232
private Page replies;
33+
private boolean isChannelOwner;
3334
private boolean creatorReply;
3435

3536
public static final int NO_LIKE_COUNT = -1;
@@ -174,6 +175,15 @@ public Page getReplies() {
174175
return this.replies;
175176
}
176177

178+
public void setChannelOwner(final boolean channelOwner) {
179+
this.isChannelOwner = channelOwner;
180+
}
181+
182+
public boolean isChannelOwner() {
183+
return isChannelOwner;
184+
}
185+
186+
177187
public void setCreatorReply(final boolean creatorReply) {
178188
this.creatorReply = creatorReply;
179189
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ default Page getReplies() throws ParsingException {
135135
return null;
136136
}
137137

138+
/**
139+
* Whether the comment was made by the channel owner.
140+
*/
141+
default boolean isChannelOwner() throws ParsingException {
142+
return false;
143+
}
144+
138145
/**
139146
* Whether the comment was replied to by the creator.
140147
*/

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public CommentsInfoItem extract(final CommentsInfoItemExtractor extractor)
101101
addError(e);
102102
}
103103

104+
try {
105+
resultItem.setChannelOwner(extractor.isChannelOwner());
106+
} catch (final Exception e) {
107+
addError(e);
108+
}
109+
110+
104111
try {
105112
resultItem.setCreatorReply(extractor.hasCreatorReply());
106113
} catch (final Exception e) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ public Page getReplies() {
278278
}
279279
}
280280

281+
@Override
282+
public boolean isChannelOwner() throws ParsingException {
283+
return getCommentRenderer().getBoolean("authorIsChannelOwner");
284+
}
285+
286+
281287
@Override
282288
public boolean hasCreatorReply() throws ParsingException {
283289
try {

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,49 @@ public void testCommentsCount() throws IOException, ExtractionException {
352352
}
353353
}
354354

355+
public static class ChannelOwnerTest {
356+
private final static String url = "https://www.youtube.com/watch?v=bem4adjGKjE";
357+
private static YoutubeCommentsExtractor extractor;
358+
359+
@BeforeAll
360+
public static void setUp() throws Exception {
361+
YoutubeTestsUtils.ensureStateless();
362+
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "channelOwner"));
363+
extractor = (YoutubeCommentsExtractor) YouTube
364+
.getCommentsExtractor(url);
365+
extractor.fetchPage();
366+
}
367+
368+
@Test
369+
void testGetCommentsAllData() throws IOException, ExtractionException {
370+
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
371+
372+
DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());
373+
374+
boolean channelOwner = false;
375+
376+
for (final CommentsInfoItem c : comments.getItems()) {
377+
assertFalse(Utils.isBlank(c.getUploaderUrl()));
378+
assertFalse(Utils.isBlank(c.getUploaderName()));
379+
YoutubeTestsUtils.testImages(c.getUploaderAvatars());
380+
assertFalse(Utils.isBlank(c.getCommentId()));
381+
assertFalse(Utils.isBlank(c.getName()));
382+
assertFalse(Utils.isBlank(c.getTextualUploadDate()));
383+
assertNotNull(c.getUploadDate());
384+
YoutubeTestsUtils.testImages(c.getThumbnails());
385+
assertFalse(Utils.isBlank(c.getUrl()));
386+
assertTrue(c.getLikeCount() >= 0);
387+
assertFalse(Utils.isBlank(c.getCommentText().getContent()));
388+
if (c.isChannelOwner()) {
389+
channelOwner = true;
390+
}
391+
}
392+
assertTrue(channelOwner, "No comments was made by the channel owner");
393+
394+
}
395+
}
396+
397+
355398
public static class CreatorReply {
356399
private final static String url = "https://www.youtube.com/watch?v=bem4adjGKjE";
357400
private static YoutubeCommentsExtractor extractor;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"request": {
3+
"httpMethod": "GET",
4+
"url": "https://www.youtube.com/sw.js",
5+
"headers": {
6+
"Referer": [
7+
"https://www.youtube.com"
8+
],
9+
"Origin": [
10+
"https://www.youtube.com"
11+
],
12+
"Accept-Language": [
13+
"en-GB, en;q\u003d0.9"
14+
]
15+
},
16+
"localization": {
17+
"languageCode": "en",
18+
"countryCode": "GB"
19+
}
20+
},
21+
"response": {
22+
"responseCode": 200,
23+
"responseMessage": "",
24+
"responseHeaders": {
25+
"access-control-allow-credentials": [
26+
"true"
27+
],
28+
"access-control-allow-origin": [
29+
"https://www.youtube.com"
30+
],
31+
"alt-svc": [
32+
"h3\u003d\":443\"; ma\u003d2592000,h3-29\u003d\":443\"; ma\u003d2592000"
33+
],
34+
"cache-control": [
35+
"private, max-age\u003d0"
36+
],
37+
"content-type": [
38+
"text/javascript; charset\u003dutf-8"
39+
],
40+
"cross-origin-opener-policy": [
41+
"same-origin; report-to\u003d\"youtube_main\""
42+
],
43+
"date": [
44+
"Mon, 09 Oct 2023 09:27:33 GMT"
45+
],
46+
"expires": [
47+
"Mon, 09 Oct 2023 09:27:33 GMT"
48+
],
49+
"origin-trial": [
50+
"AvC9UlR6RDk2crliDsFl66RWLnTbHrDbp+DiY6AYz/PNQ4G4tdUTjrHYr2sghbkhGQAVxb7jaPTHpEVBz0uzQwkAAAB4eyJvcmlnaW4iOiJodHRwczovL3lvdXR1YmUuY29tOjQ0MyIsImZlYXR1cmUiOiJXZWJWaWV3WFJlcXVlc3RlZFdpdGhEZXByZWNhdGlvbiIsImV4cGlyeSI6MTcxOTUzMjc5OSwiaXNTdWJkb21haW4iOnRydWV9"
51+
],
52+
"p3p": [
53+
"CP\u003d\"This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl\u003den-GB for more info.\""
54+
],
55+
"permissions-policy": [
56+
"ch-ua-arch\u003d*, ch-ua-bitness\u003d*, ch-ua-full-version\u003d*, ch-ua-full-version-list\u003d*, ch-ua-model\u003d*, ch-ua-wow64\u003d*, ch-ua-form-factor\u003d*, ch-ua-platform\u003d*, ch-ua-platform-version\u003d*"
57+
],
58+
"report-to": [
59+
"{\"group\":\"youtube_main\",\"max_age\":2592000,\"endpoints\":[{\"url\":\"https://csp.withgoogle.com/csp/report-to/youtube_main\"}]}"
60+
],
61+
"server": [
62+
"ESF"
63+
],
64+
"set-cookie": [
65+
"YSC\u003deXlTV75IWtE; Domain\u003d.youtube.com; Path\u003d/; Secure; HttpOnly; SameSite\u003dnone",
66+
"VISITOR_INFO1_LIVE\u003d; Domain\u003d.youtube.com; Expires\u003dTue, 12-Jan-2021 09:27:33 GMT; Path\u003d/; Secure; HttpOnly; SameSite\u003dnone",
67+
"CONSENT\u003dPENDING+344; expires\u003dWed, 08-Oct-2025 09:27:33 GMT; path\u003d/; domain\u003d.youtube.com; Secure"
68+
],
69+
"strict-transport-security": [
70+
"max-age\u003d31536000"
71+
],
72+
"x-content-type-options": [
73+
"nosniff"
74+
],
75+
"x-frame-options": [
76+
"SAMEORIGIN"
77+
],
78+
"x-xss-protection": [
79+
"0"
80+
]
81+
},
82+
"responseBody": "\n self.addEventListener(\u0027install\u0027, event \u003d\u003e {\n event.waitUntil(self.skipWaiting());\n });\n self.addEventListener(\u0027activate\u0027, event \u003d\u003e {\n event.waitUntil(\n self.clients.claim().then(() \u003d\u003e self.registration.unregister()));\n });\n ",
83+
"latestUrl": "https://www.youtube.com/sw.js"
84+
}
85+
}

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/comments/channelOwner/generated_mock_1.json

Lines changed: 80 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)