Last active
August 31, 2024 20:47
-
-
Save VinayChaurasiyaA/3e588bb25316d2d23d36353be98d9e9c to your computer and use it in GitHub Desktop.
JUST A DUMMY
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
// 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 = { | |
"'": "'", | |
""": '"', | |
"&": "&", | |
"<": "<", | |
">": ">", | |
" ": " ", | |
"¡": "¡", | |
"¢": "¢", | |
"£": "£", | |
"¤": "¤", | |
"¥": "¥", | |
"¦": "¦", | |
"§": "§", | |
"¨": "¨", | |
"©": "©", | |
"ª": "ª", | |
"«": "«", | |
"¬": "¬", | |
"­": "", | |
"®": "®", | |
"¯": "¯", | |
"°": "°", | |
"±": "±", | |
"²": "²", | |
"³": "³", | |
"´": "´", | |
"µ": "µ", | |
"¶": "¶", | |
"·": "·", | |
"¸": "¸", | |
"¹": "¹", | |
"º": "º", | |
"»": "»", | |
"¼": "¼", | |
"½": "½", | |
"¾": "¾", | |
"¿": "¿", | |
"À": "À", | |
"Á": "Á", | |
"Â": "Â", | |
}; | |
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