youtube-moosick
Version:
Unofficial Youtube music API, fully written in TypeScript
168 lines • 8.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeneralParser = void 0;
const objectScan_utility_js_1 = require("../resources/utilities/objectScan.utility.js");
const enums_js_1 = require("../enums.js");
const index_js_1 = require("../resources/generalTypes/index.js");
const parsersExtended_js_1 = require("./parsersExtended.js");
const common_js_1 = require("../resources/etc/rawResultTypes/common.js");
/**
* Parser to deal with the output generated by the Moosick#search
*
* @remarks
* Do not use this class directly, unless for tests purposes
* @internal
*/
class GeneralParser {
// eslint-disable-next-line complexity
static parseSearchResult(context, searchType) {
const isUnsorted = searchType == null;
// prep all the parts
const albums = [];
const videos = [];
const playlists = [];
const artist = [];
const songs = [];
const unsorted = [];
const continuationMode = (0, objectScan_utility_js_1.$$)('.musicShelfContinuation')(context);
const continuation = searchType || continuationMode ? (0, objectScan_utility_js_1.$$)('.nextContinuationData')(context)[0] : undefined;
const musicShelf = (0, objectScan_utility_js_1.$$)('.musicShelfRenderer')(context).length === 0
? continuationMode
: (0, objectScan_utility_js_1.$$)('.musicShelfRenderer')(context);
for (const shelfItem of musicShelf) {
const shelfContent = (0, objectScan_utility_js_1.$$)('.musicResponsiveListItemRenderer')(shelfItem);
for (const item of shelfContent) {
const flexColumnRenderer = (0, objectScan_utility_js_1.$$)('.musicResponsiveListItemFlexColumnRenderer')(item);
const category = searchType ?? (flexColumnRenderer[enums_js_1.FlexColumnOffset.ALT].text.runs[0].text).toUpperCase();
switch (category) {
case 'SONG': {
const display = (0, objectScan_utility_js_1.$)('.musicResponsiveListItemFlexColumnRenderer')(item);
songs.push(index_js_1.Song.from({
...parsersExtended_js_1.ParsersExtended.flexSecondRowComplexParser(flexColumnRenderer[enums_js_1.FlexColumnOffset.ALT].text.runs, enums_js_1.Category.SONG, Boolean(searchType)),
...GeneralParser.musicResponsiveListItemRendererParser(item),
thumbnails: parsersExtended_js_1.ParsersExtended.thumbnailParser(item),
playlistId: (0, objectScan_utility_js_1.$)('.playlistId')(display),
params: common_js_1.WatchEndpointParams.WAEB,
}));
if (isUnsorted) {
unsorted.push(songs[songs.length - 1]);
}
break;
}
case 'VIDEO': {
videos.push(index_js_1.Video.from({
...GeneralParser.musicResponsiveListItemRendererParser(item),
...parsersExtended_js_1.ParsersExtended.flexSecondRowComplexParser(flexColumnRenderer[enums_js_1.FlexColumnOffset.ALT].text.runs, enums_js_1.Category.VIDEO, Boolean(searchType)),
thumbnails: parsersExtended_js_1.ParsersExtended.thumbnailParser(item),
}));
if (isUnsorted) {
unsorted.push(videos[videos.length - 1]);
}
break;
}
case 'PLAYLIST': {
playlists.push(index_js_1.Playlist.from({
name: (0, objectScan_utility_js_1.$)('.text')(flexColumnRenderer),
browseId: item.navigationEndpoint?.browseEndpoint?.browseId ?? '',
...parsersExtended_js_1.ParsersExtended.flexSecondRowComplexParser(flexColumnRenderer[enums_js_1.FlexColumnOffset.ALT].text.runs, enums_js_1.Category.PLAYLIST, Boolean(searchType)),
}));
if (isUnsorted) {
unsorted.push(playlists[playlists.length - 1]);
}
break;
}
case 'ARTIST': {
artist.push(index_js_1.ArtistExtended.from({
name: flexColumnRenderer[enums_js_1.FlexColumnOffset.MAIN].text.runs[enums_js_1.FlexColumnOffset.ONLYRUN].text,
browseId: item.navigationEndpoint?.browseEndpoint?.browseId ?? '',
thumbnails: parsersExtended_js_1.ParsersExtended.thumbnailParser(item),
url: `${enums_js_1.ConstantURLs.CHANNEL_URL}${item.navigationEndpoint?.browseEndpoint?.browseId ?? ''}`,
...parsersExtended_js_1.ParsersExtended.flexSecondRowComplexParser(flexColumnRenderer[enums_js_1.FlexColumnOffset.ALT].text.runs, enums_js_1.Category.ARTIST, Boolean(searchType)),
}));
if (isUnsorted) {
unsorted.push(artist[artist.length - 1]);
}
break;
}
case 'ALBUM':
case 'SINGLE':
case 'EP': {
albums.push(index_js_1.AlbumExtended.from({
...parsersExtended_js_1.ParsersExtended.flexSecondRowComplexParser(flexColumnRenderer[enums_js_1.FlexColumnOffset.ALT].text.runs, enums_js_1.Category.ALBUM, Boolean(searchType)),
name: flexColumnRenderer[enums_js_1.FlexColumnOffset.MAIN].text.runs[enums_js_1.FlexColumnOffset.ONLYRUN].text,
browseId: item.navigationEndpoint?.browseEndpoint?.browseId ?? '',
url: `${enums_js_1.ConstantURLs.CHANNEL_URL}${item.navigationEndpoint?.browseEndpoint?.browseId ?? ''}`,
thumbnails: parsersExtended_js_1.ParsersExtended.thumbnailParser(item),
}));
if (isUnsorted) {
unsorted.push(albums[albums.length - 1]);
}
break;
}
default: {
break;
}
}
}
}
switch (searchType) {
case enums_js_1.Category.SONG: {
return {
result: songs,
continuation,
};
}
case enums_js_1.Category.VIDEO: {
return {
result: videos,
continuation,
};
}
case enums_js_1.Category.ALBUM: {
return {
result: albums,
continuation,
};
}
case enums_js_1.Category.ARTIST: {
return {
result: artist,
continuation,
};
}
case enums_js_1.Category.PLAYLIST: {
return {
result: playlists,
continuation,
};
}
case undefined:
default: {
const result = new index_js_1.UnsortedFactory().create({
albums,
videos,
playlists,
artist,
songs,
});
result.push(...unsorted);
return {
result,
continuation,
};
}
}
}
/**
* Only works for video and song
*/
static musicResponsiveListItemRendererParser(musicResponsiveListItemRenderer) {
const display = (0, objectScan_utility_js_1.$)('.musicResponsiveListItemFlexColumnRenderer')(musicResponsiveListItemRenderer);
const name = (0, objectScan_utility_js_1.$)('.text')(display);
const id = (0, objectScan_utility_js_1.$)('.videoId')(display);
const url = `https://www.youtube.com/watch?v=${id}`;
return { name, url, videoId: id };
}
}
exports.GeneralParser = GeneralParser;
//# sourceMappingURL=generalParser.js.map