Skip to content

Commit

Permalink
feat(lyrics-plus/netease): normalize album name before comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Aug 12, 2023
1 parent 894a9ce commit 8e345ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CustomApps/lyrics-plus/ProviderNetease.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ const ProviderNetease = (function () {
const expectedDuration = info.duration;
const actualDuration = song.dt;

const expectedAlbumName = Utils.normalize(info.album);
const actualAlbumName = Utils.normalize(song.al.name);
// normalized expected album name
const neAlbumName = Utils.normalize(info.album);
const expectedAlbumName = Utils.containsHanCharacter(neAlbumName) ? await Utils.toSimplifiedChinese(neAlbumName) : neAlbumName;
const actualAlbumName = Utils.normalize(song.al.name); // usually in Simplified Chinese

if (actualAlbumName == expectedAlbumName || Math.abs(expectedDuration - actualDuration) < 1000) {
return song;
Expand Down
37 changes: 37 additions & 0 deletions CustomApps/lyrics-plus/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,43 @@ class Utils {
return result.replace(/\s+/g, " ").trim();
}

/**
* Check if the specified string contains Han character.
*
* @param {string} s
*/
static containsHanCharacter(s) {
const hanRegex = /\p{Script=Han}/u;
return hanRegex.test(s);
}

/**
* Singleton Translator instance for {@link toSimplifiedChinese}.
*
* @type {Translator}
*/
static #translator = null;

/**
* Convert all Han characters to Simplified Chinese.
*
* Choosing Simplified Chinese makes the converted result more accurate,
* as the conversion from SC to TC may have multiple possibilities,
* while the conversion from TC to SC usually has only one possibility.
*
* @param {string} s
* @returns {Promise<string>}
*/
static async toSimplifiedChinese(s) {
// create a singleton Translator instance
if (!Utils.#translator) {
Utils.#translator = new Translator("zh");
}

// translate it to Simplified Chinese
return Utils.#translator.convertChinese(s, "tw", "cn");
}

static removeSongFeat(s) {
return (
s
Expand Down

0 comments on commit 8e345ce

Please sign in to comment.