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
Besides, in Spotify Taiwan, the album name is in Traditional
Chinese, which is not aligned with what NCM chooses. Therefore,
I have written a simple converter that converts the Traditional
Chinese album name to Simplified Chinese so that we can compare
the correct album name in such cases.
  • Loading branch information
pan93412 committed Aug 12, 2023
1 parent 9ae997f commit e231bd6
Show file tree
Hide file tree
Showing 2 changed files with 42 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
38 changes: 38 additions & 0 deletions CustomApps/lyrics-plus/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,44 @@ class Utils {
return result.replace(/\s+/g, " ").trim();
}

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

/**
* Singleton Translator instance for {@link toSimplifiedChinese}.
*
* @type {Translator | null}
*/
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 e231bd6

Please sign in to comment.