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