UNPKG

@sushibtw/youtubei

Version:

Simple package to get information from youtube such as videos, playlists, channels, video information & comments, related videos, up next video, and more!

102 lines (101 loc) 4.91 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const _1 = require("."); const constants_1 = require("../constants"); /** Represents a Comment / Reply */ class Comment extends _1.Base { /** @hidden */ constructor(comment = {}) { super(); Object.assign(this, comment); } /** * Load this instance with raw data from Youtube * * @hidden */ load(data) { var _a, _b; const { authorText, authorThumbnail, authorEndpoint, contentText, publishedTimeText, commentId, likeCount, authorIsChannelOwner, pinnedCommentBadge, replyCount, } = data.comment.commentRenderer; // Basic information this.id = commentId; this.content = contentText.runs.map((r) => r.text).join(""); this.publishDate = publishedTimeText.runs.shift().text; this.likeCount = likeCount; this.isAuthorChannelOwner = authorIsChannelOwner; this.isPinnedComment = !!pinnedCommentBadge; this.replyCount = replyCount; // Reply Continuation this.replies = []; const continuation = (_a = data.replies) === null || _a === void 0 ? void 0 : _a.commentRepliesRenderer.continuations[0].nextContinuationData; if (continuation) { this._replyContinuation = { token: continuation.continuation, itct: continuation.clickTrackingParams, xsrfToken: (_b = this.video._commentContinuation) === null || _b === void 0 ? void 0 : _b.xsrfToken, }; } // Author const { browseId } = authorEndpoint.browseEndpoint; this.author = new _1.Channel({ id: browseId, name: authorText.simpleText, thumbnails: new _1.Thumbnails().load(authorThumbnail.thumbnails), client: this.client, }); return this; } /** URL to the video with this comment being highlighted (appears on top of the comment section) */ get url() { return `https://www.youtube.com/watch?v=${this.video.id}&lc=${this.id}`; } /** Load next replies of the comment */ nextReplies(count = 1) { var _a; return __awaiter(this, void 0, void 0, function* () { const newReplies = []; for (let i = 0; i < count || count == 0; i++) { if (!this._replyContinuation) break; // Send request const response = yield this.client.http.post(constants_1.COMMENT_END_POINT, { data: { session_token: this._replyContinuation.xsrfToken, action_get_comment_replies: "1", pbj: "1", ctoken: this._replyContinuation.token, continuation: this._replyContinuation.token, itct: (_a = this._replyContinuation) === null || _a === void 0 ? void 0 : _a.itct, type: "next", }, headers: { "content-type": "application/x-www-form-urlencoded" }, }); const { contents: items, continuations, } = response.data[1].response.continuationContents.commentRepliesContinuation; if (continuations === null || continuations === void 0 ? void 0 : continuations.length) { const continuation = continuations.shift().nextContinuationData; this._replyContinuation = { token: continuation.continuation, itct: continuation.clickTrackingParams, xsrfToken: response.data[1].xsrf_token, }; } else { this._replyContinuation = undefined; } newReplies.push(...items.map((i) => new _1.Reply({ video: this.video, comment: this, client: this.client }).load(i.commentRenderer))); } this.replies.push(...newReplies); return newReplies; }); } } exports.default = Comment;