"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 }); exports.Song = void 0; const got_1 = __importDefault(require("got")); const cheerio_1 = __importDefault(require("cheerio")); const Album_1 = require("../Albums/Album"); const Artist_1 = require("../Artists/Artist"); const Constants_1 = require("../Constants"); class Song { constructor(client, res, partial = false) { this.client = client; this.partial = partial; this.title = res.title; this.fullTitle = res.full_title; this.featuredTitle = res.title_with_featured; this.id = +res.id; this.thumbnail = res.header_image_thumbnail_url; this.image = res.header_image_url; this.url = res.url; this.endpoint = res.api_path; this.artist = new Artist_1.Artist(this.client, res.primary_artist, true); this.partial = partial; this.album = !this.partial && res.album ? new Album_1.Album(res.album, this.artist) : undefined; this.releasedAt = !this.partial && res.release_date ? new Date(res.release_date) : undefined; this.raw = res; } /** * Fetches Lyrics of the Track * @example const Lyrics = await Song.lyrics(true); */ lyrics(removeChorus = false) { var _a; return __awaiter(this, void 0, void 0, function* () { if (typeof removeChorus !== "boolean") { throw new Error("'removeChorus' must be a type of 'boolean'"); } const { body } = yield got_1.default.get(this.url, Object.assign(Object.assign({}, this.client.config.requestOptions), { headers: Object.assign({ "User-Agent": Constants_1.Constants.DEF_USER_AGENT }, (_a = this.client.config.requestOptions) === null || _a === void 0 ? void 0 : _a.headers) })); const $ = cheerio_1.default.load(body); const selectors = [ () => $(".lyrics").text().trim(), () => $("div[class*='Lyrics__Container']") .toArray() .map((x) => { const ele = $(x); ele.find("br").replaceWith("\n"); return ele.text().trim(); }) .join("\n\n") .trim(), ]; for (const x of selectors) { const lyrics = x(); if (lyrics === null || lyrics === void 0 ? void 0 : lyrics.length) { return removeChorus ? this.removeChorus(lyrics) : lyrics; } } throw new Error(Constants_1.Constants.NO_RESULT); }); } /** * Fetches All Information about the Track and updates all the existing Properties (Requires Key) * @example const NewSong = await Song.fetch(); */ fetch() { return __awaiter(this, void 0, void 0, function* () { if (!this.client.key) { throw new Error(Constants_1.Constants.REQUIRES_KEY); } const data = yield this.client.api.get(`/songs/${this.id}`); const parsed = JSON.parse(data); this.album = parsed.response.song.album ? new Album_1.Album(parsed.response.song.album, this.artist) : undefined; this.releasedAt = parsed.response.song.release_date ? new Date(parsed.response.song.release_date) : undefined; this.partial = false; return this; }); } removeChorus(lyrics) { return lyrics.replace(/^\[[^\]]+\]$/g, ""); } } exports.Song = Song;