From 8bcbff7cd262c143c042e632ca54169c0f099863 Mon Sep 17 00:00:00 2001 From: the1812 Date: Wed, 11 Dec 2019 21:42:41 +0800 Subject: [PATCH] Improve multiple disc support --- dist/cli/index.js | 15 +++++++++++++-- dist/core/metadata/thb-wiki.js | 6 +++--- package.json | 2 +- src/cli/index.ts | 14 ++++++++++++-- src/core/metadata/thb-wiki.ts | 11 ++++++----- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/dist/cli/index.js b/dist/cli/index.js index 0364307..fa81392 100644 --- a/dist/cli/index.js +++ b/dist/cli/index.js @@ -15,16 +15,27 @@ const getMetadata = async (album) => { const metadata = await sourceMappings[cliOptions.source].getMetadata(album); console.log('创建文件中...'); const { readdirSync, renameSync } = await Promise.resolve().then(() => require('fs')); + const { dirname } = await Promise.resolve().then(() => require('path')); const { writerMappings } = await Promise.resolve().then(() => require('../core/writer/writer-mappings')); const fileTypes = Object.keys(writerMappings); - const files = readdirSync('.').filter(file => fileTypes.some(type => file.endsWith(type))).slice(0, metadata.length); + const fileTypeFilter = (file) => fileTypes.some(type => file.endsWith(type)); + const dir = readdirSync('.'); + const discFiles = dir.filter(f => f.match(/^Disc (\d+)/)).flatMap(f => readdirSync(f).map(inner => `${f}/${inner}`)); + const files = dir.filter(fileTypeFilter).concat(discFiles).slice(0, metadata.length); if (files.length === 0) { console.log('未找到任何支持的音乐文件.'); process.exit(); } const targetFiles = files.map((file, index) => { const maxLength = Math.max(Math.trunc(Math.log10(metadata.length)) + 1, 2); - return `${(index + 1).toString().padStart(maxLength, '0')} ${metadata[index].title}${path_1.extname(file)}`.replace(/[\/\\:\*\?"<>\|]/g, ''); + let dir = dirname(file); + if (dir === '.') { + dir = ''; + } + else { + dir += '/'; + } + return dir + `${metadata[index].trackNumber.padStart(maxLength, '0')} ${metadata[index].title}${path_1.extname(file)}`.replace(/[\/\\:\*\?"<>\|]/g, ''); }); files.forEach((file, index) => { renameSync(file, targetFiles[index]); diff --git a/dist/core/metadata/thb-wiki.js b/dist/core/metadata/thb-wiki.js index a03831c..dd929c1 100644 --- a/dist/core/metadata/thb-wiki.js +++ b/dist/core/metadata/thb-wiki.js @@ -139,11 +139,11 @@ class THBWiki { } return { title, - artists: [...new Set(artists)], + artists: [...new Set(artists)].map(it => it.replace('(人物)', '')), trackNumber, comments, - lyricists, - composers, + lyricists: lyricists ? lyricists.map(it => it.replace('(人物)', '')) : lyricists, + composers: composers ? composers.map(it => it.replace('(人物)', '')) : composers, }; } async getMetadata(albumName) { diff --git a/package.json b/package.json index 2e584a3..39ae037 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "touhou-tagger", - "version": "1.0.3", + "version": "1.0.4", "description": "从 THBWiki 自动填写东方Project CD曲目信息.", "main": "dist/core/index.js", "bin": { diff --git a/src/cli/index.ts b/src/cli/index.ts index 6731270..ad1e7f1 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -17,16 +17,26 @@ const getMetadata = async (album: string) => { const metadata = await sourceMappings[cliOptions.source].getMetadata(album) console.log('创建文件中...') const { readdirSync, renameSync } = await import('fs') + const { dirname } = await import('path') const { writerMappings } = await import('../core/writer/writer-mappings') const fileTypes = Object.keys(writerMappings) - const files = readdirSync('.').filter(file => fileTypes.some(type => file.endsWith(type))).slice(0, metadata.length) + const fileTypeFilter = (file: string) => fileTypes.some(type => file.endsWith(type)) + const dir = readdirSync('.') + const discFiles = dir.filter(f => f.match(/^Disc (\d+)/)).flatMap(f => readdirSync(f).map(inner => `${f}/${inner}`)) + const files = dir.filter(fileTypeFilter).concat(discFiles).slice(0, metadata.length) if (files.length === 0) { console.log('未找到任何支持的音乐文件.') process.exit() } const targetFiles = files.map((file, index) => { const maxLength = Math.max(Math.trunc(Math.log10(metadata.length)) + 1, 2) - return `${(index + 1).toString().padStart(maxLength, '0')} ${metadata[index].title}${extname(file)}`.replace(/[\/\\:\*\?"<>\|]/g, '') + let dir = dirname(file) + if (dir === '.') { + dir = '' + } else { + dir += '/' + } + return dir + `${metadata[index].trackNumber.padStart(maxLength, '0')} ${metadata[index].title}${extname(file)}`.replace(/[\/\\:\*\?"<>\|]/g, '') }) files.forEach((file, index) => { renameSync(file, targetFiles[index]) diff --git a/src/core/metadata/thb-wiki.ts b/src/core/metadata/thb-wiki.ts index 49090d6..8c1ec29 100644 --- a/src/core/metadata/thb-wiki.ts +++ b/src/core/metadata/thb-wiki.ts @@ -128,11 +128,12 @@ export class THBWiki implements MetadataSource { const [comments] = infos .filter(it => it.name === 'comments') .map(it => it.result as string) - const artists = ['vocals', 'coverVocals', 'harmonyVocals', 'accompanyVocals', 'chorusVocals','instruments', 'remix', 'arrangers'] + const artists = ['vocals', 'coverVocals', 'harmonyVocals', 'accompanyVocals', 'chorusVocals', 'instruments', 'remix', 'arrangers'] .flatMap(name => infos .filter(it => it.name === name) .map(it => it.result as string[]) - .flat()) + .flat() + ) const [composers] = infos .filter(it => it.name === 'composers') .map(it => it.result as string[]) @@ -141,11 +142,11 @@ export class THBWiki implements MetadataSource { } return { title, - artists: [...new Set(artists)], + artists: [...new Set(artists)].map(it => it.replace('(人物)', '')), trackNumber, comments, - lyricists, - composers, + lyricists: lyricists ? lyricists.map(it => it.replace('(人物)', '')) : lyricists, + composers: composers ? composers.map(it => it.replace('(人物)', '')) : composers, } } async getMetadata(albumName: string) {