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!

103 lines (102 loc) 4.85 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 Video, usually returned from `client.getVideo()` */ class Video extends _1.BaseVideo { /** @hidden */ constructor(video = {}) { super(); Object.assign(this, video); } /** * Load this instance with raw data from Youtube * * @hidden */ load(data) { var _a, _b, _c; super.load(data); const contents = data[3].response.contents.twoColumnWatchNextResults.results.results.contents; const videoInfo = _1.BaseVideo.parseRawData(data); // Comment Continuation Token this.comments = []; const continuation = (_c = (_b = (_a = contents[contents.length - 1]) === null || _a === void 0 ? void 0 : _a.itemSectionRenderer) === null || _b === void 0 ? void 0 : _b.continuations) === null || _c === void 0 ? void 0 : _c[0].nextContinuationData; if (continuation) { this._commentContinuation = { token: continuation.continuation, itct: continuation.clickTrackingParams, xsrfToken: data[3].xsrf_token, }; } // Duration this.duration = +videoInfo.videoDetails.lengthSeconds; return this; } /** * Load next 20 comments of the video, and push the loaded comments to {@link Video.comments} * You can only load up to 2000 comments from a video, this is due to the limitation from Youtube * * @example * ```js * const video = await youtube.getVideo(VIDEO_ID); * await video.nextComments(); * console.log(video.comments) // first 20 comments * * let newComments = await video.nextComments(); * console.log(newComments) // 20 loaded comments * console.log(video.comments) // first 40 comments * * await video.nextComments(0); // load the rest of the comments in the video * ``` * * @param count How many times to load the next comments. Set 0 to load all comments (might take a while on a video with many comments!) * * @returns Loaded comments */ nextComments(count = 1) { return __awaiter(this, void 0, void 0, function* () { const newComments = []; for (let i = 0; i < count || count == 0; i++) { if (!this._commentContinuation) break; // Send request const response = yield this.client.http.post(constants_1.COMMENT_END_POINT, { data: { session_token: this._commentContinuation.xsrfToken, action_get_comments: "1", pbj: "1", ctoken: this._commentContinuation.token, continuation: this._commentContinuation.token, itct: this._commentContinuation.itct, }, headers: { "content-type": "application/x-www-form-urlencoded" }, }); const { contents: comments, continuations, } = response.data.response.continuationContents.itemSectionContinuation; const continuation = continuations === null || continuations === void 0 ? void 0 : continuations.pop().nextContinuationData; this._commentContinuation = continuation ? { token: continuation.continuation, itct: continuation.clickTrackingParams, xsrfToken: response.data.xsrf_token, } : undefined; for (const comment of comments.map((c) => c.commentThreadRenderer)) { newComments.push(new _1.Comment({ video: this, client: this.client }).load(comment)); } } this.comments.push(...newComments); return newComments; }); } } exports.default = Video;