youtubei
Version:
Simple package to get information from youtube such as videos, playlists, channels, video information & comments, related videos, up next video, and more!
87 lines (86 loc) • 5.62 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { getContinuationFromItems, Thumbnails } from "../../common";
import { BaseVideoParser } from "../BaseVideo";
import { Comment } from "../Comment";
var VideoParser = /** @class */ (function () {
function VideoParser() {
}
VideoParser.loadVideo = function (target, data) {
var _a, _b, _c, _d, _e, _f, _g, _h;
var videoInfo = BaseVideoParser.parseRawData(data);
var mutations = videoInfo.frameworkUpdates.entityBatchUpdate.mutations;
var lastMarkers = (_a = mutations
.find(function (m) { var _a; return (_a = m.payload) === null || _a === void 0 ? void 0 : _a.macroMarkersListEntity; })) === null || _a === void 0 ? void 0 : _a.payload.macroMarkersListEntity.markersList.markers.at(-1);
target.duration =
+((_b = videoInfo.videoDetails) === null || _b === void 0 ? void 0 : _b.lengthSeconds) ||
(lastMarkers ? (+lastMarkers.startMillis + +lastMarkers.durationMillis) / 1000 : 0);
var itemSectionRenderer = (_c = data.response.contents.twoColumnWatchNextResults.results.results.contents
.reverse()
.find(function (c) { return c.itemSectionRenderer; })) === null || _c === void 0 ? void 0 : _c.itemSectionRenderer;
target.comments.continuation = getContinuationFromItems((itemSectionRenderer === null || itemSectionRenderer === void 0 ? void 0 : itemSectionRenderer.contents) || []);
var chapters = (_f = (_e = (_d = data.response.playerOverlays.playerOverlayRenderer.decoratedPlayerBarRenderer) === null || _d === void 0 ? void 0 : _d.decoratedPlayerBarRenderer.playerBar) === null || _e === void 0 ? void 0 : _e.multiMarkersPlayerBarRenderer.markersMap) === null || _f === void 0 ? void 0 : _f[0].value.chapters;
target.chapters =
(chapters === null || chapters === void 0 ? void 0 : chapters.map(function (_a) {
var c = _a.chapterRenderer;
return ({
title: c.title.simpleText,
start: c.timeRangeStartMillis,
thumbnails: new Thumbnails().load(c.thumbnail.thumbnails),
});
})) || [];
var musicPanel = (_g = data.response.engagementPanels) === null || _g === void 0 ? void 0 : _g.find(function (e) { var _a, _b; return (_b = (_a = e.engagementPanelSectionListRenderer.content) === null || _a === void 0 ? void 0 : _a.structuredDescriptionContentRenderer) === null || _b === void 0 ? void 0 : _b.items.find(function (i) { var _a, _b; return ((_b = (_a = i.horizontalCardListRenderer) === null || _a === void 0 ? void 0 : _a.footerButton) === null || _b === void 0 ? void 0 : _b.buttonViewModel.iconName) === "MUSIC"; }); });
if (!musicPanel) {
target.music = null;
}
else {
var cards = musicPanel.engagementPanelSectionListRenderer.content.structuredDescriptionContentRenderer.items.find(function (i) { var _a, _b; return ((_b = (_a = i.horizontalCardListRenderer) === null || _a === void 0 ? void 0 : _a.footerButton) === null || _b === void 0 ? void 0 : _b.buttonViewModel.iconName) === "MUSIC"; }).horizontalCardListRenderer.cards;
var music = cards.find(function (i) { return i.videoAttributeViewModel; })
.videoAttributeViewModel;
target.music = {
imageUrl: music.image.sources[0].url,
title: music.title,
artist: music.subtitle,
album: ((_h = music.secondarySubtitle) === null || _h === void 0 ? void 0 : _h.content) || null,
};
}
// target.music =
return target;
};
VideoParser.parseComments = function (data, video) {
var endpoints = data.onResponseReceivedEndpoints.find(function (c) {
var _a;
return (c.appendContinuationItemsAction ||
((_a = c.reloadContinuationItemsCommand) === null || _a === void 0 ? void 0 : _a.slot) === "RELOAD_CONTINUATION_SLOT_BODY");
});
var repliesContinuationItems = (endpoints.reloadContinuationItemsCommand || endpoints.appendContinuationItemsAction).continuationItems;
var comments = data.frameworkUpdates.entityBatchUpdate.mutations
.filter(function (m) { return m.payload.commentEntityPayload; })
.map(function (m) {
var _a;
var repliesItems = (_a = repliesContinuationItems.find(function (r) {
return r.commentThreadRenderer.commentViewModel.commentKey === m.key;
})) === null || _a === void 0 ? void 0 : _a.commentThreadRenderer;
return __assign(__assign({}, m.payload.commentEntityPayload), repliesItems);
});
return comments.map(function (c) {
return new Comment({ video: video, client: video.client }).load(c);
});
};
VideoParser.parseCommentContinuation = function (data) {
var endpoints = data.onResponseReceivedEndpoints.at(-1);
var continuationItems = (endpoints.reloadContinuationItemsCommand || endpoints.appendContinuationItemsAction).continuationItems;
return getContinuationFromItems(continuationItems);
};
return VideoParser;
}());
export { VideoParser };