lyrics-dumper
Version:
Lyrics Dumper. Get lyrics from google with everything.
35 lines (34 loc) • 1.61 kB
JavaScript
;
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 node_fetch_1 = __importDefault(require("node-fetch"));
const cheerio_1 = __importDefault(require("cheerio"));
const clearRegex = /((\[|\()(?!.*?(remix|edit|remake)).*?(\]|\))|\/+|-+| x |,|"|video oficial|official lyric video| ft.?|\|+|yhlqmdlg|x100pre|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF]|\u274C)/g, clearRegex2 = / +/g;
async function getLyrics(song) {
song = song
.toLowerCase()
.replace(clearRegex, "")
.replace(clearRegex2, " ")
.trim();
const html = await (0, node_fetch_1.default)(`https://www.google.com/search?q=${encodeURIComponent(song)}+lyrics&ie=UTF-8&tob=true`, {}).then((x) => x.textConverted());
const $ = cheerio_1.default.load(html);
const lyrics = $('div[class="hwc"]')
.find('div[class="BNeawe tAd8D AP7Wnd"]')
.map((i, value) => {
return $(value);
}), nameStuff = $('span[class="BNeawe tAd8D AP7Wnd"]')?.text(), artistsStuff = $('span[class="BNeawe s3v9rd AP7Wnd"]').map((i, value) => {
return $(value);
}), sourceStuff = $('span[class="uEec3 AP7Wnd"]')?.text();
if (!lyrics?.[0])
throw new Error("Lyrics not found!");
return {
title: nameStuff,
artist: artistsStuff[1]?.text(),
lyrics: lyrics[0]?.text(),
source: sourceStuff,
};
}
exports.getLyrics = getLyrics;