From 8e345ce10add1552180cf47e4e0b13e75e509782 Mon Sep 17 00:00:00 2001 From: pan93412 Date: Sat, 12 Aug 2023 00:00:27 +0800 Subject: [PATCH] feat(lyrics-plus/netease): normalize album name before comparing --- CustomApps/lyrics-plus/ProviderNetease.js | 6 ++-- CustomApps/lyrics-plus/Utils.js | 37 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CustomApps/lyrics-plus/ProviderNetease.js b/CustomApps/lyrics-plus/ProviderNetease.js index 4a9f66d895..53902eb03a 100644 --- a/CustomApps/lyrics-plus/ProviderNetease.js +++ b/CustomApps/lyrics-plus/ProviderNetease.js @@ -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; diff --git a/CustomApps/lyrics-plus/Utils.js b/CustomApps/lyrics-plus/Utils.js index 9f1fcd9adb..fa09eec679 100644 --- a/CustomApps/lyrics-plus/Utils.js +++ b/CustomApps/lyrics-plus/Utils.js @@ -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} + */ + 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