UNPKG

ultra-lyrics

Version:

Lyrics Fetcher

31 lines (30 loc) 1.7 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 }); exports.getLyrics = void 0; const cheerio_1 = require("cheerio"); const Constants_1 = require("./Constants"); const Utils_1 = __importDefault(require("./Utils")); const getLyrics = (param) => __awaiter(void 0, void 0, void 0, function* () { const id = typeof param === 'number' ? param : param.id; const { response } = yield Utils_1.default.fetch(`${Constants_1.API_BASE_URL}/songs/${id}`, { responseType: 'json' }); for (let i = 0; i < 6; i++) { const lyrics = yield Utils_1.default.fetch(response.song.url, { responseType: 'text' }); const data = cheerio_1.load(lyrics)('div.lyrics').text().trim(); if (data.length > 0) return { data, error: null }; } return { data: null, error: new Error('No lyrics found') }; }); exports.getLyrics = getLyrics;