Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lyrics] Romanization toggle for Genius plugin #1039

Merged
merged 19 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 58 additions & 14 deletions plugins/lyrics-genius/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -17,17 +22,49 @@ module.exports = async (win) => {
});
};

const toggleRomanized = () => {
revRomanized = !revRomanized;
};

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)}`
);
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=${encodeURI(queryString)}`);
if (!response.ok) {
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 {
Expand All @@ -36,16 +73,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: {
Expand All @@ -64,8 +108,8 @@ const fetchFromGenius = async (metadata) => {
},
},
});

return lyrics;
};

module.exports.fetchFromGenius = fetchFromGenius;
module.exports.toggleRomanized = toggleRomanized;
module.exports.fetchFromGenius = fetchFromGenius;
17 changes: 17 additions & 0 deletions plugins/lyrics-genius/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { setMenuOptions } = require("../../config/plugins");
DereC4 marked this conversation as resolved.
Show resolved Hide resolved
const { toggleRomanized } = require("./back");

module.exports = (win, options, refreshMenu) => {
return [
{
label: "Romanized Lyrics",
type: "checkbox",
checked: options.romanizedLyrics,
click: (item) => {
options.romanizedLyrics = item.checked;
setMenuOptions('lyrics-genius', options);
DereC4 marked this conversation as resolved.
Show resolved Hide resolved
toggleRomanized();
},
},
];
};