Created
August 31, 2024 20:27
-
-
Save VinayChaurasiyaA/d73ecd3f7a456173150b61f1f34e694c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function fetchLyricsFromGenius(songTitle) { | |
const accessToken = | |
"I CAN DM YOU IF YOU WANT"; | |
try { | |
console.log(`Searching for lyrics for "${songTitle}" from Genius API`); | |
const response = await fetch( | |
`https://api.genius.com/search?q=${encodeURIComponent( | |
songTitle | |
).toLowerCase()}`, | |
{ | |
headers: { | |
Authorization: `Bearer ${accessToken}`, | |
}, | |
} | |
); | |
console.log("API Response:", response); | |
if (!response.ok) { | |
throw new Error(`HTTP error! status: ${response.status}`); | |
} | |
const data = await response.json(); | |
console.log("API Data:", data); | |
const hits = data.response.hits; | |
if (hits.length === 0) { | |
console.log("No song found"); | |
return null; | |
} | |
const songUrl = hits[0].result.url; | |
console.log(`Song URL: ${songUrl}`); | |
const lyricsPage = await fetch(songUrl); | |
if (!lyricsPage.ok) { | |
throw new Error(`Failed to fetch lyrics page: ${lyricsPage.status}`); | |
} | |
const lyricsPageText = await lyricsPage.text(); | |
const regex = /<div[^>]+data-lyrics-container="true"[^>]*>(.*?)<\/div>/gs; | |
const matches = [...lyricsPageText.matchAll(regex)]; | |
if (matches.length > 0) { | |
const geniusLyrics = matches | |
.map((match) => { | |
return decodeHTMLEntities( | |
match[1] | |
.replace(/<br\s*\/?>/gi, "\n") | |
.replace(/<\/?[^>]+(>|$)/g, "") | |
); | |
}) | |
.join("\n"); | |
console.log(`Lyrics found: \n${geniusLyrics}`); | |
return geniusLyrics; | |
} else { | |
console.log("No lyrics found in the page content"); | |
return null; | |
} | |
} catch (error) { | |
console.error("Error in fetchLyricsFromGenius:", error); | |
throw error; | |
} | |
} | |
function decodeHTMLEntities(text) { | |
// Function to decode HTML entities | |
const entities = { | |
"'": "'", | |
""": '"', | |
"&": "&", | |
"<": "<", | |
">": ">", | |
" ": " ", | |
"¡": "¡", | |
"¢": "¢", | |
"£": "£", | |
"¤": "¤", | |
"¥": "¥", | |
"¦": "¦", | |
"§": "§", | |
"¨": "¨", | |
"©": "©", | |
"ª": "ª", | |
"«": "«", | |
"¬": "¬", | |
"­": "", | |
"®": "®", | |
"¯": "¯", | |
"°": "°", | |
"±": "±", | |
"²": "²", | |
"³": "³", | |
"´": "´", | |
"µ": "µ", | |
"¶": "¶", | |
"·": "·", | |
"¸": "¸", | |
"¹": "¹", | |
"º": "º", | |
"»": "»", | |
"¼": "¼", | |
"½": "½", | |
"¾": "¾", | |
"¿": "¿", | |
"À": "À", | |
"Á": "Á", | |
"Â": "Â", | |
}; | |
return text.replace( | |
/&[#A-Za-z0-9]+;/gi, | |
(entity) => entities[entity] || entity | |
); | |
} | |
// in createLyrics i am just making a call for fetchLyricsFromGenius | |
createLyrics: function () { | |
BetterLyrics.DOM.requestSongInfo(e => { | |
const song = e.song; | |
const artist = e.artist; | |
fetchLyricsFromGenius(song).then(lyrics => { | |
console.log("Lyrics:", JSON.stringify(lyrics)); | |
}).catch(error => { | |
console.error("Error:", error); | |
}); | |
BetterLyrics.Utils.log(BetterLyrics.Constants.FETCH_LYRICS_LOG, song, artist); | |
const url = `${BetterLyrics.Constants.LYRICS_API_URL}?s=${encodeURIComponent(BetterLyrics.Utils.unEntity(song))}&a=${encodeURIComponent(BetterLyrics.Utils.unEntity(artist))}`; | |
fetch(url) | |
.then(response => { | |
if (!response.ok) { | |
throw new Error(`HTTP error! status: ${response.status}`); | |
} | |
return response.json(); | |
}) | |
.then(data => { | |
const lyrics = data.lyrics; | |
BetterLyrics.Lyrics.lyrics = lyrics; // store the lyrics in the global variable | |
BetterLyrics.App.lang = data.language; | |
BetterLyrics.DOM.setRtlAttributes(data.isRtlLanguage); | |
clearInterval(BetterLyrics.App.lyricsCheckInterval); | |
if (!lyrics || lyrics.length === 0) { | |
// before this make a request to genius to get the lyrics if still no lyrics then return the next partt | |
lyrics = BetterLyrics.Lyrics.fetchLyricsFromGenius(song); | |
if (!lyrics || lyrics.length === 0) { | |
BetterLyrics.Utils.log(BetterLyrics.Constants.NO_LYRICS_FOUND_LOG); | |
setTimeout(BetterLyrics.DOM.injectError, 500); | |
return; | |
} | |
} | |
BetterLyrics.Utils.log(BetterLyrics.Constants.LYRICS_FOUND_LOG); | |
try { | |
const lyricsElement = document.getElementsByClassName(BetterLyrics.Constants.LYRICS_CLASS)[0]; | |
lyricsElement.innerHTML = ""; | |
} catch (_err) { | |
BetterLyrics.Utils.log(BetterLyrics.Constants.LYRICS_TAB_NOT_DISABLED_LOG); | |
} | |
BetterLyrics.Lyrics.injectLyrics(lyrics); | |
}) | |
.catch(err => { | |
clearInterval(BetterLyrics.App.lyricsCheckInterval); | |
BetterLyrics.Utils.log(BetterLyrics.Constants.SERVER_ERROR_LOG); | |
BetterLyrics.Utils.log(err); | |
setTimeout(BetterLyrics.DOM.injectError, 500); | |
}); | |
}); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment