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