aboutsummaryrefslogtreecommitdiff
path: root/node_modules/genius-lyrics/dist/Songs/Song.js
blob: 0dff6dd1591d94c62a4c709e406fb76fae852e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"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;