Skip to content

Instantly share code, notes, and snippets.

@VinayChaurasiyaA
Last active August 31, 2024 20:47
Show Gist options
  • Save VinayChaurasiyaA/3e588bb25316d2d23d36353be98d9e9c to your computer and use it in GitHub Desktop.
Save VinayChaurasiyaA/3e588bb25316d2d23d36353be98d9e9c to your computer and use it in GitHub Desktop.
JUST A DUMMY
// Your Genius API access token
async function fetchLyricsFromGenius(songTitle) {
// const searches = await Client.songs.search("faded");
// const firstSong = searches[0];
// const geniusLyrics = await firstSong.lyrics();
// TODO: add a api call to genius to get the lyrics if there is no lyrics in the current page
const accessToken = "";
try {
console.log(`Searching for lyrics for "${songTitle}"...`);
const response = await fetch(
`https://api.genius.com/search?q=${encodeURIComponent(
songTitle
).toLowerCase()}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
const data = await response.json();
const hits = data.response.hits;
console.log(data);
if (hits.length === 0) {
console.log("No song found");
return;
}
// Extract the URL of the song lyrics page
const songUrl = hits[0].result.url;
console.log(`Song URL: ${songUrl}`);
// Step 2: Fetch the lyrics from the song URL
const lyricsPage = await fetch(songUrl);
const lyricsPageText = await lyricsPage.text();
// More refined regex to target Genius lyrics containers
const regex = /<div[^>]+data-lyrics-container="true"[^>]*>(.*?)<\/div>/gs;
const matches = [...lyricsPageText.matchAll(regex)];
if (matches.length > 0) {
// Extract and decode the lyrics
const geniusLyrics = matches
.map((match) => {
const decoded = decodeHTMLEntities(
match[1]
.replace(/<br\s*\/?>/gi, "\n") // Convert <br> tags to new lines
.replace(/<\/?[^>]+(>|$)/g, "") // Remove any remaining HTML tags
);
return decoded;
})
.join("\n");
console.log(`Lyrics: \n${geniusLyrics}`);
return geniusLyrics;
} else {
// BetterLyrics.Utils.log(BetterLyrics.Constants.SERVER_ERROR_LOG);
console.error("Error fetching lyrics:", error.message);
}
} catch (error) {
// BetterLyrics.Utils.log(BetterLyrics.Constants.SERVER_ERROR_LOG);
console.error("Error fetching lyrics:", error.message);
}
}
function decodeHTMLEntities(text) {
// Function to decode HTML entities
const entities = {
"&#x27;": "'",
"&quot;": '"',
"&amp;": "&",
"&lt;": "<",
"&gt;": ">",
"&nbsp;": " ",
"&iexcl;": "¡",
"&cent;": "¢",
"&pound;": "£",
"&curren;": "¤",
"&yen;": "¥",
"&brvbar;": "¦",
"&sect;": "§",
"&uml;": "¨",
"&copy;": "©",
"&ordf;": "ª",
"&laquo;": "«",
"&not;": "¬",
"&shy;": "­",
"&reg;": "®",
"&macr;": "¯",
"&deg;": "°",
"&plusmn;": "±",
"&sup2;": "²",
"&sup3;": "³",
"&acute;": "´",
"&micro;": "µ",
"&para;": "¶",
"&middot;": "·",
"&cedil;": "¸",
"&sup1;": "¹",
"&ordm;": "º",
"&raquo;": "»",
"&frac14;": "¼",
"&frac12;": "½",
"&frac34;": "¾",
"&iquest;": "¿",
"&Agrave;": "À",
"&Aacute;": "Á",
"&Acirc;": "Â",
};
return text.replace(
/&[#A-Za-z0-9]+;/gi,
(entity) => entities[entity] || entity
);
}
// Replace with your song title
fetchLyricsFromGenius("pakizaah");
// getLyrics("Naina Da Kya Kasoor");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment