aboutsummaryrefslogtreecommitdiff
path: root/node_modules/genius-lyrics/dist/Songs/Client.js
blob: be25629132f10299d8abbee3f5269905af4bc51d (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
"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.SongsClient = void 0;
const got_1 = __importDefault(require("got"));
const Song_1 = require("./Song");
const Constants_1 = require("../Constants");
class SongsClient {
    /**
     * @example const SongsClient = new Genius.Songs.Client(key);
     */
    constructor(client) {
        this.client = client;
    }
    /**
     * Searches for songs for the provided query (Key is optional)
     * @example const SearchResults = await SongsClient.search("faded");
     */
    search(query, options) {
        var _a, _b, _c, _d;
        return __awaiter(this, void 0, void 0, function* () {
            const { sanitizeQuery } = Object.assign({ sanitizeQuery: true }, options);
            if (typeof query !== "string") {
                throw new Error("'query' must be a type of 'string'");
            }
            const term = encodeURIComponent(sanitizeQuery ? this.sanitizeQuery(query) : query);
            let result = [];
            if (this.client.key) {
                const data = yield this.client.api.get(`/search?q=${term}`);
                const parsed = JSON.parse(data);
                result = parsed.response.hits;
            }
            else {
                const res = yield got_1.default.get(`${((_a = this.client.config.origin) === null || _a === void 0 ? void 0 : _a.url) || Constants_1.Constants.UN_BASE_URL}/search/multi?per_page=5&q=${term}`, Object.assign(Object.assign({}, this.client.config.requestOptions), { headers: Object.assign({ "User-Agent": Constants_1.Constants.DEF_USER_AGENT }, (_b = this.client.config.requestOptions) === null || _b === void 0 ? void 0 : _b.headers) }));
                const parsed = JSON.parse(res.body);
                if (!((_c = parsed === null || parsed === void 0 ? void 0 : parsed.response) === null || _c === void 0 ? void 0 : _c.sections)) {
                    throw new Error(Constants_1.Constants.NO_RESULT);
                }
                const __hits = parsed.response.sections.find((s) => s.type === "song");
                if (!((_d = __hits === null || __hits === void 0 ? void 0 : __hits.hits) === null || _d === void 0 ? void 0 : _d.length)) {
                    throw new Error(Constants_1.Constants.NO_RESULT);
                }
                result = __hits.hits;
            }
            return result
                .filter((s) => s.type === "song")
                .map((s) => new Song_1.Song(this.client, s.result, true));
        });
    }
    /**
     * Fetches the Song using the provided ID (Requires Key)
     * @example const Song = await SongsClient.get(3276244);
     */
    get(id) {
        return __awaiter(this, void 0, void 0, function* () {
            if (typeof id !== "number") {
                throw new Error("'id' must be a type of 'number'");
            }
            if (!this.client.key) {
                throw new Error(Constants_1.Constants.REQUIRES_KEY);
            }
            const data = yield this.client.api.get(`/songs/${id}`);
            const parsed = JSON.parse(data);
            return new Song_1.Song(this.client, parsed.response.song, false);
        });
    }
    // Source: https://github.com/farshed/genius-lyrics-api/blob/110397a9f05fe20c4ded92418430f665f074c4e4/lib/utils/index.js#L15
    sanitizeQuery(query) {
        return query
            .toLowerCase()
            .replace(/ *\([^)]*\) */g, "")
            .replace(/ *\[[^\]]*]/, "")
            .replace(/feat.|ft./g, "")
            .replace(/\s+/g, " ")
            .trim();
    }
}
exports.SongsClient = SongsClient;