Skip to content

Commit

Permalink
comment options added
Browse files Browse the repository at this point in the history
  • Loading branch information
adamplocieniak committed Jan 5, 2022
1 parent 605fe3f commit c460efa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,44 @@ const humanizedClaims = result.receivedClaims.map(claim => {
console.log(humanizedClaims) // ['"Fasl", by Kabul Dreams (starting at 2771 sec.)', ...]
```

## Setting video comment options
### Enable all video comments
```js
const { init, setCommentOptions } = require('youtube-studio')

await init({ ... }) // read more below (Preparing Authentication)

const result = await setCommentOptions({
encryptedVideoId: 'hHbWF1Bvgf4', // your video ID
commentOptions: {
newAllowCommentsMode: "ALL_COMMENTS", // or "AUTOMATED_COMMENTS" or "APPROVED_COMMENTS" or "UNKNOWN_COMMENT_ALLOWED_MODE",
newAllowComments: true, // should be "false" for newAllowCommentsMode="UNKNOWN_COMMENT_ALLOWED_MODE"
newCanViewRatings: true, // Show how many viewers like and dislike this video
newDefaultSortOrder: "MDE_COMMENT_SORT_ORDER_LATEST" // or "MDE_COMMENT_SORT_ORDER_TOP"
}
})

console.log(result)
```
### Disable video comments:
```js
const { init, setCommentOptions } = require('youtube-studio')

await init({ ... }) // read more below (Preparing Authentication)

const result = await setCommentOptions({
encryptedVideoId: 'hHbWF1Bvgf4',
commentOptions: {
newAllowCommentsMode: "UNKNOWN_COMMENT_ALLOWED_MODE",
newAllowComments: false,
newCanViewRatings: true, // Show how many viewers like and dislike this video
newDefaultSortOrder: "MDE_COMMENT_SORT_ORDER_LATEST"
}
})

console.log(result)
```

## Preparing Authentication

#### STEP 1: Prepare cookies
Expand Down
22 changes: 21 additions & 1 deletion src/youtube-studio-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ async function init({
.then(res => res.json())

if (result.sessionToken) {
console.log(result.sessionToken)
sessionToken = result.sessionToken
}
}
Expand Down Expand Up @@ -181,6 +180,26 @@ async function setMonetisation(monetizationSettings) {
.then(res => res.json())
}

async function setCommentOptions(data) {
let requestBody;

requestBody = _.cloneDeep(metadata_update_request_payload)

_.set(requestBody, 'context.user.onBehalfOfUser', config.DELEGATED_SESSION_ID);
_.set(requestBody, 'context.request.sessionInfo.token', sessionToken);

requestBody = {
...requestBody,
...data
}

return fetch(`${YT_STUDIO_URL}/youtubei/v1/video_manager/metadata_update?alt=json&key=${config.INNERTUBE_API_KEY}`, {
method: 'POST',
headers,
body: `${JSON.stringify(requestBody)}`
})
.then(res => res.json())
}

async function getVideoClaims(videoId) {
const template = _.cloneDeep(get_creator_videos_template)
Expand Down Expand Up @@ -592,6 +611,7 @@ module.exports = {
init,
getVideo,
setMonetisation,
setCommentOptions,
setEndScreen,
getEndScreen,
endScreen,
Expand Down
18 changes: 16 additions & 2 deletions src/youtube-studio-api.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
jest.requireActual('node-fetch')
const nconf = require('nconf')

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

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

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

it('should set comment options', async () => {
const result = await setCommentOptions({
encryptedVideoId: VIDEO_ID,
commentOptions: {
newAllowComments: true,
newAllowCommentsMode: "APPROVED_COMMENTS",
newCanViewRatings: true,
newDefaultSortOrder: "MDE_COMMENT_SORT_ORDER_LATEST"
}
})

expect(result.commentOptions.success).toEqual(true)
})

it('should get video', async () => {
const result = await getVideo(VIDEO_ID)

const video = result.videos[0];

expect(video.videoId).toEqual(VIDEO_ID)
expect(video.status).toEqual("VIDEO_STATUS_PROCESSED")
expect(video.monetization.adMonetization.effectiveStatus).toEqual("VIDEO_MONETIZING_STATUS_NOT_MONETIZING_OFF")
expect(video.monetization.adMonetization.effectiveStatus).toEqual("VIDEO_MONETIZING_STATUS_MONETIZING_WITH_LIMITED_ADS")
expect(video.lengthSeconds).toEqual("1404")
expect(video.watchUrl).toEqual("https://youtu.be/" + VIDEO_ID)
})
Expand Down

0 comments on commit c460efa

Please sign in to comment.