Skip to content

Commit

Permalink
fix: 当有元数据获取失败时直接失败
Browse files Browse the repository at this point in the history
  • Loading branch information
XIAYM-gh committed Aug 18, 2024
1 parent f884683 commit 63bf665
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simmusic-ncm-ext",
"version": "0.0.5-alpha.2+for.0.1.0",
"version": "0.0.5-alpha.3+for.0.1.0",
"type": "module",
"scripts": {
"build": "vite build"
Expand Down
70 changes: 37 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,51 +242,54 @@ ExtensionConfig.ncm = {
musicList: {
async _import(callback: Function, id: string, isUpdate: boolean = false) {
let list: Array<MusicListEntry> = config.getItem('ext.ncm.musicList');
if (!isUpdate) {
for (let entry of list) {
if (entry.id == id) {
return alert('此歌单(' + entry.name + ')已被添加,请尝试删除后重试。')
}
if (!isUpdate) {
for (let entry of list) {
if (entry.id == id) {
return alert('此歌单(' + entry.name + ')已被添加,请尝试删除后重试。')
}
}
}

// 解析歌单
try {
const resp = await request('/playlist/detail', { id });
const name = resp.playlist.name;
let ids = resp.playlist.tracks.map((it: any) => it.id);
let filtered = 0;

if (config.getItem('ext.ncm.filterInvalid')) {
const len = ids.length;
// 解析歌单
try {
const resp = await request('/playlist/detail', { id });
const name = resp.playlist.name;
let ids = resp.playlist.tracks.map((it: any) => it.id);
let filtered = 0;

resp.privileges.forEach((it: any) => {
playableMap[it.id] = it.plLevel != 'none';
});
if (config.getItem('ext.ncm.filterInvalid')) {
const len = ids.length;

ids = ids.filter((it: string) => playableMap[it]);
filtered = len - ids.length;
}
resp.privileges.forEach((it: any) => {
playableMap[it.id] = it.plLevel != 'none';
});

const metadata = await fetchMetadata(...ids);
ids = ids.filter((it: string) => playableMap[it]);
filtered = len - ids.length;
}

if (isUpdate) {
list = list.filter(it => it.id != id);
}
const metadata = await fetchMetadata(...ids);
if (Object.keys(metadata).length != ids.length) {
throw '获取歌曲元数据时发生错误。';
}

const newEntry: MusicListEntry = { id, name, songs: metadata };
list.push(newEntry);
if (isUpdate) {
list = list.filter(it => it.id != id);
}

config.setItem('ext.ncm.musicList', list);
const newEntry: MusicListEntry = { id, name, songs: metadata };
list.push(newEntry);

if (isUpdate) {
ExtensionConfig.ncm.musicList.switchList(id);
}
config.setItem('ext.ncm.musicList', list);

alert('成功导入歌单 ' + name + ',共导入 ' + ids.length + ' 首歌曲' + (filtered ? ',' + filtered + ' 首因无法播放被过滤' : '') + '。', callback);
} catch (err) {
alert('导入歌单失败,请稍后重试:' + err);
if (isUpdate) {
ExtensionConfig.ncm.musicList.switchList(id);
}

alert('成功导入歌单 ' + name + ',共导入 ' + ids.length + ' 首歌曲' + (filtered ? ',' + filtered + ' 首因无法播放被过滤' : '') + '。', callback);
} catch (err) {
alert('导入歌单失败,请稍后重试:' + err);
}
},

add(callback: Function) {
Expand Down Expand Up @@ -346,6 +349,7 @@ ExtensionConfig.ncm = {
label: '从列表中移除', click() {
confirm(`确认移除网易云歌单 ${entry.name} 吗?`, () => {
const currentList: Array<MusicListEntry> = config.getItem('ext.ncm.musicList');
console.log(currentList.filter(it => it.id != entry.id));
config.setItem('ext.ncm.musicList', currentList.filter(it => it.id != entry.id));

if (element.classList.contains('active')) {
Expand Down

0 comments on commit 63bf665

Please sign in to comment.