UNPKG

ytmusic_api_unofficial

Version:

A simple API to get music from YouTube Music

137 lines 7.01 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Thumbnail_1 = require("./Thumbnail"); const Artist_1 = __importDefault(require("./Artist")); const Duration_1 = __importDefault(require("./Duration")); const default_1 = require("../utils/default"); const Album_1 = __importDefault(require("./Album")); const request_1 = __importDefault(require("../utils/request")); const utils_1 = require("../utils/utils"); const responseBuilder_1 = require("../utils/responseBuilder"); const error_1 = require("../utils/error"); class Music { constructor(data) { var _a, _b, _c, _d, _e, _f, _g, _h; this.thumbnails = (0, utils_1.thumbnail_defaults_size)((_c = (_a = data === null || data === void 0 ? void 0 : data.thumbnails) === null || _a === void 0 ? void 0 : _a[((_b = data === null || data === void 0 ? void 0 : data.thumbnails) === null || _b === void 0 ? void 0 : _b.length) - 1]) === null || _c === void 0 ? void 0 : _c.url, (_d = data === null || data === void 0 ? void 0 : data.thumbnails) === null || _d === void 0 ? void 0 : _d.map((thumbnail) => new Thumbnail_1.Thumbnail(thumbnail))); this.id = data.videoId; this.browseID = data.browseID; this.title = data.title; this.artists = (_e = data === null || data === void 0 ? void 0 : data.artists) === null || _e === void 0 ? void 0 : _e.map((artist) => new Artist_1.default(artist)); this.resultType = data.resultType; if (data.album) { data.album.artists = data === null || data === void 0 ? void 0 : data.artists; this.album = new Album_1.default(data.album); } this.videoType = data.videoType; this.duration = new Duration_1.default(data.duration); this.year = data.year; this.isAudioOnly = (_f = this.videoType) === null || _f === void 0 ? void 0 : _f.includes('ATV'); this.isExplicit = !!data.isExplicit; this.radioPlaylistID = (_g = data.radioPlID) === null || _g === void 0 ? void 0 : _g.playlistId; this.radioPlaylistCode = (_h = data.radioPlID) === null || _h === void 0 ? void 0 : _h.params; this.relativeBrowseID = data.relativeBrowseID; this.isTopResult = !!data.isTopResult; } /** * Get the name of the artists separated by a comma * @example "Rick Astley, Foo Fighters" * @example * ```js * const get = await ytMusic.get("Never Gonna Give You Up") * console.log(get.artistsNames) * // Output: "Rick Astley" * ``` * @returns The name of the artists */ get artistsNames() { return this.artists.map(artist => artist.name).join(', '); } /** * Get the lyrics of the music if it's available * @returns The lyrics of the music */ getLyrics() { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { if (!this.browseID) { yield (0, request_1.default)("next", { videoId: this.id }).then((res) => { Object.assign(this, (0, utils_1.parseGetResult)(res, 'song')); }).catch(reject); } (0, request_1.default)('browse', { browseId: this.browseID, context: { client: { clientVersion: "1.20230522.01.00", clientName: "WEB_REMIX" } } }).then((res) => { var _a; if (!(0, responseBuilder_1.nav)(res, ['contents', 'sectionListRenderer', 'contents', 0, 'musicDescriptionShelfRenderer', 'description'], true)) return reject((0, error_1.error)(2003, (0, responseBuilder_1.nav)(res, ['contents', 'messageRenderer', 'text', 'runs', 0, 'text']))); else { const resolveData = { lyrics: (_a = (0, responseBuilder_1.nav)(res, ['contents', 'sectionListRenderer', 'contents', 0, 'musicDescriptionShelfRenderer', 'description', 'runs', 0, 'text'], true)) === null || _a === void 0 ? void 0 : _a.replace(/\r\n/g, '\n'), source: (0, responseBuilder_1.nav)(res, ['contents', 'sectionListRenderer', 'contents', 0, 'musicDescriptionShelfRenderer', 'footer', 'runs', 0, 'text'], true).replace('Source: ', '') }; resolve(resolveData); } }).catch(reject); })); } /** * Download the music * @param format - The format of the music (Check available formats) * @param quality - The quality of the music (Check available qualities) **/ download() { return __awaiter(this, arguments, void 0, function* (format = default_1.AvailableFormat[0], quality = default_1.AvailableQuality[0]) { try { return yield (0, utils_1.downloadYTDL)(this.id, format, quality); } catch (e) { throw e; } }); } /** * Get the radio playlist of the music */ getRadioPlaylist() { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { if (!this.radioPlaylistID || !this.radioPlaylistCode) { yield (0, request_1.default)("next", { videoId: this.id }).then((res) => { Object.assign(this, (0, utils_1.parseGetResult)(res, 'song')); }).catch(reject); } (0, request_1.default)('next', { playlistId: this.radioPlaylistID, params: this.radioPlaylistCode, isAudioOnly: this.isAudioOnly }).then((res) => { return resolve((0, utils_1.parseGetResult)(res, 'autoMix')); }).catch(reject); })); } /** * Return Thumbnail with custom size * @param width - The width of the thumbnail * @param height - The height of the thumbnail */ getThumbnail(width, height) { return new Thumbnail_1.Thumbnail({ url: (0, utils_1.customThumbnailSize)(this.thumbnails[0].url, width, height), width: width, height: height }); } } exports.default = Music; //# sourceMappingURL=Music.js.map