Skip to content

Commit c460efa

Browse files
comment options added
1 parent 605fe3f commit c460efa

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,44 @@ const humanizedClaims = result.receivedClaims.map(claim => {
161161
console.log(humanizedClaims) // ['"Fasl", by Kabul Dreams (starting at 2771 sec.)', ...]
162162
```
163163

164+
## Setting video comment options
165+
### Enable all video comments
166+
```js
167+
const { init, setCommentOptions } = require('youtube-studio')
168+
169+
await init({ ... }) // read more below (Preparing Authentication)
170+
171+
const result = await setCommentOptions({
172+
encryptedVideoId: 'hHbWF1Bvgf4', // your video ID
173+
commentOptions: {
174+
newAllowCommentsMode: "ALL_COMMENTS", // or "AUTOMATED_COMMENTS" or "APPROVED_COMMENTS" or "UNKNOWN_COMMENT_ALLOWED_MODE",
175+
newAllowComments: true, // should be "false" for newAllowCommentsMode="UNKNOWN_COMMENT_ALLOWED_MODE"
176+
newCanViewRatings: true, // Show how many viewers like and dislike this video
177+
newDefaultSortOrder: "MDE_COMMENT_SORT_ORDER_LATEST" // or "MDE_COMMENT_SORT_ORDER_TOP"
178+
}
179+
})
180+
181+
console.log(result)
182+
```
183+
### Disable video comments:
184+
```js
185+
const { init, setCommentOptions } = require('youtube-studio')
186+
187+
await init({ ... }) // read more below (Preparing Authentication)
188+
189+
const result = await setCommentOptions({
190+
encryptedVideoId: 'hHbWF1Bvgf4',
191+
commentOptions: {
192+
newAllowCommentsMode: "UNKNOWN_COMMENT_ALLOWED_MODE",
193+
newAllowComments: false,
194+
newCanViewRatings: true, // Show how many viewers like and dislike this video
195+
newDefaultSortOrder: "MDE_COMMENT_SORT_ORDER_LATEST"
196+
}
197+
})
198+
199+
console.log(result)
200+
```
201+
164202
## Preparing Authentication
165203

166204
#### STEP 1: Prepare cookies

src/youtube-studio-api.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ async function init({
7474
.then(res => res.json())
7575

7676
if (result.sessionToken) {
77-
console.log(result.sessionToken)
7877
sessionToken = result.sessionToken
7978
}
8079
}
@@ -181,6 +180,26 @@ async function setMonetisation(monetizationSettings) {
181180
.then(res => res.json())
182181
}
183182

183+
async function setCommentOptions(data) {
184+
let requestBody;
185+
186+
requestBody = _.cloneDeep(metadata_update_request_payload)
187+
188+
_.set(requestBody, 'context.user.onBehalfOfUser', config.DELEGATED_SESSION_ID);
189+
_.set(requestBody, 'context.request.sessionInfo.token', sessionToken);
190+
191+
requestBody = {
192+
...requestBody,
193+
...data
194+
}
195+
196+
return fetch(`${YT_STUDIO_URL}/youtubei/v1/video_manager/metadata_update?alt=json&key=${config.INNERTUBE_API_KEY}`, {
197+
method: 'POST',
198+
headers,
199+
body: `${JSON.stringify(requestBody)}`
200+
})
201+
.then(res => res.json())
202+
}
184203

185204
async function getVideoClaims(videoId) {
186205
const template = _.cloneDeep(get_creator_videos_template)
@@ -592,6 +611,7 @@ module.exports = {
592611
init,
593612
getVideo,
594613
setMonetisation,
614+
setCommentOptions,
595615
setEndScreen,
596616
getEndScreen,
597617
endScreen,

src/youtube-studio-api.spec.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
jest.requireActual('node-fetch')
22
const nconf = require('nconf')
33

4-
const { setMonetisation, init, setInfoCards, getVideo, setEndScreen, getEndScreen, endScreen, getVideoClaims, upload } = require('./youtube-studio-api');
4+
const { setMonetisation, setCommentOptions, init, setInfoCards, getVideo, setEndScreen, getEndScreen, endScreen, getVideoClaims, upload } = require('./youtube-studio-api');
55

66
nconf.env().file({ file: './config.json' });
77

@@ -77,14 +77,28 @@ describe('for authenticated user', () => {
7777
expect(result.monetizationSettings.success).toEqual(true)
7878
})
7979

80+
it('should set comment options', async () => {
81+
const result = await setCommentOptions({
82+
encryptedVideoId: VIDEO_ID,
83+
commentOptions: {
84+
newAllowComments: true,
85+
newAllowCommentsMode: "APPROVED_COMMENTS",
86+
newCanViewRatings: true,
87+
newDefaultSortOrder: "MDE_COMMENT_SORT_ORDER_LATEST"
88+
}
89+
})
90+
91+
expect(result.commentOptions.success).toEqual(true)
92+
})
93+
8094
it('should get video', async () => {
8195
const result = await getVideo(VIDEO_ID)
8296

8397
const video = result.videos[0];
8498

8599
expect(video.videoId).toEqual(VIDEO_ID)
86100
expect(video.status).toEqual("VIDEO_STATUS_PROCESSED")
87-
expect(video.monetization.adMonetization.effectiveStatus).toEqual("VIDEO_MONETIZING_STATUS_NOT_MONETIZING_OFF")
101+
expect(video.monetization.adMonetization.effectiveStatus).toEqual("VIDEO_MONETIZING_STATUS_MONETIZING_WITH_LIMITED_ADS")
88102
expect(video.lengthSeconds).toEqual("1404")
89103
expect(video.watchUrl).toEqual("https://youtu.be/" + VIDEO_ID)
90104
})

0 commit comments

Comments
 (0)