Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const options = {
apiKey: 'XXXXXXXXXXXXXXXXXXXXXXX',
title: 'Posthumous Forgiveness',
artist: 'Tame Impala',
optimizeQuery: true
optimizeQuery: true,
onlyText: true
};

getLyrics(options).then((lyrics) => console.log(lyrics));
Expand All @@ -50,7 +51,30 @@ type options {
artist: string;
apiKey: string; // Genius developer access token
optimizeQuery?: boolean; // (optional, default: false) If true, perform some cleanup to maximize the chance of finding a match
authHeader?: boolean; // (optional, default: false) Whether to include auth header in the search request
authHeader?: boolean; // (optional, default: false) Whether to include auth header in the search request,
onlyText: boolean; //(optional, default: true) whether to return the lyrics in Text or html format(needs sanitizing in DOM to remove conflicts, then you can style it accordingly)
}

To sanitize the returned lyrics in html format, you can do this:
{
const lyricsTag = document.querySelectorAll("div a");
const lyricskek = document.querySelectorAll("div a span");
const lyricslol = document.querySelectorAll("div span");

for(i=0; i <= lyricsTag.length; i++){
lyricsTag[i].removeAttribute("href");
lyricsTag[i].removeAttribute("class");
};

for(i=0; i <= lyricskek.length; i++){
lyricskek[i].removeAttribute("style");
lyricskek[i].removeAttribute("class");
};

for(i=0; i <= lyricslol.length; i++){
lyricslol[i].removeAttribute("style");
lyricslol[i].removeAttribute("class");
};
}

```
Expand Down
20 changes: 14 additions & 6 deletions lib/getLyrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ const { checkOptions } = require('./utils');
const extractLyrics = require('./utils/extractLyrics');

/**
* @param {({apiKey: string, title: string, artist: string, optimizeQuery: boolean}|string)} arg - options object, or Genius URL
* @param {({apiKey: string, title: string, artist: string, optimizeQuery: boolean}|string, onlyText:boolen)} arg - options object, or Genius URL
*/
module.exports = async function (arg) {
try {
if (arg && typeof arg === 'string') {
let lyrics = await extractLyrics(arg);
let lyrics = await extractLyrics.extractLyrics(arg);
return lyrics;
} else if (typeof arg === 'object') {
checkOptions(arg);
let results = await searchSong(arg);
if (!results) return null;
let lyrics = await extractLyrics(results[0].url);
return lyrics;
if(arg.onlyText == false){
let results = await searchSong(arg);
if (!results) return null;
let lyrics = await extractLyrics.extractLyricsHtml(results[0].url);
return lyrics;
} else {
let results = await searchSong(arg);
if (!results) return null;
let lyrics = await extractLyrics.extractLyrics(results[0].url);
return lyrics;
}

} else {
throw 'Invalid argument';
}
Expand Down
32 changes: 31 additions & 1 deletion lib/utils/extractLyrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const cheerio = require('cheerio-without-node-native');
/**
* @param {string} url - Genius URL
*/
module.exports = async function (url) {


const lyricsFunction = {};


lyricsFunction.extractLyrics = async (url) => {
try {
let { data } = await axios.get(url);
const $ = cheerio.load(data);
Expand All @@ -27,3 +32,28 @@ module.exports = async function (url) {
throw e;
}
};

lyricsFunction.extractLyricsHtml = async (url) => {
try {
let { data } = await axios.get(url);
const $ = cheerio.load(data);
let lyrics = $('div[class="lyrics"]').text().trim();
if (!lyrics) {
lyrics = '';
$('div[class^="Lyrics__Container"]').each((i, elem) => {
if ($(elem).text().length !== 0) {
let snippet = $(elem)
.html();
console.log(snippet);
lyrics = snippet;
}
});
}
if (!lyrics) return null;
return lyrics;
} catch (e) {
throw e;
}
};

module.exports = lyricsFunction;
Loading