mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-10 10:11:46 +00:00
70 lines
1.6 KiB
JavaScript
70 lines
1.6 KiB
JavaScript
const { join } = require("path");
|
|
|
|
const { ipcMain } = require("electron");
|
|
const is = require("electron-is");
|
|
const { convert } = require("html-to-text");
|
|
const fetch = require("node-fetch");
|
|
|
|
const { cleanupName } = require("../../providers/song-info");
|
|
const { injectCSS } = require("../utils");
|
|
|
|
module.exports = async (win) => {
|
|
injectCSS(win.webContents, join(__dirname, "style.css"));
|
|
|
|
ipcMain.on("search-genius-lyrics", async (event, extractedSongInfo) => {
|
|
const metadata = JSON.parse(extractedSongInfo);
|
|
event.returnValue = await fetchFromGenius(metadata);
|
|
});
|
|
};
|
|
|
|
const fetchFromGenius = async (metadata) => {
|
|
const queryString = `${cleanupName(metadata.artist)} ${cleanupName(
|
|
metadata.title
|
|
)}`;
|
|
let response = await fetch(
|
|
`https://genius.com/api/search/multi?per_page=5&q=${encodeURI(queryString)}`
|
|
);
|
|
if (!response.ok) {
|
|
return null;
|
|
}
|
|
|
|
const info = await response.json();
|
|
let url = "";
|
|
try {
|
|
url = info.response.sections.filter((section) => section.type === "song")[0]
|
|
.hits[0].result.url;
|
|
} catch {
|
|
return null;
|
|
}
|
|
|
|
if (is.dev()) {
|
|
console.log("Fetching lyrics from Genius:", url);
|
|
}
|
|
|
|
response = await fetch(url);
|
|
if (!response.ok) {
|
|
return null;
|
|
}
|
|
|
|
const html = await response.text();
|
|
const lyrics = convert(html, {
|
|
baseElements: {
|
|
selectors: ['[class^="Lyrics__Container"]', ".lyrics"],
|
|
},
|
|
selectors: [
|
|
{
|
|
selector: "a",
|
|
format: "linkFormatter",
|
|
},
|
|
],
|
|
formatters: {
|
|
// Remove links by keeping only the content
|
|
linkFormatter: (elem, walk, builder) => {
|
|
walk(elem.children, builder);
|
|
},
|
|
},
|
|
});
|
|
|
|
return lyrics;
|
|
};
|