Path: blob/master/node_modules/@bochilteam/scraper/lib/esm/others/lyrics.js
1126 views
import got from 'got';1import cheerio from 'cheerio';2import { ScraperError } from '../utils.js';3export async function lyrics(query) {4const data = await got(`https://www.musixmatch.com/search/${encodeURIComponent(query)}`).text();5const $ = cheerio.load(data);6const results = [];7$('#search-all-results > div.main-panel > div:nth-child(2) > div.box-content > div > ul.tracks.list > li.showArtist.showCoverart').each(function () {8var _a;9const el = (_a = $(this).find('meta[itemprop="url"]').attr('content')) === null || _a === void 0 ? void 0 : _a.trim();10if (el) {11results.push({12link: 'https://www.musixmatch.com' + el,13title: $(this).find('.media-card-title > a > span').text().trim(),14author: $(this).find('.artist-field > span > a.artist').text().trim()15});16}17});18if (!results.length)19throw new ScraperError(`Can't get lyrics!\n${$.html()}`);20const { link, title, author } = results[0];21const html = await got(link).text();22const $$ = cheerio.load(html);23return {24title,25author,26lyrics: $$('p.mxm-lyrics__content > span.lyrics__content__ok').map((_, el) => $$(el).text().trim()).toArray().filter(v => v).join('\n'),27link28};29}30export async function lyricsv2(query) {31var _a, _b;32const data = await got(`https://genius.com/api/search/multi?per_page=5&q=${encodeURIComponent(query)}`, {33headers: {34accept: 'application/json, text/plain, */*',35'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'36}37}).json();38// @ts-ignore39const result = (_b = (_a = data.response.sections.find((section) => {40var _a;41return ['song', 'lyric'].includes(section.type) &&42((_a = section.hits) === null || _a === void 0 ? void 0 : _a.find((hit) => ['song', 'lyric'].includes(hit.type)));43}).hits) === null || _a === void 0 ? void 0 : _a.find((hit) => ['song', 'lyric'].includes(hit.type))) === null || _b === void 0 ? void 0 : _b.result;44if (!result)45throw new ScraperError(`Can't get json!\n${JSON.stringify(data)}`);46const { artist_names, title, url } = result;47if (!url)48throw new ScraperError(`Can't get lyrics!\n${JSON.stringify(data, null, 2)}`);49const html = await got(url).text();50const $ = cheerio.load(html);51let results = '';52$('#lyrics-root > div[data-lyrics-container="true"]').each((_, el) => {53const element = $(($(el).html() || '').replace(/<br>/g, '\n')).text().trim();54if (element)55results += element;56});57return {58title,59author: artist_names,60lyrics: results.trim(),61link: url62};63}64//# sourceMappingURL=lyrics.js.map6566