ultra-lyrics
Version:
Lyrics Fetcher
41 lines (40 loc) • 2.08 kB
JavaScript
;
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 __1 = require("..");
const Utils_1 = __importDefault(require("../Utils"));
/**
* Function to get lyrics from Genius.com
* @param {number | ISearchResults[0]} - ID of the song or an elemeny of the array of SearchResults
* @returns {Promise<UltraLyricsFunctionReturnType<{ error: null | Error, data: string }>> - The promise that resolves to the lyrics
*/
const getLyrics = (param) => __awaiter(void 0, void 0, void 0, function* () {
try {
const id = typeof param === 'number' ? param : param.id;
const { response } = yield Utils_1.default.fetch(`${__1.API_BASE_URL}/songs/${encodeURIComponent(`${id}`)}`);
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') };
}
catch (err) {
return { data: null, error: err };
}
});
exports.getLyrics = getLyrics;