Skip to content

Commit

Permalink
fix(lyrics-plus/netease): check correctly for songs (#3018)
Browse files Browse the repository at this point in the history
Co-authored-by: dompling <374779789@qq.com>
Co-authored-by: ririxi <dev@ririxi.dev>
  • Loading branch information
3 people authored Jun 16, 2024
1 parent e50f6e2 commit 22cf2b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CustomApps/lyrics-plus/ProviderNetease.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const ProviderNetease = (() => {
// normalized expected album name
const neAlbumName = Utils.normalize(info.album);
const expectedAlbumName = Utils.containsHanCharacter(neAlbumName) ? await Utils.toSimplifiedChinese(neAlbumName) : neAlbumName;
const itemId = items.findIndex(val => Utils.normalize(val.album.name) === expectedAlbumName || Math.abs(info.duration - val.duration) < 1000);
let itemId = items.findIndex(val => Utils.normalize(val.album.name) === expectedAlbumName);
if (itemId === -1) itemId = items.findIndex(val => Math.abs(info.duration - val.duration) < 3000);
if (itemId === -1) itemId = items.findIndex(val => val.name === cleanTitle);
if (itemId === -1) throw "Cannot find track";

return await Spicetify.CosmosAsync.get(lyricURL + items[itemId].id, null, requestHeader);
Expand Down
44 changes: 35 additions & 9 deletions CustomApps/lyrics-plus/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,32 +134,58 @@ const Utils = {
},
processTranslatedLyrics(result, lyricsToTranslate, { state, stateName }) {
const translatedLines = result.split("\n");

state[stateName] = [];

for (let i = 0; i < lyricsToTranslate.length; i++)
state[stateName].push({
for (let i = 0; i < lyricsToTranslate.length; i++) {
const lyric = {
startTime: lyricsToTranslate[i].startTime || 0,
text: this.rubyTextToReact(translatedLines[i])
});
};
state[stateName].push(lyric);
}
},
processTranslatedOriginalLyrics(lyrics, synced) {
const data = [];
const dataSouce = {};

for (const item of lyrics) {
dataSouce[item.startTime] = { translate: item.text };
}

for (const time in synced) {
dataSouce[item.startTime] = {
...dataSouce[item.startTime],
text: item.text
};
}

for (const time in dataSouce) {
const item = dataSouce[time];
const lyric = {
startTime: time || 0,
text: this.rubyTextToOriginalReact(item.translate || item.text, item.text || item.translate)
};
data.push(lyric);
}

return data;
},
rubyTextToOriginalReact(translated, syncedText) {
const react = Spicetify.React;
return react.createElement("p1", null, [react.createElement("ruby", {}, syncedText, react.createElement("rt", null, translated))]);
},
rubyTextToReact(s) {
const react = Spicetify.React;

const rubyElems = s.split("<ruby>");
const reactChildren = [];

reactChildren.push(rubyElems[0]);

for (let i = 1; i < rubyElems.length; i++) {
const kanji = rubyElems[i].split("<rp>")[0];
const furigana = rubyElems[i].split("<rt>")[1].split("</rt>")[0];

reactChildren.push(react.createElement("ruby", null, kanji, react.createElement("rt", null, furigana)));

reactChildren.push(rubyElems[i].split("</ruby>")[1]);
}

return react.createElement("p1", null, reactChildren);
},
formatTime(timestamp) {
Expand Down

0 comments on commit 22cf2b0

Please sign in to comment.