mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-10 10:11:46 +00:00
[lyrics] Romanization toggle for Genius plugin (#1039)
This commit is contained in:
@ -7,8 +7,13 @@ const fetch = require("node-fetch");
|
||||
|
||||
const { cleanupName } = require("../../providers/song-info");
|
||||
const { injectCSS } = require("../utils");
|
||||
let eastAsianChars = new RegExp("[\u{3040}-\u{30ff}\u{3400}-\u{4dbf}\u{4e00}-\u{9fff}\u{f900}-\u{faff}\u{ff66}-\u{ff9f}]");
|
||||
let revRomanized = false;
|
||||
|
||||
module.exports = async (win) => {
|
||||
module.exports = async (win, options) => {
|
||||
if(options.romanizedLyrics) {
|
||||
revRomanized = true;
|
||||
}
|
||||
injectCSS(win.webContents, join(__dirname, "style.css"));
|
||||
|
||||
ipcMain.on("search-genius-lyrics", async (event, extractedSongInfo) => {
|
||||
@ -17,10 +22,41 @@ module.exports = async (win) => {
|
||||
});
|
||||
};
|
||||
|
||||
const toggleRomanized = () => {
|
||||
revRomanized = !revRomanized;
|
||||
};
|
||||
|
||||
const fetchFromGenius = async (metadata) => {
|
||||
const queryString = `${cleanupName(metadata.artist)} ${cleanupName(
|
||||
metadata.title
|
||||
)}`;
|
||||
const songTitle = `${cleanupName(metadata.title)}`;
|
||||
const songArtist = `${cleanupName(metadata.artist)}`;
|
||||
let lyrics;
|
||||
|
||||
/* Uses Regex to test the title and artist first for said characters if romanization is enabled. Otherwise normal
|
||||
Genius Lyrics behavior is observed.
|
||||
*/
|
||||
let hasAsianChars = false;
|
||||
if (revRomanized && (eastAsianChars.test(songTitle) || eastAsianChars.test(songArtist))) {
|
||||
lyrics = await getLyricsList(`${songArtist} ${songTitle} Romanized`);
|
||||
hasAsianChars = true;
|
||||
} else {
|
||||
lyrics = await getLyricsList(`${songArtist} ${songTitle}`);
|
||||
}
|
||||
|
||||
/* If the romanization toggle is on, and we did not detect any characters in the title or artist, we do a check
|
||||
for characters in the lyrics themselves. If this check proves true, we search for Romanized lyrics.
|
||||
*/
|
||||
if(revRomanized && !hasAsianChars && eastAsianChars.test(lyrics)) {
|
||||
lyrics = await getLyricsList(`${songArtist} ${songTitle} Romanized`);
|
||||
}
|
||||
return lyrics;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches a JSON of songs which is then parsed and passed into getLyrics to get the lyrical content of the first song
|
||||
* @param {*} queryString
|
||||
* @returns The lyrics of the first song found using the Genius-Lyrics API
|
||||
*/
|
||||
const getLyricsList = async (queryString) => {
|
||||
let response = await fetch(
|
||||
`https://genius.com/api/search/multi?per_page=5&q=${encodeURIComponent(queryString)}`
|
||||
);
|
||||
@ -28,6 +64,9 @@ const fetchFromGenius = async (metadata) => {
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Fetch the first URL with the api, giving a collection of song results.
|
||||
Pick the first song, parsing the json given by the API.
|
||||
*/
|
||||
const info = await response.json();
|
||||
let url = "";
|
||||
try {
|
||||
@ -36,16 +75,23 @@ const fetchFromGenius = async (metadata) => {
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
let lyrics = await getLyrics(url);
|
||||
return lyrics;
|
||||
}
|
||||
|
||||
if (is.dev()) {
|
||||
console.log("Fetching lyrics from Genius:", url);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} url
|
||||
* @returns The lyrics of the song URL provided, null if none
|
||||
*/
|
||||
const getLyrics = async (url) => {
|
||||
response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is.dev()) {
|
||||
console.log("Fetching lyrics from Genius:", url);
|
||||
}
|
||||
const html = await response.text();
|
||||
const lyrics = convert(html, {
|
||||
baseElements: {
|
||||
@ -64,8 +110,8 @@ const fetchFromGenius = async (metadata) => {
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return lyrics;
|
||||
};
|
||||
|
||||
module.exports.fetchFromGenius = fetchFromGenius;
|
||||
module.exports.toggleRomanized = toggleRomanized;
|
||||
module.exports.fetchFromGenius = fetchFromGenius;
|
||||
17
plugins/lyrics-genius/menu.js
Normal file
17
plugins/lyrics-genius/menu.js
Normal file
@ -0,0 +1,17 @@
|
||||
const { setOptions } = require("../../config/plugins");
|
||||
const { toggleRomanized } = require("./back");
|
||||
|
||||
module.exports = (win, options, refreshMenu) => {
|
||||
return [
|
||||
{
|
||||
label: "Romanized Lyrics",
|
||||
type: "checkbox",
|
||||
checked: options.romanizedLyrics,
|
||||
click: (item) => {
|
||||
options.romanizedLyrics = item.checked;
|
||||
setOptions('lyrics-genius', options);
|
||||
toggleRomanized();
|
||||
},
|
||||
},
|
||||
];
|
||||
};
|
||||
Reference in New Issue
Block a user