youtube-moosick
Version:
Unofficial Youtube music API, fully written in TypeScript
67 lines • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetPlaylistParser = void 0;
const objectScan_utility_js_1 = require("../resources/utilities/objectScan.utility.js");
const parsersExtended_js_1 = require("./parsersExtended.js");
const playlistURL_js_1 = require("../resources/resultTypes/playlistURL.js");
const enums_js_1 = require("../enums.js");
const utils_js_1 = require("../utils.js");
/**
* Parser to deal with the output generated by the Moosick#getPlaylist
*
* @remarks
* Do not use this class directly, unless for tests purposes
* @internal
*/
class GetPlaylistParser {
static parsePlaylistURL(context) {
// Gets all the ListItemRenderers and pushes the first 2 out only
const flexColumns = [];
const fixedColumns = [];
const allResponsiveRenderers = (0, objectScan_utility_js_1.$$)('.musicResponsiveListItemRenderer')(context);
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < allResponsiveRenderers.length; i++) {
flexColumns.push(allResponsiveRenderers[i].flexColumns[0].musicResponsiveListItemFlexColumnRenderer);
flexColumns.push(allResponsiveRenderers[i].flexColumns[1].musicResponsiveListItemFlexColumnRenderer);
fixedColumns.push(allResponsiveRenderers[i].fixedColumns[0].musicResponsiveListItemFixedColumnRenderer);
}
const unprocessedHeader = (0, objectScan_utility_js_1.$$)('.musicDetailHeaderRenderer')(context);
const allThumbnailRenderers = ((0, objectScan_utility_js_1.$$)('.musicThumbnailRenderer')(context));
const continuation = ((0, objectScan_utility_js_1.$)('.nextContinuationData')(context));
const playlistContents = [];
for (let i = 0; i < flexColumns.length; i += 2) {
const flexColumnFirstRow = flexColumns[i];
playlistContents.push({
trackTitle: (0, objectScan_utility_js_1.$)('.text')(flexColumnFirstRow),
trackId: (0, objectScan_utility_js_1.$)('.videoId')(flexColumnFirstRow),
artist: parsersExtended_js_1.ParsersExtended.artistParser(flexColumns[i + 1].text.runs),
thumbnail: allThumbnailRenderers[Math.floor(i / 2)]?.thumbnail?.thumbnails ?? [],
duration: utils_js_1.utils.hms2ms(fixedColumns[Math.floor(i / 2)].text.runs[0].text),
});
}
return playlistURL_js_1.PlaylistURL.from({
headers: unprocessedHeader.length > 0
? GetPlaylistParser.playlistURLHeaderParser(unprocessedHeader)
: undefined,
playlistContents,
continuation,
});
}
/**
* Parses playlist header data and related information
* @param header - The header object from the returned axios request
* @internal
*/
static playlistURLHeaderParser(header) {
return playlistURL_js_1.PlaylistHeader.from({
playlistName: header[0].title.runs[enums_js_1.FlexColumnOffset.ONLYRUN].text,
owner: header[0].subtitle.runs[2].text,
createdYear: parseInt(header[0].subtitle.runs[4].text, 10),
thumbnail: header[0].thumbnail.croppedSquareThumbnailRenderer.thumbnail.thumbnails,
songCount: parseInt(header[0].secondSubtitle.runs[enums_js_1.FlexColumnOffset.ONLYRUN].text, 10),
approxRunTime: header[0].secondSubtitle.runs[2].text,
});
}
}
exports.GetPlaylistParser = GetPlaylistParser;
//# sourceMappingURL=getPlaylistParser.js.map