Skip to content

Commit

Permalink
feat: new API getComment
Browse files Browse the repository at this point in the history
  • Loading branch information
uetchy committed Mar 4, 2022
1 parent 6da05f2 commit fd3d177
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 21 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

## Unreleased

- BREAKING: normalize `replacementItem` in `ReplaceChatItemAction`
- New: `getComment` for fetching video comment by id
- BREAKING: normalize `ReplaceChatItemAction.replacementItem` interfaces
- New params for `AddBannerAction`: `viewerIsCreator`, `targetId`
- BREAKING: incompatible name changes
- `AddBannerAction.id` is renamed to `AddBannerAction.actionId` as actionId
- `AddBannerAction.id` is now become liveChatId
- BREAKING: incompatible property name changes
- `AddBannerAction.id` which is actually action id has been renamed to `AddBannerAction.actionId`
- `AddBannerAction.id` now refer to chat id
- New API `getComments` for fetching video comments
- Move cli tools (`tools/`) to [`masterchat-cli`](https://github.com/holodata/masterchat-cli)

Expand Down
39 changes: 34 additions & 5 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function main() {
// "private" => No permission (private video)
// "unavailable" => Deleted OR wrong video id
// "unarchived" => Live stream recording is not available
// "denied" => Access denied
// "denied" => Access denied (429)
// "invalid" => Invalid request
});

Expand All @@ -59,6 +59,7 @@ async function main() {
console.log("Live stream has ended");
}

// start polling live chat API
mc.listen();
}

Expand All @@ -79,8 +80,8 @@ async function main() {
);

mc.on("chats", async (chats) => {
const jsonl = chats.map((chat) => JSON.stringify(chat)).join("\n");
await appendFile("./chats.jsonl", jsonl + "\n");
const jsonl = chats.map((chat) => JSON.stringify(chat)).join("\n") + "\n";
await appendFile("./chats.jsonl", jsonl);

// save checkpoint
await writeFile("./checkpoint", continuation.token);
Expand All @@ -92,7 +93,7 @@ async function main() {
main();
```
### Tailor-made moderation bot
### Chat moderation bot
```js
import { Masterchat, stringify } from "masterchat";
Expand All @@ -117,7 +118,12 @@ async function main() {
emojiHandler: (emoji) => "",
});

if (isSpam(message)) await mc.remove(action.id);
if (isSpam(message) || /UGLY/.test(message)) {
// delete chat
// if flagged as spam by Spamreaper
// or contains "UGLY"
await mc.remove(action.id);
}
}
});

Expand All @@ -127,6 +133,29 @@ async function main() {
main();
```
### Get video comments (≠ live chats)
```js
import { getComments, getComment } from "masterchat";

async function main() {
// Iterate over all comments
let res = getComments("<videoId>", { top: true });
while (true) {
console.log(res.comments);

if (!res.next) break;
res = await res.next();
}

// Get comment by id
const comment = await getComment("<videoId>", "<commentId>");
console.log(comment);
}

main();
```
## Advanced usage
### Faster instantiation
Expand Down
55 changes: 45 additions & 10 deletions src/comments/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
import { getComments } from ".";
import { csc } from "../protobuf/assembler";
import assert from "assert";
import { getComment, getComments } from ".";
import { YTCommentThreadRenderer } from "..";
import { stringify } from "../utils";

it("can fetch comments", async () => {
const videoId = "q5ctC_sWU4g";
const commentId = "UgzNuL5flAW9vygeE9V4AaABAg";

const res = await getComments(videoId, {
highlightedCommentId: commentId,
});
const res = await getComments(videoId, { top: true });
const first = res.comments[0];
prettyPrint(first);
});

it("can fetch comment by id", async () => {
const videoId = "q5ctC_sWU4g";
const commentId = "UgzNuL5flAW9vygeE9V4AaABAg";

const comment = await getComment(videoId, commentId);
assert(comment);

prettyPrint(comment);
const fetchedId = comment.comment.commentRenderer.commentId;

expect(fetchedId).toEqual(commentId);
});

it("return undefined if wrong id specified", async () => {
const videoId = "q5ctC_sWU4g";
const commentId = "UgzNuL5flAW9vygeE9V4AaABAgwrong";

const comment = await getComment(videoId, commentId);
expect(comment).toBeUndefined();
});

function prettyPrint(comment: YTCommentThreadRenderer) {
const id = comment.comment.commentRenderer.commentId;
const membership =
first.comment.commentRenderer.sponsorCommentBadge
comment.comment.commentRenderer.sponsorCommentBadge
?.sponsorCommentBadgeRenderer.tooltip;
const authorName = first.comment.commentRenderer.authorText.simpleText;
console.log("Membership status for comment id:", commentId, "is", membership);
});
const authorName = comment.comment.commentRenderer.authorText.simpleText;
const message = stringify(comment.comment.commentRenderer.contentText);

console.log(
"id:",
id,
"membership:",
membership,
"author:",
authorName,
"message:",
message
);
}
12 changes: 12 additions & 0 deletions src/comments/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EP_NXT } from "../constants";
import {
RenderingPriority,
YTCommentThreadRenderer,
YTContinuationItem,
} from "../interfaces/yt/comments";
Expand All @@ -8,6 +9,17 @@ import { withContext, ytFetch } from "../utils";

// Comment

export async function getComment(videoId: string, commentId: string) {
const comments = await getComments(videoId, {
highlightedCommentId: commentId,
});
const first = comments.comments?.[0];
if (first.renderingPriority !== RenderingPriority.LinkedComment)
return undefined;

return first;
}

export async function getComments(
videoId: string,
continuation: string | CscOptions = {}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./comments";
export * from "./errors";
export * from "./interfaces";
export * from "./masterchat";
Expand All @@ -18,4 +19,3 @@ export {
tsToDate,
tsToNumber,
} from "./utils";
export * from "./comments";
3 changes: 2 additions & 1 deletion src/interfaces/yt/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ export enum VoteStatus {
}

export enum RenderingPriority {
RenderingPriorityUnknown = "RENDERING_PRIORITY_UNKNOWN",
Unknown = "RENDERING_PRIORITY_UNKNOWN",
LinkedComment = "RENDERING_PRIORITY_LINKED_COMMENT",
}

export interface Replies {
Expand Down

0 comments on commit fd3d177

Please sign in to comment.